阅读背景:

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

来源:互联网 
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);
            }

        }
private void btnSave_Click(object sender, Event



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: