“ExecuteReader:连接属性尚未初始化。”

时间:2023-01-29 16:27:44
private void btnSave_Click(object sender, EventArgs e)
{
    try
    {
        con = new SqlConnection("Data Source = LENOVO; Initial Catalog = MainData; Integrated Security = True");
        con.Open();

        string CheckID = "select StaffID from PersonsData where StaffID='" + txtStaffID.Text + "'";
        cm = new SqlCommand(CheckID);

        SqlDataReader rdr = null;

        rdr = cm.ExecuteReader();

        if (rdr.Read())
        {
            MessageBox.Show("Company Name Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            txtStaffID.Text = "";
            txtStaffID.Focus();
        }
        else
        {
            byte[] img = null;
            FileStream fs = new FileStream(imgLoc, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            img = br.ReadBytes((int)fs.Length);
            string Query = "insert into PersonsData (StaffID, FullName, Email, Address, Picture) values('" + this.txtStaffID.Text + "','" + this.txtFullname.Text + "','" + this.txtEmail.Text + "','" + this.txtAddress.Text + "',@img)";
                    if (con.State != ConnectionState.Open)
                        con.Open();
                    cm = new SqlCommand(Query, con);
                    cm.Parameters.Add(new SqlParameter("@img", img));
                    int x = cm.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show(x.ToString() + "Successfully Saved!");
                }
            }

            catch (Exception ex)
            {
                con.Close();
                MessageBox.Show(ex.Message);
            }

        }

This is my code i don't understand why I'm getting this error:

这是我的代码,我不明白为什么我收到此错误:

ExecuteReader:Connection Property has not been initialized.

ExecuteReader:尚未初始化连接属性。

I'm making a save button where the Staffid will be checked first if already.

我正在制作一个保存按钮,如果已经将首先检查Staffid。

4 个解决方案

#1


2  

Before executing the command, you need to say which connection is to be used. In your case, it is:

在执行命令之前,您需要说明要使用哪个连接。在你的情况下,它是:

cm.Connection = con;

Take a note that, include this line of code after opening the connection and after creating the instance of SqlCommand.

请注意,在打开连接后以及创建SqlCommand实例后,请包含此行代码。

#2


1  

    con = new SqlConnection("Data Source = LENOVO; Initial Catalog = MainData; Integrated Security = True");
    con.Open();
    string CheckID = "select StaffID from PersonsData where StaffID='" + txtStaffID.Text + "'";
    cm = new SqlCommand(CheckID);
    cm.Connection = con; //Assign connection to command

You didn't assign connection to SqlCommand used in the reader

您没有为阅读器中使用的SqlCommand分配连接

#3


0  

The error message is clear enough, You have to assign the connection for the Command, either through assignement or through the constructor, That is:

错误消息足够清楚,您必须通过assignement或通过构造函数为Command分配连接,即:

cm = new SqlCommand(CheckID);
cm.Connection = con; // Should be added
SqlDataReader  rdr = cm.ExecuteReader();

Or else you can use the constructor to initialize the command like this:

否则你可以使用构造函数来初始化命令,如下所示:

cm = new SqlCommand(CheckID,con);

Hope that you are aware of these things since you ware used it correctly in the else part of the given snippet

希望您知道这些事情,因为您在给定代码段的else部分中正确使用了它

#4


0  

Make sure that you assign the SqlConnection to your Command-Object. You can do this via Constructor or Property:

确保将SqlConnection分配给Command-Object。您可以通过构造函数或属性执行此操作:

con = new SqlConnection(//Your Connectionstring)
//assign via Constructor
cm = new SqlCommand(CheckID, con);
//or via Property
cm.Connection = con;
SqlDataReader rdr = null;
rdr = cm.ExecuteReader();

Further I would recommend you to use a using-block to make sure that the Command and Connection gets destroyed after using it.

此外,我建议您使用using-block确保命令和连接在使用后被销毁。

using (var con = new SqlConnection())
{
   using (var cm = new SqlCommand())
   {
   }
}

#1


2  

Before executing the command, you need to say which connection is to be used. In your case, it is:

在执行命令之前,您需要说明要使用哪个连接。在你的情况下,它是:

cm.Connection = con;

Take a note that, include this line of code after opening the connection and after creating the instance of SqlCommand.

请注意,在打开连接后以及创建SqlCommand实例后,请包含此行代码。

#2


1  

    con = new SqlConnection("Data Source = LENOVO; Initial Catalog = MainData; Integrated Security = True");
    con.Open();
    string CheckID = "select StaffID from PersonsData where StaffID='" + txtStaffID.Text + "'";
    cm = new SqlCommand(CheckID);
    cm.Connection = con; //Assign connection to command

You didn't assign connection to SqlCommand used in the reader

您没有为阅读器中使用的SqlCommand分配连接

#3


0  

The error message is clear enough, You have to assign the connection for the Command, either through assignement or through the constructor, That is:

错误消息足够清楚,您必须通过assignement或通过构造函数为Command分配连接,即:

cm = new SqlCommand(CheckID);
cm.Connection = con; // Should be added
SqlDataReader  rdr = cm.ExecuteReader();

Or else you can use the constructor to initialize the command like this:

否则你可以使用构造函数来初始化命令,如下所示:

cm = new SqlCommand(CheckID,con);

Hope that you are aware of these things since you ware used it correctly in the else part of the given snippet

希望您知道这些事情,因为您在给定代码段的else部分中正确使用了它

#4


0  

Make sure that you assign the SqlConnection to your Command-Object. You can do this via Constructor or Property:

确保将SqlConnection分配给Command-Object。您可以通过构造函数或属性执行此操作:

con = new SqlConnection(//Your Connectionstring)
//assign via Constructor
cm = new SqlCommand(CheckID, con);
//or via Property
cm.Connection = con;
SqlDataReader rdr = null;
rdr = cm.ExecuteReader();

Further I would recommend you to use a using-block to make sure that the Command and Connection gets destroyed after using it.

此外,我建议您使用using-block确保命令和连接在使用后被销毁。

using (var con = new SqlConnection())
{
   using (var cm = new SqlCommand())
   {
   }
}