gpt4 book ai didi

c# - MySql 和插入最后一个 ID 问题仍然存在

转载 作者:行者123 更新时间:2023-11-29 04:57:13 25 4
gpt4 key购买 nike

好的,经过大量阅读和尝试后,我似乎仍然无法让它工作:

      OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Email) VALUES ('rusty@msn.com'); SELECT LAST_INSERT_ID();", cn);

cmd.ExecuteNonQuery();
using (OdbcDataReader reader = cmd.ExecuteReader())
{

string theUserId = String.Format("{0}", reader.GetString(0));
Label10.Text = theUserId;

表格:

User
--------
UserID (auto increment, pk)
Email

在 Debug模式下运行我在这一行得到错误,

            using (OdbcDataReader reader = cmd.ExecuteReader())

和,

cmd.ExecuteNonQuery();

Mysql 在 SELECT LAST_INSERT_ID();", cn) 这行说它是一个语法错误,但从我读到的内容来看这是合法的。

确切错误:

ERROR [42000] [MySQL][ODBC 3.51 Driver][mysqld-5.5.9]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 'SELECT LAST_INSERT_ID()' at line 1

编辑:贾斯汀方法:

using (OdbcConnection connection = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;"))
{
// ODBC command and transaction objects
OdbcCommand command = new OdbcCommand();
OdbcTransaction transaction = null;

// tell the command to use our connection
command.Connection = connection;

try
{
// open the connection
connection.Open();

// start the transaction
transaction = connection.BeginTransaction();

// Assign transaction object for a pending local transaction.
command.Connection = connection;
command.Transaction = transaction;

// TODO: Build a SQL INSERT statement
OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Email, FirstName, SecondName, DOB, Location, Aboutme, username, password) VALUES ('" + TextBox1.Text + "', '" + TextBox2.Text + "', '" + TextBox3.Text + "', '" + TextBox4.Text + "', '" + TextBox5.Text + "', '" + TextBox6.Text + "', '" + TextBox7.Text + "', '" + TextBox8.Text + "')", connection);

// run the insert using a non query call
command.CommandText = cmd.ToString();
command.ExecuteNonQuery();

/* now we want to make a second call to MYSQL to get the new index
value it created for the primary key. This is called using scalar so it will
return the value of the SQL statement. We convert that to an int for later use.*/
command.CommandText = "select last_insert_id();";
id = Convert.ToInt32(command.ExecuteScalar());

// the name id doesnt not exist in the current context

// Commit the transaction.
transaction.Commit();
}
catch (Exception ex)
{
Label10.Text = ": " + ex.Message;

try
{
// Attempt to roll back the transaction.
transaction.Rollback();
}
catch
{
// Do nothing here; transaction is not active.
}
}
}

最佳答案

澄清一下,mySQL .net ODBC 驱动程序不允许像您描述的那样运行多个命令。您必须进行两个单独的调用并将它们包装在一个事务中。

// open a new connection using a default connection string I have defined elsewhere
using( OdbcConnection connection = new OdbcConnection( s_connectionString ) )
{
// ODBC command and transaction objects
OdbcCommand command = new OdbcCommand();
OdbcTransaction transaction = null;

// tell the command to use our connection
command.Connection = connection;

try
{
// open the connection
connection.Open();

// start the transaction
transaction = connection.BeginTransaction();

// Assign transaction object for a pending local transaction.
command.Connection = connection;
command.Transaction = transaction;

// TODO: Build a SQL INSERT statement
StringBuilder SQL = new StringBuilder();

// run the insert using a non query call
command.CommandText = SQL.ToString();
command.ExecuteNonQuery();

/* now we want to make a second call to MYSQL to get the new index
value it created for the primary key. This is called using scalar so it will
return the value of the SQL statement. We convert that to an int for later use.*/
command.CommandText = "select last_insert_id();";
id = Convert.ToInt32( command.ExecuteScalar() );

// Commit the transaction.
transaction.Commit();
}
catch( Exception ex )
{
Debug.WriteLine( ex.Message );

try
{
// Attempt to roll back the transaction.
transaction.Rollback();
}
catch
{
// Do nothing here; transaction is not active.
}
}
}

关于c# - MySql 和插入最后一个 ID 问题仍然存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5542999/

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