从ms访问中获取表的数量

时间:2014-03-30 10:56:12

标签: c# database

我正在使用c#为cachier开发一个程序。该程序使用ms访问数据库。不知何故,数据库没有输入到表中。我的问题在哪里?这是代码:

private void buttonCloseCart_Click(object sender,EventArgs e)         {             connect.ConnectionString = connString;             connect.Open();             OleDbCommand cmd1 = new OleDbCommand(“INSERT INTO ReceiptItem(ItemID,Quantity,UnitPrice)VALUES(@itemID,@ temp_item_quantity,@ temp_item_price)”,connect);             cmd1.Parameters.Add(“@ temp_item”,OleDbType.Char,20);             cmd1.Parameters.Add(“@ temp_item_quantity”,OleDbType.Integer,20);             cmd1.Parameters.Add(“@ temp_item_price”,OleDbType.Double,20);             cmd1.Parameters.Add(“@ cart_sum”,OleDbType.Double,20);             cmd1.Parameters.Add(“@ itemID”,OleDbType.Integer,20);

        for (int i = 0; i < baught_items.Count; i++)
        {
            /*Set temporary reference to the objects located on the List*/ 
            string temp_item = baught_items[i].ToString();
            int temp_item_quantity = baught_items_quantity[i];
            double temp_item_price = baught_items_price[i];
            double temp_total_item_price = total_items_price[i];
            /*Set the connection to the CachierPro DataBase*/

            OleDbConnection myconnection = new OleDbConnection(connect.ConnectionString);
            myconnection.Open();
            //OleDbConnection myconnection1 = new OleDbConnection(connect.ConnectionString);
            //myconnection1.Open();
            /*********************************************/
            /*This Action get the Item Name directly from the database according to the item name in the cart*/
            //OleDbConnection dataConnection = new OleDbConnection();
            //dataConnection.ConnectionString = connString;
            //dataConnection.Open();
            //OleDbConnection oledbConn = new OleDbConnection(connString);
            //oledbConn.Open();
            //OleDbCommand myCommand1 = new OleDbCommand();
            //myCommand1.Connection = myconnection1;
            //myCommand1.Connection.Close();
            OleDbCommand cmd = new OleDbCommand("SELECT [ItemID] FROM ItemsList WHERE [ItemName] = " + "'"+temp_item +"'" , connect);
            OleDbDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read()) { }
            int itemID = dataReader.GetInt32(0);
            /********************************************/
            /*Initialize the Command ment for the Query*/
            OleDbCommand myCommand = new OleDbCommand();
            myCommand.Connection = myconnection;
            //myCommand.ExecuteNonQuery();
            myCommand.Connection.Close();
            //dataConnection.Close();
            //oledbConn.Close();
            myconnection.Close();
            dataReader.Close();
            /*Enter the first query*/


            if (connect.State == ConnectionState.Open)
            {

                try
                { int addedCount = cmd.ExecuteNonQuery(); }

                catch (Exception expe)
                { MessageBox.Show(expe.Message); connect.Close(); }
            }

            else
            {
                MessageBox.Show("Connection Failed");
            }
        }

        OleDbDataAdapter da = new OleDbDataAdapter();
        da.SelectCommand = new OleDbCommand("SELECT * FROM ReceiptItem", connect);
        DataTable dt = new DataTable();
        da.Fill(dt);
        textBoxCurrentCartSumTXT.Clear();
        textBoxPricePerOneTXT.Clear();
        textBoxQuantityTXT.Clear();
        textBoxSumForCurrentItemTXT.Clear();
        connect.Close();


        }

1 个答案:

答案 0 :(得分:0)

产品

项目Id
项目名称
商品价格

Recipet

ReciptID
ReciptInvoiceNo
ReciptDateTime

ReciptItem

ReciptID
项目ID
数量
UnitPrice