我想將UserId的返回值從sql查詢存儲到Usersid變量。但我無法獲取該值.FYI UserName是文本。想要在c中存儲sql select語句的返回值#
int usersid;
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AzureSql"].ToString()))
using (SqlCommand command = new SqlCommand())
{
connection.Open();
command.CommandText = @"Select UserId from [dbo].[User] where username= @username";
command.Parameters.Clear();
command.Parameters.AddWithValue("@username", currentUser.UserName);
usersid = (int)command.ExecuteScalar();
command.CommandText = @"INSERT INTO [dbo].[ClientEmailConfirmation] ([JobNumber],[OrderNumber],[UserId]) VALUES
(@JobNumber,@OrderNumber,@UserId)";
command.Parameters.Clear();
command.Parameters.AddWithValue("@JobNumber", JobNumberTextBox.Text);
command.Parameters.AddWithValue("@OrderNumber", OrderNumberTextBox.Text);
command.Parameters.AddWithValue("@UserId", usersid);
command.ExecuteNonQuery();
}
我將不勝感激您的幫助 謝謝, 一個
這中回答:[?得到插入行的身份最好的辦法(http://stackoverflow.com/questions/42648/best-way-to-get-identity-of-inserted-row )和[執行插入命令並返回插入的Id在Sql](http://stackoverflow.com/questions/18373461/execute-insert-command-and-return-inserted-id-in-sql) – Adrian
此問題已答覆[獲取插入行的身份的最佳方式?](http://stackoverflow.com/questions/42648/best-way-to-get-identity-of-inserted-row)和[執行插入命令並返回插入的ID Sql](http://stackoverflow.com/questions/18373461/execute-insert-command-and-return-inserted-id-in-sql) – Adrian