gpt4 book ai didi

c# - 非静态字段、方法或属性(数据集)需要对象引用

转载 作者:太空宇宙 更新时间:2023-11-03 21:48:06 24 4
gpt4 key购买 nike

为什么方法不能使用数据集从数据库向我发送值?

Here is example

错误示例

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;

}

enter image description here

最佳答案

那是因为您的 DataSet1可能不是静态的,但我们不知道,因为您没有向我们展示。如果不先创建 DataSet1 对象,则不能引用 Tables。我想你在这里得到了错误:

DataTable table = DataSet1.Tables["Products"];

在您的第二个代码示例中,您实际上创建了一个解决错误的 DataSet 实例。

您可以保留您的修复或将 DataSet1 设为静态,但在这种情况下,这似乎不是一个好的解决方案。

关于c# - 非静态字段、方法或属性(数据集)需要对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15882221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com