附加信息:ExecuteNonQuery:尚未初始化Connection属性。

问题描述:

使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System。 Threading.Tasks;

使用System.Windows.Forms;

使用System.Data.OleDb;

使用System.Data.SqlClient;




命名空间Stock_Control_System

{

   公共部分类产品:表格

    {

        SqlConnection con = new SqlConnection(@"Data Source = INFINITY; Initial Catalog = Stock; Integrated Security = True"); //建立联系   

        SqlCommand cmd;

        SqlDataAdapter适应;

        //更新和删除记录中使用的ID变量  

        int ID = 0;

       公共产品()

        {

            InitializeComponent();

            DisplayData();

        }


        private void Products_Load(object sender,EventArgs e)

        {

$
        }


        private void btnadd_Click(object sender,EventArgs e)

        {

            if(txtprocode.Text!=""&& txtproname.Text!=""&& txtprotype.Text!=""&& txtbrand.Text!="" && txtquantity.Text!=""&&
txtmeter.Text!=""&& txtprice.Text!="")

            {

                cmd = new SqlCommand(@" INSERT INTO [dbo]。[Product]([ProductCode],[ProductName],[ProductType],[Brand],[Quantity],[Meter],[Price]) 

       VALUES(@ ProductCode,@ ProductName,@ ProductType,@ Brand,@ Quantity,@ Meter,@ Price)");

                con.Open();

                cmd.Parameters.AddWithValue(" @@ ProductCode",txtprocode.Text);

                cmd.Parameters.AddWithValue(" @@ ProductName",txtproname.Text);

                cmd.Parameters.AddWithValue(" @@ ProductType",txtprotype.Text);

                cmd.Parameters.AddWithValue(" @ Brand",txtbrand.Text);

                cmd.Parameters.AddWithValue(" @ Quantity",txtquantity.Text);

                cmd.Parameters.AddWithValue(" @ Meter",txtmeter.Text);

                cmd.Parameters.AddWithValue(" @ Price",txtprice.Text);

                cmd.ExecuteNonQuery(); //错误突出显示此代码

                con.Close();

                MessageBox.Show(" Record Inserted Successfully");

                DisplayData();

                ClearData();

            }¥b $ b           否则

            {

                MessageBox.Show(" Please Provide Details!");

            }¥b $ b        }¥b $ b              //在DataGridView中显示数据  

        private void DisplayData()

        {

            con.Open();

            DataTable dt = new DataTable();

            adapt = new SqlDataAdapter(" select * from [dbo]。[Product]",con);

            adapt.Fill(dt);

            dataGridView1.DataSource = dt;

            con.Close();

        }¥b $ b        //清除数据  

        private void ClearData()

        {

            txtprocode.Text ="&quot ;;

            txtproname.Text ="&quot ;;

            txtprotype.Text ="&quot ;;

            txtbrand.Text ="&quot ;;

            txtquantity.Text ="&quot ;;

            txtmeter.Text ="&quot ;;

            txtprice.Text ="&quot ;; $


            ID = 0;

        }


        private void dataGridView1_CellContentClick(object sender,DataGridViewCellEventArgs e)

        {

            ID = Convert.ToInt32(dataGridView1.Rows [e.RowIndex] .Cells [0] .Value.ToString());
$


        }¥b $ b    }¥b $ b    }

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;


namespace Stock_Control_System
{
    public partial class Products : Form
    {
        SqlConnection con = new SqlConnection(@"Data Source = INFINITY; Initial Catalog = Stock; Integrated Security = True"); // making connection   
        SqlCommand cmd;
        SqlDataAdapter adapt;
        //ID variable used in Updating and Deleting Record  
        int ID = 0;
        public Products()
        {
            InitializeComponent();
            DisplayData();
        }

        private void Products_Load(object sender, EventArgs e)
        {

        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            if (txtprocode.Text != "" && txtproname.Text != "" && txtprotype.Text != "" && txtbrand.Text != "" && txtquantity.Text != "" && txtmeter.Text != "" && txtprice.Text != "")
            {
                cmd = new SqlCommand(@"INSERT INTO [dbo].[Product]([ProductCode],[ProductName],[ProductType],[Brand],[Quantity],[Meter],[Price]) 
            VALUES(@ProductCode,@ProductName,@ProductType,@Brand,@Quantity,@Meter,@Price)");
                con.Open();
                cmd.Parameters.AddWithValue("@ProductCode", txtprocode.Text);
                cmd.Parameters.AddWithValue("@ProductName", txtproname.Text);
                cmd.Parameters.AddWithValue("@ProductType", txtprotype.Text);
                cmd.Parameters.AddWithValue("@Brand", txtbrand.Text);
                cmd.Parameters.AddWithValue("@Quantity", txtquantity.Text);
                cmd.Parameters.AddWithValue("@Meter", txtmeter.Text);
                cmd.Parameters.AddWithValue("@Price", txtprice.Text);
                cmd.ExecuteNonQuery(); // the error highlights this code
                con.Close();
                MessageBox.Show("Record Inserted Successfully");
                DisplayData();
                ClearData();
            }
            else
            {
                MessageBox.Show("Please Provide Details!");
            }
        }
             //Display Data in DataGridView  
        private void DisplayData()
        {
            con.Open();
            DataTable dt = new DataTable();
            adapt = new SqlDataAdapter("select * from [dbo].[Product]", con);
            adapt.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();
        }
        //Clear Data  
        private void ClearData()
        {
            txtprocode.Text = "";
            txtproname.Text = "";
            txtprotype.Text = "";
            txtbrand.Text = "";
            txtquantity.Text = "";
            txtmeter.Text = "";
            txtprice.Text = "";

            ID = 0;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());

        }
    }
    }

请帮助我带着这个 

Please help me out with this 

谢谢你

您好,

该命令需要连接如下所示。

The command needs a connection as per below.

static void Demo()
{
    var insertStatement = 
        "INSERT INTO [dbo].[Product]([ProductCode],[ProductName],[ProductType],[Brand],[Quantity],[Meter],[Price]) " + 
        "VALUES(@ProductCode,@ProductName,@ProductType,@Brand,@Quantity,@Meter,@Price)";

    using (var cn = new SqlConnection(@"Data Source = INFINITY; Initial Catalog = Stock; Integrated Security = True"))
    {
        using (var cmd = new SqlCommand(insertStatement, cn) )
        {
            
        }
    }
}


注意最好创建一个连接和命令对象,而不是私有范围的连接。

Note it's best to create a connection and command object as needed rather than having a privately scoped connection.