English 中文(简体)
54 DataAdapter。 Fill误差
原标题:SQLDataAdapter.Fill error

我在使用以下代码时正出现错误。 并非所有记录都是如此。 但是,在5 000项交易中,我至少有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)
问题回答

2. 数据输入物体的密码

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

以尝试/副渔获物加以校正,并记录例外的信息和内歇。

您的数据对一些行文可能有些“高度精确”。

数据校正员填写这些行文。 相反,它将放弃例外。 核对贵国代码中的“指标”数据类型。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签