有谁知道我如何从 Student 表中获取 StudentID,将其存储在 datareader 或数据集中,然后使用它来更新另一个表,即 Users 表,因为我希望用户的用户名和密码作为默认的 StudentID . 顺便说一句,这是 C# ASP.NET。这是我的代码。
SqlConnection conUpdate = new SqlConnection(GetConnectionString());
conUpdate.Open();
SqlCommand com2 = new SqlCommand();
com2.Connection = conUpdate;
com2.CommandText = "SELECT Students.StudentID, Users.UserID FROM Students, Users " +
"WHERE Students.UserID = Users.UserID";
int UserId = ((int)com2.ExecuteScalar());
com2.CommandText = "SELECT MAX(StudentID) FROM Students";
int StudentId = ((int)com2.ExecuteScalar());
com2.CommandType = CommandType.Text;
com2.CommandText = "UPDATE Users SET UserName=@UserName, Password=@Password WHERE UserID=@UserID";
com2.Parameters.Add("@UserName", SqlDbType.NVarChar);
com2.Parameters.Add("@Password", SqlDbType.NVarChar);
com2.Parameters[0].Value = reader;
com2.Parameters[1].Value = reader;
com2.ExecuteNonQuery();
conUpdate.Close();
conUpdate.Dispose();