【发布时间】:2019-11-08 03:35:32
【问题描述】:
我正在使用数据绑定从 LocalDB 加载组合框项目,而无需任何编码。 image
我正在使用下面的代码插入新的 item_name。
using (SqlConnection myConn = new SqlConnection(connectionString))
{
myConn.Open();
string Query = "INSERT INTO items (item_name, generic) VALUES ('" + NewItemNameBox.Text + "', '" + SelectGenericNewItemBox.Text + "');";
using (SqlCommand SQLcmd = new SqlCommand(Query, myConn))
{
SQLcmd.ExecuteNonQuery();
}
myConn.Close();
}
代码正在运行。我的问题是在向数据库添加新项目后,组合框没有更新。我需要在组合框中显示新添加的项目名称。当我重新打开我的应用程序时它正在工作。不重新打开怎么办?就像使用按钮...
【问题讨论】:
-
试试
BindingSource.ResetBindings(true) -
感谢您的帮助。这段代码为我做。
this.itemsTableAdapter.Fill(this.mainDBDataSet.items); -
SQL Injection alert - 您应该不将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入 - 查看Little Bobby Tables
-
亲爱的@mark_s 感谢您的警告。我在本地 PC 上使用这个应用程序
标签: c# sql data-binding combobox localdb