我使用以下代码购买书籍.....
在TextBox
每当我输入已售书数时,它会更新数据库中的新TextBox
值。每次书籍销售时都必须减法。但是,它会更新新值。
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
sql = new SqlConnection(@"Data Source=PC-PC\PC;Initial Catalog=Anbar;Integrated Security=True");
adapter = new SqlDataAdapter("select * from Goods", sql);
cmd = new SqlCommand();
cmd.Connection = sql;
cmd.CommandText = ("Update Goods set Buy =@Buy, Remain =@Remain where GoodsNumber =@GoodsNumber");
cmd.Parameters.AddWithValue("@Buy", Convert.ToInt32(textBox1.Text));
cmd.Parameters.AddWithValue("@GoodsNumber", Convert.ToInt32(comboBox1.Text));
cmd.Parameters.AddWithValue("@Remain", Convert.ToInt32(comboBox3.Text) - Convert.ToInt32(textBox1.Text));
sql.Open();
cmd.ExecuteNonQuery();
sql.Close();
fill();
}
}
它应该显示整本书的剩余部分。
答案 0 :(得分:0)
我认为问题是您在更新数据库中的剩余计数后不会更新comboBox3
。
顺便问一下你为什么要使用组合框?此外,数据库中的列名非常混乱:Buy,GoodsNumber和Remain。很难猜出那里存储的数据。考虑诸如ProductId,Name和Quantity之类的名称。
如果我要实现类似的东西,我就开始将产品加载到组合框:
private void Form_Load(object sender, EventArgs e)
{
productsComboBox.DataSource = GetAllProducts();
productsComboBox.DisplayMember = "Name";
productsComboBox.ValueMember = "Id";
}
private IList<Product> GetAllProducts()
{
List<Product> products = new List<Product>();
// I use ConfigurationManager from System.Configuration.dll
// to read connection strings from App.config
string connectionString = ConfigurationManager.ConnectionStrings["anbar"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
string query = "SELECT * FROM Products";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
products.Add(new Product() { Id = (int)reader["Id"],
Name = (string)reader["Name"],
Quantity = (int)reader["Quantity"] });
}
}
return products;
}
产品很简单:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
}
选择的产品更改后,显示其详细信息:
private void ProductsComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
Product product = productsComboBox.SelectedItem as Product;
qtyTextBox.Text = product.Quantity.ToString();
// I use NumericUpDown control to input numbers
// Minimum is set to 1
qtyToSellNumericUpDown.Maximum = product.Quantity;
}
最后 - 当我输入数量出售(1 ...当前数量)时,我会遵循:
private void ButtonSell_Click(object sender, EventArgs e)
{
Product product = productsComboBox.SelectedItem as Product;
int qtyToSell = (int)qtyToSellNumericUpDown.Value;
SellProduct(product, qtyToSell);
product.Quantity -= qtyToSell; // update product
qtyTextBox.Text = product.Quantity.ToString(); // update current quantity
}
private void SellProduct(Product product, int qtyToSell)
{
string connectionString = ConfigurationManager.ConnectionStrings["anbar"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
string query = "UPDATE Products SET Quantity = @Quantity WHERE Id = @Id";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@Id", product.Id);
cmd.Parameters.AddWithValue("@Quantity", product.Quantity - qtyToSell);
conn.Open();
cmd.ExecuteNonQuery();
}
}
多数民众赞成。是的,有很多事情需要改进,但这个小样本有效。
更新:如何将连接字符串添加到App.Config
<configuration>
<connectionStrings>
<add name="anbar"
connectionString="Data Source=PC-PC\PC;Initial Catalog=Anbar;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>