我在处理从MySQL插入到我的C#应用程序的LAST_INSERT_ID()时遇到问题。我不知道自己做错了什么,所以任何帮助都会受到赞赏。
这是一个winforms应用程序。我想要的只是在save方法执行完毕后插入的记录的TicketID
...
这是我的MySQL查询:
CREATE DEFINER=`COMP`@`%` PROCEDURE `usp_SaveTicket`(IN AssignedToUserID INT, IN LoggedByUserID INT,
IN LoggedByUserEmailAddress VARCHAR(200), IN LoggedByUserFullname VARCHAR(200), IN DepartmentID INT,
IN QuickCode VARCHAR(255), IN `Subject` VARCHAR(500), IN `Status` VARCHAR(100), IN Priority INT,
IN CreateDate DATE, IN ModifyDate DATE, IN AutoCloseSent INT, IN FirstMessageID INT, IN LastMessageID INT,
IN Info VARCHAR(500), IN NumberOfReplies INT, OUT TicketID INT)
BEGIN
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
INSERT INTO table
(
staff, customer_id, customer_email, customer_fullname, departmentid, quickcode
,`subject`, `status`, priority, created, updated, auto_close_sent, first_msg_id
,last_msg_id, info, replies
)
VALUES
(
AssignedToUserID, LoggedByUserID, LoggedByUserEmailAddress, LoggedByUserFullname, DepartmentID, QuickCode,
`Subject`, `Status`, Priority, CreateDate, ModifyDate, AutoCloseSent, FirstMessageID,
LastMessageID, Info, NumberOfReplies
);
SET TicketID = LAST_INSERT_ID();
COMMIT;
END
这是我的C#方法:
public async Task SaveTicket(bool connectToLive, string commandText, CommandType commandType, int assignedToUserID,
int loggedByUserID, string loggedByUserEmailAddress, string loggedByUserFullname, int departmentID, string quickCode, string subject, string status, int priority,
DateTime createDate, DateTime modifyDate, int autoCloseSent, int firstMessageID, int lastMessageID, string info, int numberOfReplies, [Optional] int ticketID)
{
using (MySqlCommand command = new MySqlCommand())
{
try
{
command.CommandType = commandType;
command.Parameters.AddWithValue("AssignedToUserID", assignedToUserID);
command.Parameters.AddWithValue("LoggedByUserID", loggedByUserID);
command.Parameters.AddWithValue("LoggedByUserEmailAddress", loggedByUserEmailAddress);
command.Parameters.AddWithValue("LoggedByUserFullname", loggedByUserFullname);
command.Parameters.AddWithValue("QuickCode", quickCode);
command.Parameters.AddWithValue("DepartmentID", departmentID);
command.Parameters.AddWithValue("Subject", subject);
command.Parameters.AddWithValue("Status", status);
command.Parameters.AddWithValue("Priority", priority);
command.Parameters.AddWithValue("CreateDate", createDate);
command.Parameters.AddWithValue("AutoCloseSent", autoCloseSent);
command.Parameters.AddWithValue("ModifyDate", modifyDate);
command.Parameters.AddWithValue("FirstMessageID", firstMessageID);
command.Parameters.AddWithValue("LastMessageID", lastMessageID);
command.Parameters.AddWithValue("Info", info);
command.Parameters.AddWithValue("NumberOfReplies", numberOfReplies);
command.Parameters.AddWithValue("TicketID", ticketID);
command.Parameters["TicketID"].Direction = ParameterDirection.Output;
switch (connectToLive)
{
case true:
command.CommandText = commandText;
command.Connection = connection;
if (connection.State != ConnectionState.Closed)
connection.Close();
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
break;
case false:
command.CommandText = commandText;
command.Connection = devConnection;
if (devConnection.State != ConnectionState.Closed)
devConnection.Close();
if (devConnection.State != ConnectionState.Open)
{
devConnection.Open();
}
break;
}
await command.ExecuteNonQueryAsync();
//TEST 1
//command.CommandText = "SELECT LAST_INSERT_ID() FROM table";
command.CommandText = "SELECT LAST_INSERT_ID()";
IDataReader reader = command.ExecuteReader();
long id = 0;
if (reader != null && reader.Read())
id = reader.GetInt64(0);
//TEST2
//long ticketID = command.LastInsertedId;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message, "An Error Has Occured", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
finally
{
if (devConnection.State != ConnectionState.Closed)
devConnection.Close();
if (connection.State != ConnectionState.Closed)
connection.Close();
}
}
}
这是我对Save方法的调用:
logic.SaveTicket(false, "usp_SaveTicket", CommandType.StoredProcedure, 0, GlobalVariables.complaintLoggedByID, GlobalVariables.emailAddress, GlobalVariables.complaintLoggedByValue,
departmentID, quickCode, txtComplaintSubject.Text, "open", GlobalVariables.priorityID, DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")),
DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")), 0, 0, 0, "", 0);
我的TEST1
和TEST2
看到了(即使他们已经评论过了),但它并没有给我LAST_INSERT_ID()。
初始保存将包含零等。我需要TicketID来更新此记录以及在其他地方插入。我没有尝试过的任何东西似乎都在起作用:(