insert INTO stu(name) VALUES('maimai'); set @ReturnValue=@@IDENTITY;
string sql="insert INTO stu(name) VALUES(@name);set @ReturnValue=@@IDENTITY;
MySqlParameter[] parameters = { new MySqlParameter("@name", MySqlDbType.VarChar,100),MySqlDbType.Int32,1), new MySqlParameter("@ReturnValue",MySqlDbType.Int32) };
public static int ExecuteSql(string SQLString, params MySqlParameter[] cmdParms) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); int rows = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return rows; } catch (MySql.Data.MySqlClient.MySqlException e) { throw e; } } } } private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, string cmdText, MySqlParameter[] cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = cmdText; if (trans != null) cmd.Transaction = trans; cmd.CommandType = CommandType.Text;//cmdType; if (cmdParms != null) { foreach (MySqlParameter parameter in cmdParms) { if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null)) { parameter.Value = DBNull.Value; } cmd.Parameters.Add(parameter); } } }
然后执行
ExecuteSql(sql,parameters);
想要返回 return 1(int)parameters[1].Value;
发现报错 : near NULL=@@IDENTITY;
问题:难道是mysql.data 把输出参数的null值也带入sql语句了?
最后呈现我们的是:insert INTO stu(name) VALUES('maimai');set NULL=@@IDENTITY;
根源还是没解决,有大神知道的麻烦解决下,下的mysql.data的源码 调试一直进不去,就没接着调试了...