我在winform項目中有以下形式,我有一個datagridview裏面,我有一個更新按鈕,我想推它來更新datagridview更改相應的表。標籤告訴我,記錄更新成功,但是當我查詢數據庫它不起作用。請有任何想法嗎? :從gridview更新數據庫
private SqlConnection con;
private SqlCommandBuilder scbCust;
private SqlCommandBuilder scbOrd;
private DataSet dsCommon;
private SqlDataAdapter custAdapter;
private void MainForm_Load(object sender, EventArgs e)
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["EbosPr.Properties.Settings.Database1ConnectionString1"].ConnectionString);
// Creating bridge between Server and DataSet
custAdapter = new SqlDataAdapter("SELECT * FROM dbo.CustCalls", con);
// SqlCommandBuilder that can create Update commands
scbCust = new SqlCommandBuilder(custAdapter);
con.Open();
// Filling dataset by respective adapter
dsCommon = new DataSet();
custAdapter.Fill(dsCommon, "CustCalls");
// Set datagridview datasource
dataGridView1.DataSource = dsCommon.Tables["CustCalls"];
con.Close();
}
private void update_Click(object sender, EventArgs e)
{
con.Open();
dsCommon.AcceptChanges();
this.custAdapter.UpdateCommand = this.scbCust.GetUpdateCommand(true);
int rowCust = this.custAdapter.Update(dsCommon.Tables["CustCalls"]);
if (rowCust > 0)
{
lblMessage.Text = "INFO: Record updated successfully!";
}
con.Close();
}
這是app.config中
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"