数据不显示在 MS 访问上
本文关键字:访问 MS 显示 数据 | 更新日期: 2023-09-27 17:55:11
我正在C#(Winform)上编写代码。 该程序是关于Cachier的,数据库是女士访问。当我将数据输入数据库时,似乎输入了数据,但是当我打开 MS Access 时,表是空的。虽然,如果我右键单击Visual Studio中的"预览数据集",我可以看到数据。这是我到目前为止对数据库的代码:
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");
}
}
}
要通过 OleDbDataAdapter 从数据库存储中获取任何行,您需要使用包含 SELECT 语句的命令设置其 SelectCommand
da.SelectCommand = new OleDbCommand("SELECT * FROM Receipts", connect);
DataTable dt = new DataTable();
da.Fill(dt);
实际上,您正在使用与选择命令相同的命令来插入数据。显然,它不会返回记录。表中应有重复记录。
我会对您的代码进行更改。如果您有多条记录要添加到表中(那里有一个循环),那么在每个循环中从数据库中提取数据是没有意义的。我会在循环外调用表的 Fill。此外,在进入循环之前,只需定义一次 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();
}