使用C#计算并显示在SQL Server数据库中

时间:2019-03-24 13:51:49

标签: c# sql sql-server

因此,基本上,我想在textboxTotalamount中计算产品的总量并将其显示在数据网格视图(或sql数据库中)中 我必须编写代码,但不确定它是否正确以及在哪里放置这也是一个困惑。我将把我的代码放在下面。

public partial class OrderDetails : Form
{
    SqlConnection con = new SqlConnection(@"Data Source = INFINITY; Initial Catalog = Stock; Integrated Security = True"); // making connection   
    SqlCommand cmd;
    SqlDataAdapter adapt;
    //ID variable used in Updating and Deleting Record  
   int OrderID = 0;                                                                                                                 

    public OrderDetails()
    {
        InitializeComponent();
    }
    private void OrderDetails_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'stockDataSet.OrderDetails' table. You can move, or remove it, as needed.
       this.orderDetailsTableAdapter.Fill(this.stockDataSet.OrderDetails);

        // my code for calculating total amount
        int Qty = Convert.ToInt32(txtquantity.Text);
        decimal Unit_Price = Convert.ToDecimal(txtcost.Text);
        decimal Tax = Convert.ToDecimal(txttax.Text);
        decimal TotalAmount = (Qty * Unit_Price);
        decimal PercentageAmount = ((Unit_Price * Tax) / 100);
        TotalAmount = TotalAmount + PercentageAmount;
        txttotal.Text = TotalAmount.ToString();

    }

    private void btnorder_Click(object sender, EventArgs e)
    {


        //ud variable used in Updating and Deleting Record  
        try
        {
            cmd = new SqlCommand(@"INSERT INTO [dbo].[OrderDetails]([OrderName],[dateCreated],[StockID],[StockCode],[Quantity],[Cost],[TAX],[Total]) 
        VALUES(@OrderName,@dateCreated,@StockID,@StockCode,@Quantity,@Cost,@TAX,@Total)", con);


            con.Open();
            cmd.Parameters.AddWithValue("@OrderName", txtordername.Text);
            cmd.Parameters.AddWithValue("@dateCreated", SqlDbType.Date).Value = dtpdatecreated.Value.Date;
            cmd.Parameters.AddWithValue("@StockID", txtstockid.Text);
            cmd.Parameters.AddWithValue("@StockCode", txtstockcode.Text);
            cmd.Parameters.AddWithValue("@Quantity", txtquantity.Text);
            cmd.Parameters.AddWithValue("@Cost", txtcost.Text);
            cmd.Parameters.AddWithValue("@TAX", txttax.Text);
            cmd.Parameters.AddWithValue("@Total", txttotal.Text);
            if (cmd.ExecuteNonQuery() > 0)
            {
                MessageBox.Show("Record inserted successfully");
                DisplayData();
                ClearData();
            }
            else
            {
                MessageBox.Show("Record failed");
            }

        }
        catch (SqlException ex)
        {
            MessageBox.Show("Error during insert: "  + ex);
        }
        finally
        {
            con.Close();
        }
    }

    private void DisplayData()
    {

        DataTable dt = new DataTable();
        adapt = new SqlDataAdapter("SELECT * FROM [dbo].[OrderDetails]", con);
        adapt.Fill(dt);
        dataGridView1.DataSource = dt;
        con.Close();
    }
    private void ClearData()
    {
        txtordername.Text = "";
        txtstockid.Text = "";
        txtstockcode.Text = "";
        txtquantity.Text = "";
        txtcost.Text = "";
        txttax.Text = "";
        txttotal.Text = "";
        OrderID = 0;
    }


    private void orderDetailsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
        this.Validate();
        this.orderDetailsBindingSource.EndEdit();
        this.tableAdapterManager.UpdateAll(this.stockDataSet);

    }

}

我希望我的总金额在“总金额”文本框中计算并也保存在sql数据库中。 用户界面 : enter image description here

0 个答案:

没有答案