错误ExecuteNonQuery:当我单击插入按钮时,尚未初始化Connection属性

时间:2020-12-09 16:30:30

I've been trying to add data from Visual Studio to SQL server using c# language but it shows an error when I press the add button. The connection class is given below:

我一直在尝试使用c#语言将Visual Studio中的数据添加到SQL服务器,但是当我按下添加按钮时它会显示错误。连接类如下:

class Globalconnection
{
    public static SqlConnection cn;
    public static string strServer = "";
    public static string strDatabase = "";
    public static string strUID = "";
    public static string strPWD = "";

    public static void SetupConnection()
    {
        cn = new SqlConnection("Server=" + strServer + "; Database=" + strDatabase + "; UID=" + strUID + ";PWD=" + strPWD);
        cn.Open();
    }
}

In this class most of the SQL commands are given for inserting data into SQL.

在本类中,大多数SQL命令都是为了将​​数据插入SQL而给出的。

class DACLASS
{
    public static string strUID = "";
    public static string strPWD = "";
    public static string strNPWD = "";
    public static DataTable AccessData()
    {
        SqlDataAdapter da = new SqlDataAdapter("Select * from Users where Username='" + strUID + "' and Password='" + strPWD + "'", Globalconnection.cn);
        DataSet ds = new DataSet();
        da.Fill(ds, "USERS");
        return ds.Tables[0];
    }

    public static void addStatue(string ID, string Name)
    {
        string strAddStatue = "Insert into Statues  VALUES (@ID, @Name)";

        SqlCommand cmdAddStatue = new SqlCommand(strAddStatue, Globalconnection.cn);

        cmdAddStatue.Parameters.AddWithValue("@ID", ID);
        cmdAddStatue.Parameters.AddWithValue("@Name", Name);
        cmdAddStatue.ExecuteNonQuery(); 
    }
}

The following command is from the from "ADD" button.

以下命令来自“ADD”按钮。

private void txtADD_Click(object sender, EventArgs e)
{
    try
    {
        if (txtID.Text == "" || txtName.Text == "" || txtSize.Text == "" || txtTCP.Text == "" || txtMP.Text == "")
        {
            MessageBox.Show("ID, Name, Size, Total Cost Price and Marked Price are Mandatory", "Handicrafts");
        }
        else
        {
            DACLASS.addStatue(txtID.Text, txtName.Text);
            MessageBox.Show("Statue Added", "Handicraft");
            btnClear_Click(sender, e);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Handicrafts");
        btnClear_Click(sender, e);
    }
}

When I put null values the Messagebox is displayed but when I insert the correct values I get this error

当我输入空值时,会显示Messagebox,但是当我插入正确的值时,我收到此错误

ExecuteNonQuery: Connection property has not been initialized

ExecuteNonQuery:尚未初始化Connection属性

3 个解决方案

#1


3  

You need to open the SqlConnection before calling ExecuteNonQuery method from an instance of SqlCommand. After calling ExecuteNonQuery, the SqlConnection must be closed. I would suggest changing Globalconnection as below

您需要在从SqlCommand实例调用ExecuteNonQuery方法之前打开SqlConnection。在调用ExecuteNonQuery之后,必须关闭SqlConnection。我建议改变Globalconnection如下

class Globalconnection
{
    public static string strServer = "";
    public static string strDatabase = "";
    public static string strUID = "";
    public static string strPWD = "";
    public static SqlConnection cn = new SqlConnection("Server=" + strServer + "; Database=" + strDatabase + "; UID=" + strUID + ";PWD=" + strPWD);
}

then use using statement inside addStatue method to make sure the SqlConnection is closed after cmdAddStatue is executed.

然后在addStatue方法中使用using语句以确保在执行cmdAddStatue后关闭SqlConnection。

public static void addStatue(string ID, string Name)
{
    string strAddStatue = "Insert into Statues  VALUES (@ID, @Name)";

    using (var connection1 = Globalconnection.cn)
    {
        using (var cmdAddStatue = new SqlCommand(strAddStatue, connection1))
        {
            cmdAddStatue.Parameters.AddWithValue("@ID", ID);
            cmdAddStatue.Parameters.AddWithValue("@Name", Name);

            connection1.Open();
            cmdAddStatue.ExecuteNonQuery(); 
        }
    }
}

#2


0  

Add this line to your constructor, or in desperation to the txtADD_Click() method

将此行添加到构造函数中,或者绝望地添加到txtADD_Click()方法

 Globalconnection.SetupConnection();

You have done all the work to explain to the computer how to open the connection, you are just never actually telling it to DO it :)

你已经完成了所有的工作来向计算机解释如何打开连接,你实际上从来没有告诉它去做:)

Hope it helps..

希望能帮助到你..

#3


0  

It does seem like you never actually ran the setup connection method in the GlobalConnection class. maybe you'd be better implementing the singleton pattern around your sql connection.

看起来您实际上从未在GlobalConnection类中运行设置连接方法。也许你最好在你的sql连接周围实现单例模式。

#1


3  

You need to open the SqlConnection before calling ExecuteNonQuery method from an instance of SqlCommand. After calling ExecuteNonQuery, the SqlConnection must be closed. I would suggest changing Globalconnection as below

您需要在从SqlCommand实例调用ExecuteNonQuery方法之前打开SqlConnection。在调用ExecuteNonQuery之后,必须关闭SqlConnection。我建议改变Globalconnection如下

class Globalconnection
{
    public static string strServer = "";
    public static string strDatabase = "";
    public static string strUID = "";
    public static string strPWD = "";
    public static SqlConnection cn = new SqlConnection("Server=" + strServer + "; Database=" + strDatabase + "; UID=" + strUID + ";PWD=" + strPWD);
}

then use using statement inside addStatue method to make sure the SqlConnection is closed after cmdAddStatue is executed.

然后在addStatue方法中使用using语句以确保在执行cmdAddStatue后关闭SqlConnection。

public static void addStatue(string ID, string Name)
{
    string strAddStatue = "Insert into Statues  VALUES (@ID, @Name)";

    using (var connection1 = Globalconnection.cn)
    {
        using (var cmdAddStatue = new SqlCommand(strAddStatue, connection1))
        {
            cmdAddStatue.Parameters.AddWithValue("@ID", ID);
            cmdAddStatue.Parameters.AddWithValue("@Name", Name);

            connection1.Open();
            cmdAddStatue.ExecuteNonQuery(); 
        }
    }
}

#2


0  

Add this line to your constructor, or in desperation to the txtADD_Click() method

将此行添加到构造函数中,或者绝望地添加到txtADD_Click()方法

 Globalconnection.SetupConnection();

You have done all the work to explain to the computer how to open the connection, you are just never actually telling it to DO it :)

你已经完成了所有的工作来向计算机解释如何打开连接,你实际上从来没有告诉它去做:)

Hope it helps..

希望能帮助到你..

#3


0  

It does seem like you never actually ran the setup connection method in the GlobalConnection class. maybe you'd be better implementing the singleton pattern around your sql connection.

看起来您实际上从未在GlobalConnection类中运行设置连接方法。也许你最好在你的sql连接周围实现单例模式。