在数据库

时间:2016-09-05 05:11:03

标签: c# asp.net

我正在尝试将客户端详细信息插入到数据库中但是会出现此异常

  

连接必须有效且开放。 at MySql.Data.MySqlClient.MySqlCommand.Throw(Exception ex)at MySql.Data.MySqlClient.MySqlCommand.CheckState()at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader ()at book_online.saveClient_Click的MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()(Object sender,EventArgs e)

MySqlConnection con = new MySqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings("conio").ConnectionString();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "INSERT INTO clientsDetails (clientName, companyAddress, city, contactPersonName, contactNumber1) Values(@clientName, @companyAddress, @city, @contactPersonName, @contactNumber1)";
cmd.Parameters.AddWithValue("@clientName", companyName.Text);
cmd.Parameters.AddWithValue("@companyAddress", companyAddress.Text);
cmd.Parameters.AddWithValue("@city", companyCity.Text);
cmd.Parameters.AddWithValue("@contactPersonName", concernPersonName.Text);
cmd.Parameters.AddWithValue("@contactNumber1", countryCodeWithNumber);
con.Open();
cmd.ExecuteNonQuery();
con.Close();

3 个答案:

答案 0 :(得分:2)

您已经打开了连接并执行了命令,但是您忘记将连接分配给命令,那么只有代码可以正常工作。强烈建议您也使用。然后代码如下所示:

using (MySqlConnection con = new MySqlConnection())
{
    con.ConnectionString = ConfigurationManager.ConnectionStrings("conio").ConnectionString();
    string queryText = "INSERT INTO clientsDetails (clientName, companyAddress, city, contactPersonName, contactNumber1) Values(@clientName, @companyAddress, @city, @contactPersonName, @contactNumber1)";
    using (MySqlCommand cmd = new MySqlCommand(queryText, con))
    {
        cmd.Parameters.AddWithValue("@clientName", companyName.Text);
        cmd.Parameters.AddWithValue("@companyAddress", companyAddress.Text);
        cmd.Parameters.AddWithValue("@city", companyCity.Text);
        cmd.Parameters.AddWithValue("@contactPersonName", concernPersonName.Text);
        cmd.Parameters.AddWithValue("@contactNumber1", countryCodeWithNumber);
        con.Open();
        cmd.ExecuteNonQuery();
    }
}

答案 1 :(得分:1)

命令缺少连接字符串。

MySqlConnection con = new MySqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings("conio").ConnectionString();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = con;
cmd.CommandText = "INSERT INTO clientsDetails (clientName, companyAddress, city, contactPersonName, contactNumber1) Values(@clientName, @companyAddress, @city, @contactPersonName, @contactNumber1)";
cmd.Parameters.AddWithValue("@clientName", companyName.Text);
cmd.Parameters.AddWithValue("@companyAddress", companyAddress.Text);
cmd.Parameters.AddWithValue("@city", companyCity.Text);
cmd.Parameters.AddWithValue("@contactPersonName", concernPersonName.Text);
cmd.Parameters.AddWithValue("@contactNumber1", countryCodeWithNumber);
con.Open();
cmd.ExecuteNonQuery();
con.Close();

答案 2 :(得分:0)

您没有为命令指定连接对象:

cmd.Connection = con;

此外,最好使用using块,当它退出该块时会自动关闭您的连接:

using(MySqlConnection con = new MySqlConnection())
{
   con.ConnectionString =
                       ConfigurationManager.ConnectionStrings("conio").ConnectionString();

   MySqlCommand cmd = new MySqlCommand();
   cmd.CommandText = @"INSERT INTO clientsDetails
              (clientName, companyAddress, city, contactPersonName, contactNumber1)
               Values
              (@clientName, @companyAddress, @city, @contactPersonName, @contactNumber1)";

   cmd.Connection = con;
   cmd.Parameters.AddWithValue("@clientName", companyName.Text);
   cmd.Parameters.AddWithValue("@companyAddress", companyAddress.Text);
   cmd.Parameters.AddWithValue("@city", companyCity.Text);
   cmd.Parameters.AddWithValue("@contactPersonName", concernPersonName.Text);
   cmd.Parameters.AddWithValue("@contactNumber1", countryCodeWithNumber);
   con.Open();
   cmd.ExecuteNonQuery();
}

仅供参考,using关键字允许编译为封闭的块生成try...finally,并在Disposeusing内的对象上调用con在你的情况下)生成的finally块内。因此,保证在最终得到保证的所有情况下都能关闭您的连接。