我尝试制作 Qr 代码应用程序(生成然后将其保存在数据库中),但我的程序代码有一些错误

I try to make Qr code application ( generated and then save it on DataBase) , but have some errors with my program code

我遇到了这两个错误

'SqlConnection' does not contain a definition for 'Parameters' and no extension method 'Parameters' accepting a first argument of type 'SqlConnection' could be found (are you missing a using directive or an assembly reference?)

'SqlConnection' does not contain a definition for 'ExecuteNonQuery' and no extension method 'ExecuteNonQuery' accepting a first argument of type 'SqlConnection' could be found (are you missing a using directive or an assembly reference?)

来源错误:

SqlConnection con1;
        con1 = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User1\Documents\ESDB.mdf;Integrated Security=True;Connect Timeout=30");
        con1.Open();
        string qry = "Insert into QRCODES(Image) VALUES(@PIC)";
        SqlConnection cmd = new SqlConnection(qry);
        MemoryStream STREAM = new MemoryStream();
        pictureBox1.Image.Save(STREAM,System.Drawing.Imaging.ImageFormat.Jpeg);
        byte[] pic = STREAM.ToArray();
        cmd.Parameters.AddWithValue("@PIC", pic);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        con1.Close();

现在,当我用

重新构建我的代码时

SqlCommand cmd = new SqlCommand

我收到新错误(警告)并显示:

An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe Additional information: Object reference not set to an instance of an object.

您正在使用两个 SqlConnection 对象。应该是 SqlCommand:

SqlConnection con1;
con1 = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User1\Documents\ESDB.mdf;Integrated Security=True;Connect Timeout=30");
con1.Open();
string qry = "Insert into QRCODES(Image) VALUES(@PIC)";
SqlCommand cmd = new SqlCommand(qry);
^^^^^^^^^^           ^^^^^^^^^^
MemoryStream STREAM = new MemoryStream();
pictureBox1.Image.Save(STREAM, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] pic = STREAM.ToArray();
cmd.Parameters.AddWithValue("@PIC", pic);
cmd.ExecuteNonQuery();
cmd.Dispose();
con1.Close();

还要小心该代码,因为您使用的是命令和连接,而不是处置它们。它可能会导致内存泄漏和一些问题。

也许你应该考虑重构为这样的东西:

using (SqlConnection con1 = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User1\Documents\ESDB.mdf;Integrated Security=True;Connect Timeout=30"))
{
    con1.Open();
    string qry = "Insert into QRCODES(Image) VALUES(@PIC)";
    using (SqlCommand cmd = new SqlCommand(qry))
    {
        using (MemoryStream STREAM = new MemoryStream())
        {
            pictureBox1.Image.Save(STREAM, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] pic = STREAM.ToArray();

            cmd.Parameters.AddWithValue("@PIC", pic);
            cmd.ExecuteNonQuery();
            con1.Close();
        }
    }
}

UPDATE:

如错误所述,您的问题是命令和连接没有关联。这应该有效:

using (SqlConnection con1 = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User1\Documents\ESDB.mdf;Integrated Security=True;Connect Timeout=30"))
{
    con1.Open();
    string qry = "Insert into QRCODES(Image) VALUES(@PIC)";
    using (SqlCommand cmd = con1.CreateCommand()) //associate the connection to the command
    {
        cmd.CommandText = qry; //assign the command text to the command.
        using (var STREAM = new MemoryStream())
        {
            pictureBox1.Image.Save(STREAM, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] pic = STREAM.ToArray();

            cmd.Parameters.AddWithValue("@PIC", pic);
            cmd.ExecuteNonQuery();
            con1.Close();
        }
    }
}