gpt4 book ai didi

c# - CommandText 属性未初始化

转载 作者:行者123 更新时间:2023-11-30 20:48:24 24 4
gpt4 key购买 nike

只有在我将工作代码更改为使用数据 View 而不是文本框来显示单行数据之后,我才遇到这个问题。

我有以下内容:

static SqlConnection dbConnection = new SqlConnection
(DBConnection.DBConnection.connectionString);
SqlDataAdapter holdone = new SqlDataAdapter(getCommand, dbConnection);

DataSet holdall = new DataSet();
DataSet updateall = new DataSet();
DataTable invoiceTable = new DataTable();

DataView invoiceView = new DataView();

被使用

public void GetOne(/* connectionString, string invref, string tableref*/)
{

getCommand = "select *redacted* from " + tableref +
"where *redacted* = " + invref;

using (SqlConnection dbConnection = new SqlConnection
(DBConnection.DBConnection.connectionString))
{
dbConnection.Open();

holdone.Fill(holdall);

invoiceTable = holdall.Tables[0];

dbConnection.Close();


}

DataRowView rowView = invoiceView.AddNew();

rowView["*redacted*"] = invoiceTable;

rowView.EndEdit();
}

错误报告 holdone.fill(holdall) 作为违规行,但是我不确定为什么,因为我没有使用 SQLCommand 作为参数,而不是 SQLDataAdapter 的参数。

我正在努力寻找哪里出了问题?

最佳答案

问题是您确实SqlDataAdapter 的选择命令字符串设置为getCommand当前 值> 在这一行中:

SqlDataAdapter holdone = new SqlDataAdapter(getCommand, dbConnection); 

但是,由于字符串并不是真正的指针,因此更改 getCommand 后记将不会更改 SqlDataAdapter 的选择命令。

您需要做的是:

public void GetOne(/* connectionString, string invref, string tableref*/)
{

getCommand = "select *redacted* from " + tableref + "where *redacted* = " + invref;
using (SqlConnection dbConnection = new SqlConnection(DBConnection.DBConnection.connectionString))
{
dbConnection.Open();

holdone.SelectCommand = new SqlCommand(getCommand, dbConnection);
holdone.Fill(holdall);

invoiceTable = holdall.Tables[0];
//dbConnection.Close(); // This line is unnecessary, as the connection will be closed by `using`
}

DataRowView rowView = invoiceView.AddNew();
rowView["*redacted*"] = invoiceTable;
rowView.EndEdit();
}

关于c# - CommandText 属性未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24696632/

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