ms访问时未显示数据

时间:2014-03-30 08:34:03

标签: c# database ms-access

我在c#(winform)上编写代码。该程序是关于cachier和数据库是 ms访问。 当我输入数据到数据库时,似乎输入了数据,但是当我打开ms访问时,表是空的。 althogh,如果我右键点击预览数据集'在视觉工作室,我可以看到数据。 到目前为止,我的代码是关于数据库的:

    private void buttonCloseCart_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < baught_items.Count; i++)
        {
            connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\אורון\documents\visual studio 2012\Projects\CachierPro\CachierPro\CachierProDB.accdb";
            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];
            connect.Open();
            OleDbCommand cmd = new OleDbCommand("INSERT INTO Receipts (ItemName, Quantity, PricePerOne, Total) VALUES (@temp_item, @temp_item_quantity, @temp_item_price, @temp_total_item_price)", connect);
            if (connect.State == ConnectionState.Open)
            {
                cmd.Parameters.Add ("@temp_item", OleDbType.Char, 20).Value = temp_item;
                cmd.Parameters.Add("@temp_item_quantity", OleDbType.Integer, 20).Value = temp_item_quantity;
                cmd.Parameters.Add("@temp_item_price", OleDbType.Double, 20).Value = temp_item_price;
                cmd.Parameters.Add("@cart_sum", OleDbType.Double,20).Value = temp_total_item_price;

                try
                {
                    cmd.ExecuteNonQuery();
                    OleDbDataAdapter da = new OleDbDataAdapter();
                    da.SelectCommand = cmd;
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    MessageBox.Show("Data Added To DataBase");
                    textBoxCurrentCartSumTXT.Clear();
                    textBoxPricePerOneTXT.Clear();
                    textBoxQuantityTXT.Clear();
                    textBoxSumForCurrentItemTXT.Clear();
                    connect.Close();
                }
                catch (Exception expe)
                {
                    MessageBox.Show(expe.Source);
                    connect.Close();
                }
            }
            else
            {
                MessageBox.Show("Connection Failed");
            }
        }


    }

1 个答案:

答案 0 :(得分:1)

要通过OleDbDataAdapter从数据库存储中获取任何行,您需要使用包含SELECT语句的命令设置其SelectCommand

da.SelectCommand = new OleDbCommand("SELECT * FROM Receipts", connect);
DataTable dt = new DataTable();
da.Fill(dt);

实际上,您正在使用与用于INSERT数据相同的命令,因为它是SelectCommand。显然它不会返回记录。您的表格中应该有重复的记录。

我会改变你的代码。如果你有多个记录要添加到你的表中(你有一个循环)那么在每个循环中从db中提取数据是没有意义的。我会在循环外调用表的填充。在进入循环之前,只需一次定义OleDbCommand及其参数,就可以获得一点性能提升。在循环内部只需更新参数值并调用ExecuteNonQuery

    private void buttonCloseCart_Click(object sender, EventArgs e)
    {
        connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\אורון\documents\visual studio 2012\Projects\CachierPro\CachierPro\CachierProDB.accdb";
        connect.Open();
        OleDbCommand cmd = new OleDbCommand(@"INSERT INTO Receipts 
        (ItemName, Quantity, PricePerOne, Total) 
         VALUES (@temp_item, @temp_item_quantity, 
         @temp_item_price, @temp_total_item_price)", connect);

         cmd.Parameters.Add ("@temp_item", OleDbType.Char, 20);
         cmd.Parameters.Add("@temp_item_quantity", OleDbType.Integer, 20);
         cmd.Parameters.Add("@temp_item_price", OleDbType.Double, 20);
         cmd.Parameters.Add("@cart_sum", OleDbType.Double,20);

        for (int i = 0; i < baught_items.Count; i++)
        {
            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];
            if (connect.State == ConnectionState.Open)
            {
                cmd.Parameters["@temp_item"].Value = temp_item;
                cmd.Parameters["@temp_item_quantity"].Value = temp_item_quantity;
                cmd.Parameters["@temp_item_price"].Value = temp_item_price;
                cmd.Parameters["@cart_sum"].Value = temp_total_item_price;
                try
                {
                    int addedCount = cmd.ExecuteNonQuery();
                    if(addedCount == 0)
                    {
                       ... problems here, record not added for some reasons
                    }
                }
                catch (Exception expe)
                {
                    MessageBox.Show(expe.Source);
                    connect.Close();
                }
            }
            else
            {
                MessageBox.Show("Connection Failed");
            }
        }

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