我使用asp.net作爲前端,MySQL作爲後端。 我有兩個表「註冊」和「用戶」。 我有一個註冊表格,因爲我有註冊詳細信息以及登錄詳細信息。 文用戶點擊註冊將註冊詳細信息保存到「註冊」,登錄詳細信息用表註冊的外鍵Reg_Id保存到「用戶」。獲取最近添加的記錄的主鍵並作爲外鍵插入另一個表
這是我的代碼:
DateTime dob = DateTime.Parse(txtDob.Text);
string formattedDate = dob.ToString("yyyy-MM-dd");
string qry = "insert into Registration(First_Name,Last_Name,Dob,Place,Zip,Phone_Number)values(@fname,@lname,@dob,@place,@zip,@phone);SELECT SCOPE_IDENTITY()";
MySqlCommand cmd = new MySqlCommand(qry, Connection.get());
cmd.Parameters.AddWithValue("@fname", txtFname.Text);
cmd.Parameters.AddWithValue("@lname", txtLname.Text);
cmd.Parameters.AddWithValue("@dob", formattedDate);
cmd.Parameters.AddWithValue("@place", txtPlace.Text);
cmd.Parameters.AddWithValue("@zip", txtZip.Text);
cmd.Parameters.AddWithValue("@phone", txtPhone.Text);
cmd.ExecuteNonQuery();
int modified = (int)cmd.ExecuteScalar();
Connection.close();
string qryToUser = "insert into User(User_Name,Password)values(@user,@password)";
MySqlCommand cmd1 = new MySqlCommand(qryToUser, Connection.get());
cmd.Parameters.AddWithValue("@user", txtUsername.Text);
cmd.Parameters.AddWithValue("@password", txtPasswordConfirm.Text);
cmd1.ExecuteNonQuery();
我不能夠得到使用cmd.ExecuteScalar()方法REG_ID。 有沒有其他方式或我做錯了什麼? 請別人幫我
請適當地格式化你的代碼片段,並刪除,如「請幫助我的人」從你的問題短語 - 無論如何,我們都在這裏幫助你。 –