gpt4 book ai didi

c# - 错误在我的 SQL 语句中显示 "Missing right parenthesis"...但它不是!帮助?

转载 作者:行者123 更新时间:2023-11-30 14:16:49 26 4
gpt4 key购买 nike

这是我的 C# 程序中的语句:

(由 gbn 编辑,为清楚起见格式化所以不是全部在一行)

DbCommand.CommandText =
@"SELECT
HIST.MBRSEP, HIST.LOCATION, HIST.BILLTYPE, HIST.BILLMOYR, LOCINFO.CYCLE,
LOCINFO.DIST, LOCINFO.LOCATION
FROM
(CAV_MBRHISTDEL AS HIST INNER JOIN CAV_LOCINFODETL AS LOCINFO ON HIST.LOCATION = LOCINFO.LOCATION)
WHERE
LOCINFO.CYCLE = @CYCLE AND
LOCINFO.DIST = @DISTRICT AND
HIST.BILLTYPE = '09' AND
HIST.BILLMOYR <> '9999'";

这是错误信息:

ERROR [HY000] [Oracle][ODBC][Ora]ORA-00907: missing right parenthesis

我的 SQL 语句中只有 2 个括号,一个右括号,一个左括号。我不确定错误告诉我什么。有什么建议吗?

编辑:这是参数的定义方式:

        string cycle = cbCycle.Text;
string district = cbDistrict.Text.Substring(0,2);

这是我将它们添加到 DbCommand 的地方:

        DbCommand.Parameters.AddWithValue("@CYCLE", cycle);
DbCommand.Parameters.AddWithValue("@DISTRICT", district);

这是我的完整代码,当有人点击我表单上的“开始”按钮时触发:

private void btnGo_Click(object sender, EventArgs e)
{
//get parameters
string cycle = cbCycle.Text;
string district = cbDistrict.Text.Substring(0,2);

//create a connection to the database
OdbcConnection DbConnection = new OdbcConnection("DSN=UPN2;uid=xxx;pwd=xxxx");
DbConnection.Open();

//create a command to extract the required data and
//assign it to the connection string
OdbcCommand DbCommand = DbConnection.CreateCommand();
DbCommand.CommandText =
@"SELECT HIST.MBRSEP, HIST.LOCATION, HIST.BILLTYPE, HIST.BILLMOYR, LOCINFO.CYCLE, LOCINFO.DIST, LOCINFO.LOCATION FROM CAV_MBRHISTDEL AS HIST INNER JOIN CAV_LOCINFODETL AS LOCINFO ON HIST.LOCATION = LOCINFO.LOCATION WHERE LOCINFO.CYCLE = @CYCLE AND LOCINFO.DIST = @DISTRICT AND HIST.BILLTYPE = '09' AND HIST.BILLMOYR <> '9999'; ";

DbCommand.Parameters.AddWithValue("@CYCLE", cycle);
DbCommand.Parameters.AddWithValue("@DISTRICT", district);

//Create a DataAdapter to run the command and fill the datatable
OdbcDataAdapter da = new OdbcDataAdapter();
da.SelectCommand = DbCommand;
DataTable dt = new DataTable();
da.Fill(dt);


tbOutput.Text = PrintDataTable(dt);

DbCommand.Dispose();
DbConnection.Close();

}

最佳答案

问题可能是您使用 oracle 保留字作为列名和参数名 - 即 CYCLE...这样做可能会导致数据库出现奇怪且不稳定的行为!

参见 http://download.oracle.com/docs/cd/B19306_01/em.102/b40103/app_oracle_reserved_words.htm

根据您的数据库提供商,您可能希望使用 : 而不是 @ 作为参数。

关于c# - 错误在我的 SQL 语句中显示 "Missing right parenthesis"...但它不是!帮助?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6893536/

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