带有SQL Server数据库错误的C#Game Server

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带有SQL Server数据库错误的C#Game Server相关的知识,希望对你有一定的参考价值。

我有一个游戏服务器。它是用SQL Server 2005用C#编写的。服务器运行正常大约一个小时左右,但随后出现了一些数据库错误。我对C#并不太了解,所以我不确定错误是什么以及导致它的原因。这是控制台输出:

enter image description here

Update:

好吧,试着按照狗仔队的“简单指示”......我修改了代码:

public virtual StoredProcRet StoredProc(string Procedure, params SqlParameter[] Params)
{
    SqlConnection Connection = new SqlConnection(ConnectionString);

    DataSet Results;
    int RETURN_VALUE;

    try
    {
        Connection.Open();

        using (SqlCommand Command = new SqlCommand())
        {
            Command.Connection = Connection;
            Command.CommandText = Procedure;
            Command.CommandType = CommandType.StoredProcedure;

            if (Params != null)
            {
                foreach (SqlParameter p in Params)
                    {
                        Command.Parameters.Add(p);
                    }
            }
            else
            {
                return new StoredProcRet(-1, null);
            }

            SqlParameter param = Command.Parameters.Add("@RETURN_VALUE", SqlDbType.Int);
            param.Direction = ParameterDirection.ReturnValue;

            Results = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = Command;
            adapter.Fill(Results);

            int FINAL_VALUE;
            int.TryParse(Convert.ToString(Command.Parameters["@RETURN_VALUE"].Value), out FINAL_VALUE);
            RETURN_VALUE = FINAL_VALUE;
        }
    }
    catch (SqlException ex)
    {
        Console.Error.WriteLine(ex.Message);
        return new StoredProcRet(-1, null);
    }
    finally
    {
        Connection.Close();
    }

    return new StoredProcRet(RETURN_VALUE, Results);
}

我正在处理的当前错误(完整堆栈跟踪和消息):

   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolea
n breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception
, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObj
ect stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand
 cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,
TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, Run
Behavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean for
DescribeParameterEncryption)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBe
havior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 time
out, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean
describeParameterEncryptionRequest)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehav
ior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletio
nSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asy
ncWrite, Boolean inRetry)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehav
ior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, S
tring method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behav
ior)
   at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandB
ehavior behavior)
   at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[]
 datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand co
mmand, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord,
Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)

   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
   at XiasporaServer.SQLDatabase.StoredProc(String Procedure, SqlParameter[] Par
ams)
Procedure or Function 'UpdatePlayer' expects parameter '@LoginID', which was not
 supplied.
UpdatePlayer function:
public void UpdatePlayer(Player p)
{

    var sLoginID = new SqlParameter("@LoginID", SqlDbType.NVarChar);
    var sPlayerName = new SqlParameter("@PlayerName", SqlDbType.NVarChar);
    var sPassword = new SqlParameter("@Password", SqlDbType.NVarChar);
    var sEmail = new SqlParameter("@Email", SqlDbType.NVarChar);
    var sIP = new SqlParameter("@IP", SqlDbType.NVarChar);

    var sHomePlanet = new SqlParameter("@HomePlanet", SqlDbType.Int);
    var sCurrentPlanet = new SqlParameter("@CurrentPlanet", SqlDbType.Int);
    var sMoney = new SqlParameter("@Money", SqlDbType.BigInt);
    var sExperiencePoint = new SqlParameter("@ExperiencePoint", SqlDbType.Int);
    var sCurrentShip = new SqlParameter("@CurrentShip", SqlDbType.Int);

    var sBluePrint1 = new SqlParameter("@BluePrint1", SqlDbType.TinyInt);
    var sBluePrint2 = new SqlParameter("@BluePrint2", SqlDbType.TinyInt);
    var sBluePrint3 = new SqlParameter("@BluePrint3", SqlDbType.TinyInt);
    var sBluePrint4 = new SqlParameter("@BluePrint4", SqlDbType.TinyInt);
    var sBluePrint5 = new SqlParameter("@BluePrint5", SqlDbType.TinyInt);
    var sBluePrint6 = new SqlParameter("@BluePrint6", SqlDbType.TinyInt);
    var sBluePrint7 = new SqlParameter("@BluePrint7", SqlDbType.TinyInt);
    var sBluePrint8 = new SqlParameter("@BluePrint8", SqlDbType.TinyInt);

    var sShip1 = new SqlParameter("@Ship1", SqlDbType.NVarChar);
    var sShip2 = new SqlParameter("@Ship2", SqlDbType.NVarChar);
    var sShip3 = new SqlParameter("@Ship3", SqlDbType.NVarChar);
    var sShip4 = new SqlParameter("@Ship4", SqlDbType.NVarChar);
    var sShip5 = new SqlParameter("@Ship5", SqlDbType.NVarChar);

    var sShipMisc1 = new SqlParameter("@ShipMisc1", SqlDbType.NVarChar);
    var sShipMisc2 = new SqlParameter("@ShipMisc2", SqlDbType.NVarChar);
    var sShipMisc3 = new SqlParameter("@ShipMisc3", SqlDbType.NVarChar);
    var sShipMisc4 = new SqlParameter("@ShipMisc4", SqlDbType.NVarChar);
    var sShipMisc5 = new SqlParameter("@ShipMisc5", SqlDbType.NVarChar);

    var sGuild = new SqlParameter("@Guild", SqlDbType.NVarChar);
    var sGuildRank = new SqlParameter("@GuildRank", SqlDbType.Int);
    var sGuildCredit = new SqlParameter("@GuildCredit", SqlDbType.BigInt);

    var sGenKill = new SqlParameter("@GenKill", SqlDbType.Int);
    var sVelicKill = new SqlParameter("@VelicKill", SqlDbType.Int);
    var sGenVelicAlignment = new SqlParameter("@GenVelicAlignment", SqlDbType.Int);
    var sBonusDamage = new SqlParameter("@BonusDamage", SqlDbType.Int);
    var sUserRef = new SqlParameter("@UserRef", SqlDbType.Int);

    sLoginID.Value = p.LoginID;
    sPlayerName.Value = p.PlayerName;
    sPassword.Value = p.Password;
    sEmail.Value = p.Email;
    sIP.Value = p.IP;

    sHomePlanet.Value = p.HomePlanet;
    sCurrentPlanet.Value = p.CurrentPlanet;
    sMoney.Value = p.Money;
    sExperiencePoint.Value = p.ExperiencePoint;
    sCurrentShip.Value = p.CurrentShip;

    sBluePrint1.Value = p.BluePrint1;
    sBluePrint2.Value = p.BluePrint2;
    sBluePrint3.Value = p.BluePrint3;
    sBluePrint4.Value = p.BluePrint4;
    sBluePrint5.Value = p.BluePrint5;
    sBluePrint6.Value = p.BluePrint6;
    sBluePrint7.Value = p.BluePrint7;
    sBluePrint8.Value = p.BluePrint8;

    sShip1.Value = p.Ship1;
    sShip2.Value = p.Ship2;
    sShip3.Value = p.Ship3;
    sShip4.Value = p.Ship4;
    sShip5.Value = p.Ship5;

    sShipMisc1.Value = p.ShipMisc1;
    sShipMisc2.Value = p.ShipMisc2;
    sShipMisc3.Value = p.ShipMisc3;
    sShipMisc4.Value = p.ShipMisc4;
    sShipMisc5.Value = p.ShipMisc5;

    sGuild.Value = p.Guild;
    sGuildRank.Value = p.GuildRank;
    sGuildCredit.Value = p.GuildCredit;

    sGenKill.Value = p.GenKill;
    sVelicKill.Value = p.VelicKill;
    sGenVelicAlignment.Value = p.GenVelicAlignment;
    sBonusDamage.Value = p.BonusDamage;
    sUserRef.Value = p.UserRef;

            StoredProc("UpdatePlayer",
                sLoginID, sPlayerName, sPassword, sEmail, sIP, sHomePlanet,
                sCurrentPlanet, sMoney, sExperiencePoint, sCurrentShip,
                sBluePrint1, sBluePrint2, sBluePrint3, sBluePrint4, sBluePrint5, sBluePrint6, sBluePrint7, sBluePrint8,
                sShip1, sShip2, sShip3, sShip4, sShip5,
                sShipMisc1, sShipMisc2, sShipMisc3, sShipMisc4, sShipMisc5,
                sGuild, sGuildRank, sGuildCredit, sGenKill, sVelicKill, sGenVelicAlignment, sBonusDamage, sUserRef);
}
答案

我宁愿看到你在try catch finally关闭连接的finally

我宁愿在using块中看到命令。

try
{
    Connection.Open();
    using (Command cmd = Connection.CreateCommand())
    {
    }
}
catch (SqlException ex)
{
    Console.Error.WriteLine(ex.Message);
    return new StoredProcRet(-1, null);
}
finally 
{
    Connection.Close();
}

至于错误的原因,我不确定。

另一答案

我怀疑错误可能在以下代码中:

   try
   {
        Results = new DataSet();
        var adapter = new SqlDataAdapter {SelectCommand = Command};
        adapter.Fill(Results);
        Connection.Close();

        RETURN_VALUE = Convert.ToInt32(Command.Parameters["@RETURN_VALUE"].Value);
    }
    catch (SqlException ex)
    {
            Console.Error.WriteLine(ex.StackTrace);
    }

为什么?

  1. 如果SqlExceptionConnection.Close()之前发生,程序将无法使用catch并且连接将保持打开状态,因此一段时间后您将遇到连接池问题
  2. 你只捕捉SqlException,如果在解析Convert.ToInt32时发生错误,那么SqlException不会被抛出,但可能会抛出InvalidCastOverflowException。在这种情况下,您的程序将以未捕获的异常终止

要解决什么?

  1. 使用try ... catch ... finally确保在finally块中始终关闭连接
  2. 使用Int32.TryParse而不是Convert.ToInt32

以上是关于带有SQL Server数据库错误的C#Game Server的主要内容,如果未能解决你的问题,请参考以下文章

带有实体框架和 SQL Server 数据库的 ASP.NET MVC - 图像未显示在视图中......错误显示“无法将“字节”转换为“字符串”

SQL INSERT,参数为查询

如何使用json从sql server ASP.net中检索数据

带有 SQL Server 后端更新的 MS Access 失败且没有错误

带有 JSP 的 JDBC 连接字符串 SQL Server 导致“非法转义字符”错误

SQL Server标量函数将数字转换为带有序数的单词