我显示组成产品的项目库存,用户必须能够在选择产品后在DataGridView中为每个项目更新这些项目的大小和数量。
我创建DataGridViews和事件以允许以编程方式更新数据库,因为每个产品最多可以包含1个项目(最多5个项目)。
SqlCommand stockCommand;
SqlDataAdapter stockAdapter;
SqlCommandBuilder stockBuilder;
DataSet stockDs;
DataTable stockTable;
private void DisplayItems()
{
string queryItems = "SELECT id_item, id_PF, Name, Type FROM Items WHERE id_PF = "+ PF_id + " AND Type = 'BE'";
using (SqlConnection con = new SqlConnection(conStringLocal))
{
using (SqlCommand cmdStock = new SqlCommand(queryItems, con))
{
int i = 0;
con.Open();
SqlDataReader readerStock = cmdStock.ExecuteReader();
while (readerStock.Read())
{
string itemName = readerStock["Name"].ToString();
DisplayItemsStock(i, itemName);
i++;
}
}
}
}
private void DisplayItemsStock(int i, string item)
{
DataGridView stock = new DataGridView();
stock.KeyDown += new KeyEventHandler(stock_KeyDown);
string queryItemStock = "SELECT id_stock, item_name, size, quantity FROM Stock WHERE item_name = '" + item + "'";
SqlConnection con = new SqlConnection(conStringLocal);
stockCommand = new SqlCommand(queryItemStock con);
stockAdapter = new SqlDataAdapter(stockCommand);
stockBuilder = new SqlCommandBuilder(stockAdapter);
stockDs = new DataSet();
stockAdapter.Fill(stockDs, "stock");
stockTable = stockDs.Tables["stock"];
con.Close();
stock.DataSource = stockTable;
panelStock.Controls.Add(stock);
}
private void stock_KeyDown(object sender, KeyEventArgs e)
{
DataGridView stock = (DataGridView)sender;
if (e.Keycode == Keys.Enter)
{
// Check different conditions and update if everything is good
using (SqlConnection con = new SqlConnection(conStringLocal))
{
con.Open();
stockAdapter.Update(stockTable);
MessageBox.Show("Saved changes");
}
}
}
仅当我需要在所有最后创建的DataGridView上运行Update命令时,才发生更新命令。
答案 0 :(得分:1)
我相信您会遇到此问题,因为这些变量在navigator.mediaDevices.getUserMedia({
audio: {
deviceId: devices[0].deviceId
}
});
之间共享。
DataGridViews
例如,当为第一个项目创建SqlCommand stockCommand;
SqlDataAdapter stockAdapter;
SqlCommandBuilder stockBuilder;
DataSet stockDs;
DataTable stockTable;
时,使用对该项目的查询将DataGridView
设置为新的stockCommand
。适配器,构建器,数据集和表也由此创建。为下一项创建SqlCommand
时,就会出现问题。现在,将使用下一个项目的查询创建一个新的DataGridView
对象。同样,将适配器,构建器,数据集和表都设置为下一项的新对象。他们不再使用查询的第一项。
现在,当按键事件发生时,它会使用stockCommand
和stockTable
来查询最后一项。因此,最后一项是唯一更新的项。
您可以通过为每个stockAdapter
创建并保留单独的命令,适配器,构建器,数据集和数据表变量来避免此问题。您可以使用DataGridView
来做到这一点,或者我可以使用新的类来做到这一点。
Dictionary
public class StockItem
{
private const string conStringLocal = "Data Source=TestDatabase.sqlite;Version=3;";
private readonly SqlCommand stockCommand;
private readonly SqlDataAdapter stockAdapter;
private readonly SqlCommandBuilder stockBuilder;
private readonly DataSet stockDs;
private readonly DataTable stockTable;
public DataGridView StockDataGridView { get; }
public StockItem(string item)
{
StockDataGridView = new DataGridView();
StockDataGridView.KeyDown += new KeyEventHandler(stock_KeyDown);
string queryItemStock = "SELECT id_stock, item_name, size, quantity "
+ "FROM Stock WHERE item_name = '" + item + "'";
SqlConnection con = new SqlConnection(conStringLocal);
stockCommand = new SqlCommand(queryItemStock, con);
stockAdapter = new SqlDataAdapter(stockCommand);
stockBuilder = new SqlCommandBuilder(stockAdapter);
stockDs = new DataSet();
stockAdapter.Fill(stockDs, "Stock");
stockTable = stockDs.Tables["Stock"];
con.Close();
StockDataGridView.DataSource = stockTable;
}
private void stock_KeyDown(object sender, KeyEventArgs e)
{
DataGridView stock = (DataGridView)sender;
if (e.KeyCode == Keys.Enter)
{
// Check different conditions and update if everything is good
using (SqlConnection con = new SqlConnection(conStringLocal))
{
con.Open();
stockAdapter.Update(stockTable);
MessageBox.Show("Saved changes");
}
}
}
}
函数保持不变。 DisplayItems
变为:
DisplayItemsStock