连接必须有效且开放。 MySql 5.2出错

时间:2013-01-28 15:10:55

标签: c# .net mysql

我在调用存储过程时遇到此错误...为什么?

    public void InsertVideo()
    {
            MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["AxWaveConnection"].ToString());
            MySqlCommand cmd = new MySqlCommand("InsertVideo", conn);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            cmd.Parameters.Add(new MySqlParameter("in_youtubevideoid", VideoId));
            cmd.Parameters.Add(new MySqlParameter("in_title", Title));
            cmd.Parameters.Add(new MySqlParameter("in_rating", ViewCount));
            cmd.Parameters.Add(new MySqlParameter("in_viewcount", Rating));

            cmd.ExecuteNonQuery();
    }

2 个答案:

答案 0 :(得分:3)

因为您在致电OPEN之前忘记了ExecuteNonQuery连接,请先致电conn.Open()

conn.Open();
cmd.ExecuteNonQuery();

答案 1 :(得分:0)

  Your complete solution

    public void InsertVideo()
      {
         MySqlConnection conn = new  MySqlConnection(ConfigurationManager.ConnectionStrings["AxWaveConnection"].ToString());

         MySqlCommand cmd = new MySqlCommand("InsertVideo", conn);

        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        conn.Open()
        cmd.Parameters.Add(new MySqlParameter("in_youtubevideoid", VideoId));
        cmd.Parameters.Add(new MySqlParameter("in_title", Title));
        cmd.Parameters.Add(new MySqlParameter("in_rating", ViewCount));
        cmd.Parameters.Add(new MySqlParameter("in_viewcount", Rating));

        cmd.ExecuteNonQuery();
      }