C# SqlDataAdapter.Fill(DataTable) 不填充 DataTable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16985502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SqlDataAdapter.Fill(DataTable) not filling DataTable
提问by DanteTheEgregore
I'm currently trying to retrieve records from a database in a WPF application written in C#. I'm using the SQL data reader and it appears to connect fine but the following is giving me a NullReferenceException:
我目前正在尝试从用 C# 编写的 WPF 应用程序中的数据库检索记录。我正在使用 SQL 数据读取器,它似乎连接正常,但以下内容给了我一个 NullReferenceException:
if (isString == true)
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT PartNumber FROM LoanerItems", loanersConnection);
DataTable datatable = new DataTable();
adapter.Fill(datatable);
for (int i = 0; i < datatable.Rows.Count - 1; i ++)
{
passBackRecords[i] = datatable.Rows[i].ToString();
}
}
Again it would appear that I'm connecting to the database but if I run through the debugger the datatable doesn't appear to be filled.
再次看起来我正在连接到数据库,但如果我通过调试器运行数据表似乎没有被填充。
My connection string in case it might help:
我的连接字符串,以防万一它可能有帮助:
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
scsb.DataSource = "LLOYD2\";
scsb.InitialCatalog = "LoanersTest";
scsb.IntegratedSecurity = true;
scsb.ConnectTimeout = 30;
I'm currently using a test database with two records in it instead of the finalized database. I've tried using the Transact-SQL Editor and the records appear just fine.
我目前正在使用一个包含两条记录的测试数据库,而不是最终的数据库。我试过使用 Transact-SQL 编辑器,并且记录显示正常。
*EDIT:
*编辑:
After intializing passBackRecords the following started throwing an ArguementOutOfRange exception:
初始化 passBackRecords 后,以下开始抛出 ArguementOutOfRange 异常:
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM LoanerItems", loanersConnection);
DataTable datatable = new DataTable();
adapter.Fill(datatable);
for (int i = 0; i < datatable.Rows.Count - 1; i++)
{
passBackRecords[i] = datatable.Rows[i]["BCPartNumber"].toString;
}
I've tried both:
我都试过:
for (int i = 0; i < datatable.Rows.Count - 1; i++)
and:
和:
for (int i = 1; i < datatable.Rows.Count; i++)
But neither seems to work.
但两者似乎都不起作用。
datatable.Rows.Count;
shows that there are two rows in the datatable.
显示数据表中有两行。
采纳答案by Mike Perrenoud
Even if zero rows were returned, the DataTablewouldn't be null. However, there is one place in this line:
即使返回零行,DataTable也不会是null. 但是,这一行有一个地方:
passBackRecords[i] = datatable.Rows[i].ToString();
that will fail -when passBackRecordsis null. Another possible location is this:
那会失败 -when passBackRecordsis null。另一个可能的位置是这样的:
.Rows[i].ToString();
if the value in the row is nullyou'd also get a NullReferenceExceptionwhen calling ToString(). But I'd almostput money on the fact that you've not yet initialized passBackRecords.
如果行中的值null,你也就会得到一个NullReferenceException呼叫时ToString()。但我几乎要花钱买你还没有初始化的事实passBackRecords。

