c# - 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();
}
}
}
解决方案
您没有使用 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);
推荐阅读
- python - 用代码中的单词替换值,然后将其粘贴到文件中,python?
- c++ - cmake如何在C ++中将编译器标志附加到末尾
- html - 即使更改屏幕尺寸,导航选项卡仍保持在页面中心
- python - 在 Django 中上传和下载 excel 文件
- python - Calling foreign key object several times in Django without several DB requests
- dns - iRedmail:电子邮件进入 Hotmail 的垃圾邮件
- java - 如何在 Android 的 RGB 轮中找到特定颜色的 X、Y 坐标
- javascript - uikit 框架 - 进度条动画零到值
- r - R Markdown 文档中是否可以有通用常量?
- c++ - 当我们可以更有效地使用向量来实现优先级队列时,为什么使用堆来实现它