データベースからデータを入力しようとしていますが、フォームが読み込まれたときとクリックdataGridView
されたときにデータを取得する必要があります..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
おり、実行時に正しく動作しますが、データベースから行を削除すると問題が発生します (クリックしてクエリを使用し、展示delete btn
をクリックしてください。refresh btn