【发布时间】:2014-03-30 10:56:11
【问题描述】:
我正在使用 c# 为 cachier 开发一个程序。该程序使用 ms 访问数据库。不知何故,数据库没有进入表中。我的问题在哪里?这是代码:
private void buttonCloseCart_Click(object sender, EventArgs e) { 连接.ConnectionString = connString; 连接.打开(); 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();
}
【问题讨论】:
-
"程序为每张收据创建新表" 您可能想考虑一下这个问题,或者阅读一本有关数据库设计的书。这听起来像是一个“坏主意”(tm)。
-
好吧,你的解决方案是什么?在一张表中输入所有收据?您应该知道每张收据都包含几个字段,例如 ItemName、ItemPrice、Quantity 和 CartSum……朋友有什么建议吗?
-
是的,读一本关于数据库设计的好书。每条记录存储多个字段正是表的用途和解释数据库设计确实超出了此问答网站的范围。
-
那么同时,获取数据库中表数量的语法是什么?
-
您真的非常需要阅读有关关系数据库管理系统 (RDBMS) 的文章,以便牢牢掌握表、字段、键、索引等的概念。了解手头的知识,阅读有关数据库设计的内容,以便您了解数据建模和物理对象作为数据库对象的表示。