【问题标题】:INSERT error using C#/Asp.Net/SQL使用 C#/Asp.Net/SQL 插入错误
【发布时间】:2012-05-22 04:56:56
【问题描述】:

我尝试运行查询以将数据插入数据库,但在运行代码时出现错误..

ExecuteNonQuery 需要一个开放且可用的连接。这 连接的当前状态为关闭。

您能告诉我错误是什么以及为什么会发生吗?

{
    con2.Open();

    if (TextBox1.Text == "")
    {
        Response.Write("<script>alert('please enter Login Name')</script>");
    }
    else if (TextBox2.Text == "")
    {
        Response.Write("<script>alert('please enter Password')</script>");
    }
    else if (TextBox3.Text == "")
    {
        Response.Write("<script>alert('please enter Confirm Password')</script>");
    }
    else
    {
        //if (TextBox2.Text == TextBox3.Text)
        //{

            string a;
            a = "insert into tbl_Purchase_Users(Login_Name, Password, Uname, Uid, EmailID, Role, Status) values(@LName, @Pswd, @Uname, @uid, @Eid, @role, @stat)";
            SqlCommand cm = new SqlCommand(a, con1);
            cm.Parameters.AddWithValue("@LName", TextBox1.Text);

            string original;
            original = TextBox2.Text.Trim();
            int h = original.GetHashCode();
            string withHash = original;
            b1 = Encoding.BigEndianUnicode.GetBytes(withHash);
            encrypted = Convert.ToBase64String(b1);
            cm.Parameters.AddWithValue("@Pswd", encrypted);
            cm.Parameters.AddWithValue("@Uname", TextBox3.Text);
            cm.Parameters.AddWithValue("@uid", TextBox4.Text);
            cm.Parameters.AddWithValue("@Eid", TextBox5.Text);
            cm.Parameters.AddWithValue("@role", TextBox6.Text);
            cm.Parameters.AddWithValue("@stat", TextBox7.Text);

            cm.ExecuteNonQuery();
            Response.Write("<Script>alert('inserted')</script>");
        }
        con2.Close();
    }

【问题讨论】:

    标签: c#


    【解决方案1】:

    您只打开了 con2 而不是 con1。 您在 SqlCommand 中传递了 con1。 使用以下代码:

    SqlCommand cm = new SqlCommand(a, con2);
    

    【讨论】:

    • 谢谢朋友..这是我犯的小错误..谢谢纠正我
    • @user1402552 如果某个答案对您有帮助,那么您需要接受它作为答案。
    【解决方案2】:

    您正在打开名为 con2 的连接,但您在 SqlCommand 上使用 con1。

    据我所知你还没有打开con1

    【讨论】:

      【解决方案3】:

      看起来您有两个不同的 SqlConnection 对象 - con1 和 con2。您正在打开 con2,但将 con1 传递给 SqlCommand 构造函数。

      如错误消息所述,您使用的连接必须打开。

      如果您将 con2 传递给 SqlCommand 构造函数,或者如果您打开 con1,您的代码应该可以工作。

      【讨论】:

        【解决方案4】:

        确保 con1 已打开,因为您的呼叫中显示的唯一 .Open() 呼叫与 con2 相关

        con1.Open();
        

        【讨论】:

          【解决方案5】:

          我看不到 con1.open()。您已经在 Sqlcommand 中使用了 Con1。 请打开 Con1.Open();

          【讨论】:

            【解决方案6】:

            只需使用一个连接对象,例如

             con2.Open();
             SqlCommand cm = new SqlCommand(a, con2)
            

            【讨论】:

              【解决方案7】:

              首先你需要使用 Open() 连接数据库,在你的例子中是 con1.Open();然后执行操作并关闭连接。 con1.关闭(); 别的 { //if (TextBox2.Text == TextBox3.Text) //{

                      string a;
                      a = "insert into tbl_Purchase_Users(Login_Name,Password,Uname,Uid,EmailID,Role,Status) values(@LName,@Pswd,@Uname,@uid,@Eid,@role,@stat)";
                      SqlCommand cm = new SqlCommand(a, con1);
                      con1.Open();
                      cm.Parameters.AddWithValue("@LName", TextBox1.Text);
              
                      string original;
                      original = TextBox2.Text.Trim();
                      int h = original.GetHashCode();
                      string withHash = original;
                      b1 = Encoding.BigEndianUnicode.GetBytes(withHash);
                      encrypted = Convert.ToBase64String(b1);
                      cm.Parameters.AddWithValue("@Pswd", encrypted);
                      cm.Parameters.AddWithValue("@Uname", TextBox3.Text);
                      cm.Parameters.AddWithValue("@uid", TextBox4.Text);
                      cm.Parameters.AddWithValue("@Eid", TextBox5.Text);
                      cm.Parameters.AddWithValue("@role", TextBox6.Text);
                      cm.Parameters.AddWithValue("@stat", TextBox7.Text);
              
                      cm.ExecuteNonQuery();
                      Response.Write("<Script>alert('inserted')</script>");
                      con1.Close();
              

              }

              【讨论】:

                猜你喜欢
                • 2013-05-05
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多