ExecuteScalar多个事务

时间:2014-02-24 11:07:29

标签: c# executescalar

我正在尝试使用2条指令执行ExecuteScalar(),并在查询中使用ODBC连接到mysql。我的疑问是:

string query = @"SET @MY_ID := (SELECT IFNULL(MAX(id) + 1, 1) FROM sc);" + Environment.NewLine + "INSERT INTO sc (id, name) values (@MY_ID, 'TESTE');"

我得到了:

{System.Data.Odbc.OdbcException (0x80131937): ERROR [42000] [MySQL][ODBC 5.1 Driver] [mysqld-5.0.83-community-nt]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO

2 个答案:

答案 0 :(得分:0)

您无法使用SET初始化结果集中的变量,您需要实际执行查询

 SELECT @MY_ID := ...

答案 1 :(得分:0)

试试这个:

string query = "Declare @MY_ID int;Select @MY_ID  = IFNULL(MAX(id) + 1, 1) from sc;INSERT INTO sc (id, name) values (@MY_ID, 'TESTE');"
OleDBCommand command=new OleDbCommand(query,connectionObj);
int status;

status=command.ExecuteNonQuery();

if(status>0)
{
//success
}