在MS Access数据库中进行计算的更简单方法?

时间:2019-07-06 09:15:36

标签: c# database ms-access-2007

我在MS Access中有一个数据库,并且有一个程序减去手头数量,该数量是数据库表中的一列。

当用户出售商品时,必须扣除现有数量。因此,我想知道最快的方法,因为现在我的程序从数据库中获取现有数量,然后减去程序中的数量,然后更新数据库,如果有更多项目需要花费一些时间。立即出售。

这是我现在正在使用的代码

// SALE STARTS HERE
int customerID;  

OleDbConnection objconn = new OleDbConnection(conn);
objconn.Open();

OleDbConnection objconn2 = new OleDbConnection(Con_Str);
objconn2.Open();

OleDbCommand cmd3 = new OleDbCommand("insert into CUSTOMERINVOICE (INVOICEDATE, INVOICESTATUS, ISDEBTORINVOICE, INVOICETYPE)" + "VALUES('"+System.DateTime.Now.ToShortDateString()+",0,0,1)", objconn2);
cmd3.ExecuteNonQuery();

OleDbDataAdapter ada2 = new OleDbDataAdapter("SELECT TOP 1 ID FROM CUSTOMERINVOICE ORDER BY ID DESC", objconn2);

DataTable dt2 = new DataTable();
ada2.Fill(dt2);

DataRow dr2 = dt2.Rows[0];
customerID = Int32.Parse(dr2["ID"].ToString());

foreach(DataGridViewRow row in dgSaleBasket.Rows)
{
    OleDbCommand cmd4 = new OleDbCommand("INSERT INTO  CUSTOMERINVOICEDETAIL (INVOICEID, ITEMCODE, DESCRIPTION, QUANTITY, PRICE)" + "VALUES(" + customerID + ",'"+row.Cells[0].Value.ToString()+"','"+row.Cells[1].Value.ToString()+"',"+row.Cells[2].Value.ToString()+",'"+row.Cells[4].Value.ToString()+"')", objconn2);
    cmd4.ExecuteNonQuery();

    OleDbDataAdapter ada5 = new OleDbDataAdapter("SELECT CODE, Onhand FROM STMAST.DBF where CODE ='" + row.Cells[0].Value + "'", objconn);

    DataTable dt5 = new DataTable();
    ada5.Fill(dt5);

    int onhand;
    int quantityPurchased = Int32.Parse(row.Cells[2].Value.ToString());
    int newOnhand;

    for (int i = 0; i < dt2.Rows.Count; i++)
    {
        DataRow dr5 = dt5.Rows[i];
        onhand = Int32.Parse(dr2["onhand"].ToString());
        newOnhand = onhand - quantityPurchased;

        OleDbCommand cmd6 = new OleDbCommand("UPDATE stmast SET onhand =" +newOnhand+ " WHERE code =" + row.Cells[0].Value, objconn2);
        cmd6.ExecuteNonQuery();
    }
}

0 个答案:

没有答案