为什么方法不能使用数据集从数据库向我发送值?
错误示例
public string dane()
{
// Get the DataTable of a DataSet.
DataTable table = DataSet1.Tables["Products"];
DataRow[] rows = table.Select();
string s ="";
// Print the value one column of each DataRow.
for (int i = 0; i < rows.Length; i++)
{
s += rows[i]["ProductID"] + " ";
}
return s;
}
错误 - 非静态字段、方法或属性需要对象引用
无法找到数据。 (但错误已修复)
public string dane()
{
// Get the DataTable of a DataSet.
DataSet1 dataSet = new DataSet1();
DataTable table = dataSet.Tables["Products"];
DataRow[] rows = table.Select();
string s ="";
// Print the value one column of each DataRow.
for (int i = 0; i < rows.Length; i++)
{
s += rows[i]["ProductID"] + " ";
}
return s;
}
请您参考如下方法:
那是因为您的 DataSet1 类可能不是静态的,但我们不知道,因为您没有向我们展示。如果不先创建 DataSet1 对象,则不能引用 Tables。我想你在这里得到了错误:
DataTable table = DataSet1.Tables["Products"];
在您的第二个代码示例中,您实际上创建了一个解决错误的 DataSet 实例。
您可以保留您的修复或将 DataSet1 设为静态,但在这种情况下,这似乎不是一个好的解决方案。
