0

有谁知道我如何从 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();
4

3 回答 3

0

您的命令com2.CommandText = "SELECT MAX(StudentID) FROM Students";将返回 Max student ID,这可能不是必需的。您之前的命令com2.CommandText = "SELECT Students.StudentID, Users.UserID ....是获取学生用户 ID 所需要的。

您可以使用数据阅读器(面向连接),如下所示:

SqlDataReader reader = com2.ExecuteReader();
while (reader.Read())
{
     int UserId = Convert.ToInt(reader[0]);// or reader["UserID"]
}
reader.Close();

或者您可以使用DataAdapter(断开连接模式),例如:

SqlDataAdapter a = new SqlDataAdapter(com2, connection);
DataTable dt = new DataTable();
a.Fill(dt);

现在您dt.Rows["UserID"]将拥有所需的用户 ID。

你可能想看看这个: http: //www.dotnetperls.com/sqldataadapter

于 2012-06-22T05:59:42.330 回答
0

如果我理解正确,我认为以下代码可能有效。或者至少给你一个关于如何去做的想法。我假设您希望每个学生的用户名和密码默认为他们的学生 ID

    SqlConnection conUpdate = new SqlConnection(GetConnectionString());
    conUpdate.Open();

    SqlCommand com2 = new SqlCommand();
    com2.Connection = conUpdate;
    com2.CommandType = CommandType.Text;
    com2.CommandText = "SELECT Students.StudentID, Users.UserID FROM Students, Users " +
        "WHERE Students.UserID = Users.UserID";

    SqlDataReader reader = com2.ExecuteReader();

    if(reader != null)
    {
        while(reader.Read())
        {
            SqlCommand com3 = new SqlCommand();
            com3.Connection = conUpdate;
            com3.CommandType = CommandType.Text;
            com3.CommandText = "UPDATE Users SET UserName=@UserName, Password=@Password WHERE UserID=@UserID";
            // Assuming that you need both the UserName and Password to default to StudentID
            com3.Parameters.AddWithValue("@UserName", reader.GetString(0)); // Assuming StudentID is NVARCHAR
            com3.Parameters.AddWithValue("@Password", reader.GetString(0)); // Assuming StudentID is NVARCHAR
            com3.Parameters.AddWithValue("@UserID", reader.GetString(1)); // Assuming UserID is NVARCHAR
            com3.ExecuteNonQuery();                
        }
        reader.Close();
    }

    conUpdate.Close();
于 2012-06-22T05:59:57.487 回答
0

由于您已经在选择查询中获取了 UserId,因此您应该使用 DataReader 获取值。像这样:

// Execute the query
SqlDataReader rdr = cmd.ExecuteReader();
int UserId;
while(rdr.Read())
{
  UserId = Convert.ToInt32(rdr["UserID"].ToString());
}
于 2012-06-22T05:54:41.407 回答