如何在另一个表中获取值,将其存储在变量或数据读取器中,然后使用它来更新另一个表

时间:2012-06-22 05:32:59

标签: c# asp.net datareader

有没有人知道如何从Students表中获取StudentID,将其存储在datareader或数据集中,然后使用它来更新另一个表,即Users Table,因为我希望用户的用户名和密码是他们的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();

3 个答案:

答案 0 :(得分:0)

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

// Execute the query
SqlDataReader rdr = cmd.ExecuteReader();
int UserId;
while(rdr.Read())
{
  UserId = Convert.ToInt32(rdr["UserID"].ToString());
}

答案 1 :(得分:0)

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

您可以使用Data reader(面向连接),如下所示:

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

答案 2 :(得分:0)

如果我理解正确,我认为以下代码可能有效。或者至少让你知道如何去做。假设您希望每个学生的用户名和密码默认为其StudentID

    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();