我想将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