我目前正在尝试使用我拥有的数据库填充数据集。以下代码似乎根本没有填充数据集:
private static DataSet records = new DataSet();
public static DataSet LoanersData()
{
//query string **add more tables as needed**
string querystring = "SELECT * FROM LoanerItems; SELECT * FROM Customers; SELECT * FROM Jobs";
//sql connection
SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
scsb.DataSource = "LLOYD2\\";
scsb.InitialCatalog = "LoanersTest";
scsb.IntegratedSecurity = true;
scsb.ConnectTimeout = 30;
SqlConnection loanersConnection = new SqlConnection(scsb.ConnectionString);
SqlDataAdapter adapter = new SqlDataAdapter(querystring, loanersConnection);
adapter.TableMappings.Add("Table", "LoanerItems");
adapter.TableMappings.Add("Table1", "Customers");
adapter.TableMappings.Add("Table2", "Jobs");
adapter.Fill(records);
return records;
}
和这个:
{
InitializeComponent();
records = DataRetreive.LoanersDataSet.LoanersData();
LoanerItemsTable = records.Tables["LoanerItems"];
CustomersTable = records.Tables["Customers"];
LoanerItemsTable = records.Tables["Jobs"];
}
我无法确定数据集是否已填充,但在尝试像这样查询其中一个数据表时:
IEnumerable<DataRow> query = from LoanerItems in LoanerItemsTable.AsEnumerable() select LoanerItems;
foreach (DataRow p in query)
{
loanerComboBox1.Items.Add(p.Field<string>("BCPartnNumber"));
}
我没有收到任何错误,但没有任何内容添加到组合框中。在调试期间,我注意到它无法循环遍历数据行,因为数据表中没有任何内容。
我正在尝试做的事情和我正在做的事情可能是两件不同的事情。我想至少澄清一下,在表中“BCPartnumber 是列的名称。如果需要,我可以提供更多代码,但目前没有太多其他可提供的。