0

私は本を​​購入するために次のコードを使用します。.....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();
        }
    }

合計の残りの本を表示する必要があります。

4

1 に答える 1

0

comboBox3問題は、データベースの残りの数を更新した後に更新しないことだと思います。

ところで、なぜこれにコンボボックスを使用するのですか? また、データベースには、Buy、GoodNumber、および 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>
于 2012-05-03T20:49:58.170 に答える