首页 > 解决方案 > 使用 C# 计算并将其显示在 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 数据库中。用户界面: 在此处输入图像描述

标签: c#sqlsql-server

解决方案


推荐阅读