在C#中显示MS Access数据库时出错 [英] Error in displaying MS Access Database in C#
问题描述
我想我在这里是一个不包含很多细节的人,对此我感到很抱歉.因此,这次我将尽力为我的问题提供更多信息.
I think I'm know here as a person who doesn't include a lot of details which I'm sorry about so this time I'll try to be more informative with my problem.
注意:如果您要问为什么我不使用异常处理,那是因为我想在处理异常之前使用代码.
最近,我们被教导使用MS Access创建基本数据库.所以我的问题是这个.使用此代码将我的数据库显示到我的listView:
As of recent we were taught using MS Access to create a basic databases. So my problem is this. Using this code to display my Database to my listView:
private void Form1_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source =AssetManagement.accdb");//accessItems is the database file
System.Data.OleDb.OleDbDataAdapter adpt = new System.Data.OleDb.OleDbDataAdapter("select * from tbl_Assets", con);
adpt.Fill(ds);
DataTable table = ds.Tables[0];
foreach (DataRow row in table.Rows)
{
lstViewListOfRooms.Items.Add(row[1].ToString()).SubItems.Add(row[2].ToString());
for (int i = 0; i < lstViewListOfRooms.Items.Count; i++)
{
lstViewListOfRooms.Items[i].SubItems.Add(row[3].ToString());
lstViewListOfRooms.Items[i].SubItems.Add(row[4].ToString());
}
}
这是为了以另一种形式为我的数据库添加新项目:
and this for adding new items for my database from a different form:
private void btnSaveAddAsset_Click(object sender, EventArgs e)
{
if (txtAddFloor.Text == "" || txtAddRoom.Text == "" || string.IsNullOrWhiteSpace(txtAddFloor.Text) == true || string.IsNullOrWhiteSpace(txtAddRoom.Text) == true)
{
MessageBox.Show("Please enter valid information", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
}
else
{
ths.lstViewListOfRooms.Items.Add(txtAddFloor.Text).SubItems.Add(txtAddRoom.Text);
for (int i = 0; i < ths.lstViewListOfRooms.Items.Count; i++)
{
String date = "dd/MM/yyyy - HH:mm:ss";
ths.lstViewListOfRooms.Items[i].SubItems.Add(txtAddDescriptionDetail.Text);
ths.lstViewListOfRooms.Items[i].SubItems.Add(DateTime.Now.ToString(date));
con = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source =AssetManagement.accdb");
Ds = new DataSet();
string query = "INSERT INTO tbl_Assets(asset_floor, asset_room, asset_description, asset_createdOn)" + " VALUES (" + txtAddFloor.Text + "," + txtAddRoom.Text + ", '" + txtAddDescriptionDetail.Text + "' , '" + DateTime.Now.ToString(date) + "'" + ") ";
con.Open();
Da = new OleDbDataAdapter(query, con);
Da.Fill(Ds, "tbl_Assets");
con.Close();
this.Close();
}
}
}
问题:每次添加一个项目时,不仅增加一个项目,而且每次添加的倍数越来越多.如您在此屏幕截图中所见:
The problem: Each time I add an item it doesn't just add one but more, every time I add it multiples even more and more. As you can see in this screenshot:
推荐答案
每次保存时,由于以下代码,您将每个现有房间再次添加:for (int i = 0; i < ths.lstViewListOfRooms.Items.Count; i++)
Each time you save, you are adding again every existing room because of this code: for (int i = 0; i < ths.lstViewListOfRooms.Items.Count; i++)
您应该只插入新的,并更新(如果需要)现有的.此代码将执行以下操作:
You should insert only new ones and update (if needed) the existing ones. This code will do this:
private void btnSaveAddAsset_Click(object sender, EventArgs e)
{
if (txtAddFloor.Text == "" || txtAddRoom.Text == "" || string.IsNullOrWhiteSpace(txtAddFloor.Text) == true || string.IsNullOrWhiteSpace(txtAddRoom.Text) == true)
{
MessageBox.Show("Please enter valid information", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
}
else
{
ths.lstViewListOfRooms.Items.Add(txtAddFloor.Text).SubItems.Add(txtAddRoom.Text);
String date = "dd/MM/yyyy - HH:mm:ss";
ths.lstViewListOfRooms.Items[lstViewListOfRooms.Items.Count - 1].SubItems.Add(txtAddDescriptionDetail.Text);
ths.lstViewListOfRooms.Items[lstViewListOfRooms.Items.Count - 1].SubItems.Add(DateTime.Now.ToString(date));
con = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source =AssetManagement.accdb");
Ds = new DataSet();
string query = "INSERT INTO tbl_Assets(asset_floor, asset_room, asset_description, asset_createdOn)" + " VALUES (" + txtAddFloor.Text + "," + txtAddRoom.Text + ", '" + txtAddDescriptionDetail.Text + "' , '" + DateTime.Now.ToString(date) + "'" + ") ";
con.Open();
Da = new OleDbDataAdapter(query, con);
Da.Fill(Ds, "tbl_Assets");
con.Close();
this.Close();
}
}
这篇关于在C#中显示MS Access数据库时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!