SQLDataAdapter.Fill错误

时间:2014-02-25 09:27:14

标签: c# sqlcommand

我在代码下面使用时遇到错误。所有记录都没有发生这种情况。但在5000次交易中,我至少有10次失误。我交叉检查数据库中的数据,数据是好的,但我不知道为什么我收到此错误。请求您帮我展示正确的代码实现路径。


Service Method:


public List GetStockPIN(string sInput)
{
    List listStockInfo = new List();

    try
    {

    DEBUG("Info from GetStockPIN : ", sInput);
    // Parse request string 
    Parseparams(sInput);
    string sTerminalId = GetValue("TerminalId");
    string sMerchantId = GetValue("MerchantId");
    string sBankCode= GetValue("BankCode");
    string sAppId = GetValue("AppId");
    string sProductName = GetValue("ProductName");
    string sDenomName = GetValue("DenomName");
    string sQTY = GetValue("QTY");

    StockRepository objStockRepository = new StockRepository(sConString) { DenomName = sDenomName, ProductName = sProductName, TerminalId = sTerminalId, Qty = Convert.ToInt32(sQTY) };
    listStockInfo = objStockRepository.GetStockPIN();
    return listStockInfo;
    }
    catch (Exception ex)
    {
    DEBUG("Error from GetStockPIN : ", ex);
    return listStockInfo;
    }
}



Database Access Method:


public List  GetStockPIN()
       {
           DataTable dtrec = new DataTable();
           List objStockInfo = new List();
           SqlConnection conn = null;
           try
           {
               using (conn = new SqlConnection(sConnectionString))
               {
                   using (SqlCommand cmd = conn.CreateCommand())
                   {


                       cmd.CommandType = CommandType.StoredProcedure;
                       cmd.CommandText = "prc_GetStockPIN";
                       cmd.Parameters.Add(new SqlParameter("@TerminalId", SqlDbType.NVarChar));
                       cmd.Parameters["@TerminalId"].Value = TerminalId;

                       cmd.Parameters.Add(new SqlParameter("@ProductName", SqlDbType.VarChar));
                       cmd.Parameters["@ProductName"].Value = ProductName;

                       cmd.Parameters.Add(new SqlParameter("@DenomName", SqlDbType.VarChar));
                       cmd.Parameters["@DenomName"].Value = DenomName;

                       cmd.Parameters.Add(new SqlParameter("@Qty", SqlDbType.Int));
                       cmd.Parameters["@Qty"].Value = Qty;

                       conn.Open();
                       using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                       {
                          da.Fill(dtrec);                          
                       }

               //Some additional code that generates List

                   }
               }
           }
           catch (Exception error)
           {
               throw error;
           }
           finally
           {
               if (conn != null)
               {
                   try
                   {
                       conn.Close();
                   }
                   catch (Exception ex)
                   {
                   }
               }
           }
           return objStockInfo;
       }
    }


以下是异常输出:

 DEBUG  : Error from GetStockPIN :  
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject 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, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
   at TAMSBulkPIN.DAL.StockRepository.GetStockPIN()
   at TAMSBulkPIN.BulkPINSVC.GetStockPIN(String sInput)

3 个答案:

答案 0 :(得分:0)

将连接名称传递给dataadapter对象

using (SqlDataAdapter da = new SqlDataAdapter(cmd,/* your connection object*/))
{
   try
   {
      da.Fill(dtrec);
   }
}

答案 1 :(得分:0)

使用try / catch包装它并记录异常消息和不足之处。

答案 2 :(得分:0)

对于某些行,您的数据可能具有“高精度”。

数据适配器不填充这些行。相反,它将引发异常。在代码中检查“十进制”数据类型。