C# .Net Framework迁移MySQL后货架添加异常及列表不更新问题
问题概述
- 从Access迁移至MySQL后,点击
btn_Ekle按钮时,货架已成功写入数据库,但按钮点击事件内的后续代码无法执行; RafListesi()方法在程序启动后首次调用可正常列出货架,但添加货架后关闭窗口再重新打开,货架列表无法刷新显示,必须重启程序才能加载新数据。
RafEkle 方法
public static ClassRaf RafEkle(string ad) { ClassRaf result; try { if (ClassData.RafSec(ad) != null) { MessageBox.Show("Eklemek İstediğiniz Raf Zaten Kayıtlarda Mevcut.", "Kütüphane Sistemi | Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information); result = null; } else { MySqlCommand sqlCommand = new MySqlCommand("INSERT INTO bookrack (shelf_name) VALUES (@shelf_name)", ClassData.connectSql); sqlCommand.Parameters.Add("@shelf_name", MySqlDbType.VarChar).Value = ad; if (sqlCommand.ExecuteNonQuery() > 0) { MySqlCommand sqlCommand1 = new MySqlCommand("SELECT * FROM bookrack ORDER BY id DESC", ClassData.connectSql); MySqlDataReader sqlDataReader = sqlCommand1.ExecuteReader(); if (sqlDataReader.Read()) { result = ClassData.RafSec((int)sqlDataReader["id"]); } else { result = null; } } else { result = null; } } } catch { result = null; } return result; }
btnEkle_Click 事件
private void btnEkle_Click(object sender, EventArgs e) { ClassRaf classRaf = ClassData.RafEkle(txtEkle.Text); if (classRaf != null) { txtEkle.Text = ""; liste.Items.Add(classRaf.Ad); ClassLog.Log(string.Concat(new object[] { classRaf.Ad, " adlı raf eklendi. ID=[", classRaf.ID, "]" })); ShelfValid(); txtEkle.Select(); } }
RafListesi 方法
public static List<ClassRaf> RafListesi() { List<ClassRaf> result; try { List<ClassRaf> list = new List<ClassRaf>(); MySqlCommand sqlCommand = new MySqlCommand("SELECT * FROM bookrack", ClassData.connectSql); MySqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); while (sqlDataReader.Read()) { list.Add(new ClassRaf { ID = Convert.ToInt32(sqlDataReader["id"]), Ad = sqlDataReader["shelf_name"].ToString() }); } result = list; sqlDataReader.Close(); sqlDataReader.Dispose(); } catch { result = new List<ClassRaf>(); } return result; }
内容的提问来源于stack exchange,提问作者Burak
相关产品推荐
相关产品推荐

