首页 > 解决方案 > System.InvalidOperationException:“ExecuteNonQuery:连接属性尚未初始化。”

问题描述

我在cmd.ExecuteNonQuery();. 有什么我错过的吗?我已经尝试了很多在 StackOverflow 中找到的方法和更正,但没有任何效果。

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.Sql;
using System.Data.SqlClient;


namespace InventoryManagementSystem
{
    public partial class frmInventory : Form
    {
        SqlCommand cmd;
        SqlConnection con;
        SqlDataAdapter da;
        DataTable dt;
        int id;

        public frmInventory()
        {
            InitializeComponent();
            ShowData();
        }

        public void ShowData()
        {
            con = new SqlConnection(@"Data Source=LAPTOP-KA7UGSG3;Initial Catalog=imsysdb;Integrated Security=True");
            con.Open();
            da = new SqlDataAdapter("SELECT * FROM Inventory", con);
            dt = new DataTable();
            da.Fill(dt);
            dgv_Inventory.DataSource = dt;
        }

        private void UpdateRecord()
        {
            con = new SqlConnection(@"Data Source=LAPTOP-KA7UGSG3;Initial Catalog=imsysdb;Integrated Security=True");
            con.Open();
            cmd = new SqlCommand("UPDATE Report set Quantity=x.q from (SELECT Variety, SUM(Quantity) as q FROM Inventory GROUP BY Variety) x where Report.Variety=x.Variety");
            cmd.ExecuteNonQuery();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if(cboxVariety.Text == "" || txtboxWeight.Text == "" || txtboxPrice.Text == "" || NumQuantity.Text == "")
            {
                MessageBox.Show("All fields are required!");
            }
            else
            {
                con = new SqlConnection(@"Data Source=LAPTOP-KA7UGSG3;Initial Catalog=imsysdb;Integrated Security=True");
                con.Open();
                cmd = new SqlCommand("INSERT INTO Inventory (Variety, Weight, Price, Quantity, TotalPrice) VALUES(@Variety, @Weight, @Price, @Quantity, @TotalPrice)", con);

                decimal weight = Convert.ToDecimal(txtboxWeight.Text);
                decimal price = Convert.ToDecimal(txtboxPrice.Text);
                int quantity = Convert.ToInt32(NumQuantity.Text);
                decimal totalPrice = weight * price * quantity;

                cmd.Parameters.AddWithValue("@Variety", cboxVariety.SelectedItem.ToString());
                cmd.Parameters.AddWithValue("@Weight", weight);
                cmd.Parameters.AddWithValue("@Price", price);
                cmd.Parameters.AddWithValue("@Quantity", quantity);
                cmd.Parameters.AddWithValue("@TotalPrice", totalPrice);
                cmd.ExecuteNonQuery();
                ShowData();
                UpdateRecord();
            }

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            cboxVariety.ResetText();
            txtboxWeight.Clear();
            txtboxPrice.Clear();
            NumQuantity.ResetText();
        }

        private void dgv_Inventory_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            id = Convert.ToInt32(dgv_Inventory.Rows[e.RowIndex].Cells[0].Value.ToString());
            cboxVariety.Text = dgv_Inventory.Rows[e.RowIndex].Cells[1].Value.ToString();
            txtboxWeight.Text = dgv_Inventory.Rows[e.RowIndex].Cells[2].Value.ToString();
            txtboxPrice.Text = dgv_Inventory.Rows[e.RowIndex].Cells[3].Value.ToString();
            NumQuantity.Value = Convert.ToInt32(dgv_Inventory.Rows[e.RowIndex].Cells[4].Value.ToString());
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (cboxVariety.Text == "" || txtboxWeight.Text == "" || txtboxPrice.Text == "" || NumQuantity.Text == "")
            {
                MessageBox.Show("Double click the cell of the specific product you want to update.");
            }
            else
            {
                con = new SqlConnection(@"Data Source=LAPTOP-KA7UGSG3;Initial Catalog=imsysdb;Integrated Security=True");
                con.Open();
                cmd = new SqlCommand("UPDATE Inventory SET Variety=@Variety, Weight=@Weight, Price=@Price, Quantity=@Quantity, TotalPrice=@TotalPrice WHERE Id='" + id + "'", con);


                decimal weight = Convert.ToDecimal(txtboxWeight.Text);
                decimal price = Convert.ToDecimal(txtboxPrice.Text);
                int quantity = Convert.ToInt32(NumQuantity.Text);
                decimal totalPrice = weight * price * quantity;

                cmd.Parameters.AddWithValue("@Variety", cboxVariety.SelectedItem.ToString());
                cmd.Parameters.AddWithValue("@Weight", weight);
                cmd.Parameters.AddWithValue("@Price", price);
                cmd.Parameters.AddWithValue("@Quantity", quantity);
                cmd.Parameters.AddWithValue("@TotalPrice", totalPrice);
                cmd.ExecuteNonQuery();
                ShowData();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            con = new SqlConnection(@"Data Source=LAPTOP-KA7UGSG3;Initial Catalog=imsysdb;Integrated Security=True");
            con.Open();
            cmd = new SqlCommand("DELETE FROM Inventory", con);
            cmd.ExecuteNonQuery();
            ShowData();
        }
    }
}

标签: c#

解决方案


您没有使用 SqlConnection。您需要将它传递给 SqlCommand 的构造函数,如下所示:

 cmd = new SqlCommand("UPDATE Report set Quantity=x.q from (SELECT Variety, SUM(Quantity) as q FROM Inventory GROUP BY Variety) x where Report.Variety=x.Variety", con);

推荐阅读