在C#中处理SqlCommand的概念

时间:2013-09-11 09:25:12

标签: c# sql sqldatareader sqlconnection

我正在尝试从数据库中选择用户列表,并根据条件isSent == false向每个用户发送电子邮件。向他们发送电子邮件后,此false的值应更新为true。下面的代码是我从数据库中检索用户列表并将方法sendEmail()调用到每个用户的方式。

           myConnection.Open();
            //*******
            try
            {
                SqlDataReader myReader = null;
                string sql = "SELECT * FROM testTable where isSent = false";
                SqlCommand myCommand = new SqlCommand(sql, myConnection);
                myReader = myCommand.ExecuteReader();

                while (myReader.Read())
                {
                    sendEmail(myReader["emailAdd"].ToString(), 
                              myReader["UserID"].ToString());
                }

第二部分:

public static void sendEmail(string emailAdd,string userID){
    .
    .
    .
    try
    {
        smtpClient.Send(mail);
        try
            {
                string sql = "UPDATE testTable SET isSent = 1 WHERE  UserID = " + userID;
                SqlCommand myCommand = new SqlCommand(sql, myConnection);
                int rows = myCommand.ExecuteNonQuery();
                .
                .
                .
            }
    }
}

我面临的问题是,因为从main方法我已经拥有SqlDataReader来读取,所以我现在无法更新。我有什么工作吗?我得到的错误消息如下:

There is already an open DataReader associated with this Command which must be closed first.

3 个答案:

答案 0 :(得分:3)

  

我面临的问题是,因为从main方法我已经拥有SqlDataReader来读取,所以我现在无法更新。我有什么工作吗?

这只是一个问题,因为您正在共享连接(myConnection)。不要那样做。每次要执行数据库操作时创建新的SqlConnection,并让连接池基础结构处理使其高效。另外,对数据库相关资源使用using语句:

using (var connection = new SqlConnection(...))
{
    using (var command = new SqlCommand(...))
    {
        using (var reader = command.ExecuteReader(...))
        {
            ...
        }
    }
}

答案 1 :(得分:0)

作为另一种选择。您可以将其添加到数据库的连接字符串中。

  

MultipleActiveResultSets =真

性能下降很小。它在旧的SQL Server中不受支持。我想2005年之前

答案 2 :(得分:0)

试试这个代码......`

 public static void sendEmail(string emailAdd,string userID){
  try
    {
        SqlConnection con = new SqlConnection(@"");
        con.Open();

        MailMessage mail = new MailMessage("example@gmail.com", emailAdd);
        SmtpClient smtpClient = new SmtpClient();
        NetworkCredential nc = new NetworkCredential("example@gmail.com", "test");

        smtpClient.Port = 587;
        smtpClient.Host = "smtp.gmail.com";
        smtpClient.EnableSsl = true;
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.UseDefaultCredentials = false;
       // smtpClient.Host = "smtp.google.com";
        mail.Subject = "this is a test email.";
        mail.Body = "this is my test email body";
        smtpClient.Credentials = nc;
        smtpClient.Send(mail);

        using (SqlCommand cmd = con.CreateCommand())
        {
            string sql = "UPDATE TestTable SET IsSent = 'true' WHERE  UserID = " + userID;
            SqlCommand myCommand = new SqlCommand(sql, con);
            int rows = myCommand.ExecuteNonQuery();
        }
    }
    catch(Exception e)
    {
        string message = e.Message;
    }`
 }