[英]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. 我想我在这里是一个不包含很多细节的人,对此我感到很抱歉,所以这次我将尝试对我的问题提供更多信息。
Note: If you're asking why I'm not using exception handle it's because I wanted to work with the codes before handling the exceptions. 注意:如果您要问为什么我不使用异常处理,那是因为我想在处理异常之前使用代码。
As of recent we were taught using MS Access to create a basic databases. 最近,我们被教导使用MS Access创建基本数据库。 So my problem is this. 所以我的问题是这个。 Using this code to display my Database to my listView: 使用以下代码将我的数据库显示到我的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: 如您在此屏幕截图中所见:
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++)
每次保存时, 都会由于以下代码而再次添加每个现有房间 : 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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.