简体   繁体   English

向数据表添加行时出现奇怪的错误

[英]Strange error when adding row to datatable

I'm hoping you guys can help me figure out why this is happening. 我希望你们能帮助我弄清楚为什么会这样。 I've been tearing my hair out trying to figure this out. 我一直在试图解决这个问题。

Here's an example directly from my code (with the boring bits cut out) 这是直接来自我的代码的示例(删去了一些无聊的内容)

...(Set up the connection and command, initialize a datatable "dataTable")...

using (SqlDataReader reader = cmd.ExecuteReader())
{
   //Query storage object
   object[] buffer = new object[reader.FieldCount];

   //Set the datatable schema to the schema from the query
   dataTable = reader.GetSchemaTable();


   //Read the query
   while (reader.Read())
   {  
    reader.GetValues(buffer);
    dataTable.Rows.Add(buffer);
   }
}                        

The error is 错误是

Input string was not in a correct format.Couldn't store in NumericScale Column. 输入的字符串格式不正确。无法存储在NumericScale Column中。 Expected type is Int16. 预期的类型是Int16。

The specific column data types as returned by the schema are (ordered by column) 模式返回的特定列数据类型为(按列排序)

  1. System.Data.SqlTypes.SqlInt32 System.Data.SqlTypes.SqlInt32
  2. System.Data.SqlTypes.SqlInt32 System.Data.SqlTypes.SqlInt32
  3. System.Data.SqlTypes.SqlByte System.Data.SqlTypes.SqlByte
  4. System.Data.SqlTypes.SqlMoney System.Data.SqlTypes.SqlMoney
  5. System.Data.SqlTypes.SqlString System.Data.SqlTypes.SqlString
  6. System.Data.SqlTypes.SqlGuid System.Data.SqlTypes.SqlGuid
  7. System.Data.SqlTypes.SqlDateTime System.Data.SqlTypes.SqlDateTime

It would appear that the data that should be in column #5 is actually appearing in column #3. 看起来应该在第5列中的数据实际上出现在第3列中。 But that is pure speculation. 但这纯粹是猜测。

What I know is that in order to use a dataTable "dynamically" with a query that can continue any number of different types of data the best route is to use GetSchemaTable() to retrieve it. 我所知道的是,为了在查询中“动态”使用dataTable,该查询可以继续许多不同类型的数据,最好的方法是使用GetSchemaTable()检索它。

What I Saw In The Debugger 我在调试器中看到的内容

When I dropped into the debugger I took a look at dataTable's types built from the schema vs. the types returned to the object from reader.GetValues(). 当我进入调试器时,我研究了从模式构建的dataTable的类型与从reader.GetValues()返回给对象的类型。 They are exactly the same . 他们是完全一样的

It seems like dataTable.Rows.Add(buffer) is adding the columns a few columns off from where it should be. 好像dataTable.Rows.Add(buffer)正在将列添加到应有的位置。 But this shouldn't be possible. 但这不可能。 Especially with the schema being directly built from the reader. 特别是直接从读者那里构建模式。 I've played with options such as "CommandBehavior.KeyInfo" within ExecuteReader() and still had the same error occur. 我在ExecuteReader()中使用了诸如“ CommandBehavior.KeyInfo”之类的选项,但仍然发生相同的错误。

Note: I need to run the query this way to enable the end-user to halt the query mid-read. 注意:我需要以这种方式运行查询,以使最终用户能够中止查询。 Please do not suggest I scrap this and use an SqlDataAdapter or DataTable.Load() solution. 请不要建议我取消此操作,并使用SqlDataAdapter或DataTable.Load()解决方案。

I'd really appreciate any help. 我真的很感谢您的帮助。 Thank you! 谢谢!

Method DbDataReader.GetSchemaTable() return metadata table containing column names and types. 方法DbDataReader.GetSchemaTable()返回包含列名称和类型的元数据表。 It is not an empty table that one could expect. 它不是一个空表。 For more details see MSDN 有关更多详细信息,请参见MSDN。

I'm sorry but GetSchemaTable retrieves schema for a given table and GetValues retrieves the actual row data . 抱歉, GetSchemaTable检索给定表的模式 ,而GetValues检索实际的行数据 Eg your dataTable will contain columns like column name, column type, etc. (see MSDN reference ) while your buffer will contain the actual data whose representation will differ from what the dataTable contains. 例如,您的dataTable将包含列名称,列类型等列(请参见MSDN参考 ),而您的buffer将包含实际数据,这些数据的表示形式将不同于dataTable包含的内容。

Why don't you just use: 您为什么不只使用:

dataTable.Fill();

Why are you manually loading it one row at a time? 为什么您一次要手动加载一行?

Please check what you are inserting into the column row which gas GUID data type. 请检查您要插入哪种气体GUID数据类型的列行中的内容。 I got the same error and found that while inserting records after reading records from csv file, there were couple of empty spaces in csv file. 我遇到了同样的错误,发现从csv文件读取记录后插入记录时,csv文件中有几个空白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM