C# 在C上运行SQL查询时出错-OLEDBEException未经处理,在SQL语句结束后找到字符
每当我在C上运行下面的事件时,我都会收到以下错误消息-OLEDBEException未处理,在int affectedRows=intcommand.ExecuteOnQuery的SQL语句结束后找到字符;线你知道我怎么修吗C# 在C上运行SQL查询时出错-OLEDBEException未经处理,在SQL语句结束后找到字符,c#,sql,C#,Sql,每当我在C上运行下面的事件时,我都会收到以下错误消息-OLEDBEException未处理,在int affectedRows=intcommand.ExecuteOnQuery的SQL语句结束后找到字符;线你知道我怎么修吗 private void save_btn_Click(object sender, EventArgs e) { if (pgpText.Text.Trim().Length == 0) { MessageBox.Show("Please
private void save_btn_Click(object sender, EventArgs e)
{
if (pgpText.Text.Trim().Length == 0)
{
MessageBox.Show("Please fill the following textbox: PGP");
}
else if (teamText.Text.Trim().Length == 0)
{
MessageBox.Show("Please fill the following textbox: Team");
}
else
{
using (OleDbConnection conn = new OleDbConnection())
{
string pgp = pgpText.Text;
string team = teamText.Text;
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='db.mdb'";
OleDbCommand command = new OleDbCommand();
command.Connection = conn;
command.CommandText = "UPDATE PGP SET PGP=pgp,Team=team WHERE pgp=pgp; SELECT @@ROWCOUNT;";
conn.Open();
int affectedRows = (int)command.ExecuteNonQuery();
if (affectedRows == 0)
{
command.CommandText = "INSERT INTO PGP (PGP,Team) VALUES (pgp,team)";
command.ExecuteNonQuery();
}
}
}
}
我怀疑您实际上是在尝试使用参数——请注意,C中的pgp和团队变量在代码中根本没有使用。我猜你想要的是:
using (OleDbConnection conn = new OleDbConnection())
{
string pgp = pgpText.Text;
string team = teamText.Text;
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='db.mdb'";
OleDbCommand command = new OleDbCommand();
command.Connection = conn;
command.CommandText = "UPDATE PGP SET Team=? WHERE PGP=?";
command.Parameters.Add("team", OleDbType.VarChar).Value = team;
command.Parameters.Add("pgp", OleDbType.VarChar).Value = pgp;
conn.Open();
int affectedRows = (int) command.ExecuteNonQuery();
if (affectedRows == 0)
{
command.CommandText = "INSERT INTO PGP (Team, PGP) VALUES (?, ?)";
// Parameters as before
command.ExecuteNonQuery();
}
}
请注意,我已经从更新中删除了SELECT@@ROWCOUNT部分-这是不需要的,因为ExecuteOnQuery会返回受影响的行数
还有几点需要注意:
对于大多数数据库提供程序,您将使用命名参数而不是位置参数,例如值@pgp、@team,然后使用参数名称。。。但是.NET中的OLE DB提供程序不支持这些。
不要使用SQL的字符串连接作为另一个答案,在阅读本文时可能会被删除,这为数据和转换问题铺平了道路。而且很乱。
我怀疑您实际上是在尝试使用参数——请注意,C中的pgp和团队变量在代码中根本没有使用。我猜你想要的是:
using (OleDbConnection conn = new OleDbConnection())
{
string pgp = pgpText.Text;
string team = teamText.Text;
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='db.mdb'";
OleDbCommand command = new OleDbCommand();
command.Connection = conn;
command.CommandText = "UPDATE PGP SET Team=? WHERE PGP=?";
command.Parameters.Add("team", OleDbType.VarChar).Value = team;
command.Parameters.Add("pgp", OleDbType.VarChar).Value = pgp;
conn.Open();
int affectedRows = (int) command.ExecuteNonQuery();
if (affectedRows == 0)
{
command.CommandText = "INSERT INTO PGP (Team, PGP) VALUES (?, ?)";
// Parameters as before
command.ExecuteNonQuery();
}
}
请注意,我已经从更新中删除了SELECT@@ROWCOUNT部分-这是不需要的,因为ExecuteOnQuery会返回受影响的行数
还有几点需要注意:
对于大多数数据库提供程序,您将使用命名参数而不是位置参数,例如值@pgp、@team,然后使用参数名称。。。但是.NET中的OLE DB提供程序不支持这些。
不要使用SQL的字符串连接作为另一个答案,在阅读本文时可能会被删除,这为数据和转换问题铺平了道路。而且很乱。
您实际希望插入哪些值?为什么要使用更新命令将两个字段更新为相同的值?您是否确实尝试使用参数?您实际希望插入哪些值?为什么要使用更新命令将两个字段更新为相同的值?你们真的在尝试使用参数吗?嗨,乔恩,请看更新的问题。我试图做的是1检测具有指定PGP的特定行是否存在2如果存在,则使用新PGP/团队指定的内容进行更新3如果不存在,则创建新行。现在,只要pgp发生更改,就会创建一个新行。@Antoine LaurentLavoisier:我给出的代码应该可以做到这一点。。。虽然您已经更新了问题以删除ID字段,并且在您已经指定了PGP字段的值时更新PGP字段会很奇怪…但我已经更新了代码,以反映您仅使用PGP值作为标识符来更改团队值。您好,Jon-请参阅更新的问题。我试图做的是1检测具有指定PGP的特定行是否存在2如果存在,则使用新PGP/团队指定的内容进行更新3如果不存在,则创建新行。现在,只要pgp发生更改,就会创建一个新行。@Antoine LaurentLavoisier:我给出的代码应该可以做到这一点。。。虽然您已经更新了问题以删除ID字段,并且在您已经指定了PGP字段的值时更新PGP字段会很奇怪……但我已经更新了代码,以反映您仅使用PGP值作为标识符来更改团队值。