gpt4 book ai didi

c# - 执行 OleDbCommand 时出错。 "Must declare the scalar variable "@MaxID“。”

转载 作者:行者123 更新时间:2023-11-30 18:05:06 27 4
gpt4 key购买 nike

private void AddValue(string strValue)

{

//get the maximum id for Lists first

int MaxID = DataOperations.ReturnMaxIDInATable("Lists", connString);
int iSqlStatus = 0;
string query = "INSERT INTO Lists(ID, ListName, ListValue)
VALUES(@MaxID, @ListName, @ListValue)";

MaxID++;

OleDbConnection dbConn = new OleDbConnection(connString);
OleDbCommand dbComm = new OleDbCommand();

dbComm.Parameters.Clear();
try
{
dbComm.CommandText = query;
dbComm.CommandType = CommandType.Text;

OleDbParameter IDParam = new OleDbParameter();
IDParam.ParameterName = "@MaxID";
IDParam.OleDbType = OleDbType.BigInt;
IDParam.Value = MaxID;
dbComm.Parameters.Add(IDParam);

dbComm.Parameters.AddWithValue("@ListName", ListName);
dbComm.Parameters.AddWithValue("@ListValue", strValue);
dbComm.Connection = dbConn;
DataAccess.HandleConnection(dbConn);

iSqlStatus = Convert.ToInt16(dbComm.ExecuteNonQuery());

//Now check the status
if (iSqlStatus != 0)
{
//DO your failed messaging here
//return false;
}
else
{
//Do your success work here
//dbComm.
//return true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error inserting value in "
+ ListName + ","
+ strValue);
//return false;
}
finally
{
DataAccess.HandleConnection(dbConn);
}

}

最佳答案

我相信您在通过OleDbCommand 执行SQL 时需要对参数使用问号(而SqlCommand 使用@)。示例:

INSERT INTO Lists (ID, ListName, ListValue) VALUES (?, ?, ?)

您只需按照参数在 SQL 中出现的顺序添加参数即可。

关于c# - 执行 OleDbCommand 时出错。 "Must declare the scalar variable "@MaxID“。”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5870146/

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