我正在尝试dataGridView
用数据库中的数据填充 a,它必须在加载表单和refreshButton
单击时获取数据。
这是代码:
public partial class PhoneBookMainWindow : Form
{
static public string connString = "Server=(local); Database=PhoneBook; Trusted_Connection=TRUE";
public SqlConnection connection = new SqlConnection(connString);
private void btnRefreshPhoneBook_Click(object sender, EventArgs e)
{
SqlCommand command = new SqlCommand("SELECT ID, contactName, jobTitle, currentAddress, workAddress, workPhone, cellPhone FROM ContactsInformations", connection);
try
{
SqlDataAdapter dataAdapter = new SqlDataAdapter();
dataAdapter.SelectCommand = command;
DataTable dataSet = new DataTable();
dataAdapter.Fill(dataSet);
BindingSource bindingSrc = new BindingSource();
bindingSrc.DataSource = dataSet;
dataGridView1.DataSource = bindingSrc;
dataAdapter.Update(dataSet);
}
catch (Exception x)
{
MessageBox.Show(x.Message);
throw;
}
}
}
Form loading
我在&中使用相同的代码,btn clicking
它们在执行中正常工作,但是当我从数据库中删除一行时出现问题(通过单击 a 使用查询delete btn
然后单击refresh btn
展览来到我面前。