我想输入数据然后更新数据库,现在遇到的问题比较古怪,我在VS2010 按F5调试后,输入数据再显示数据库,刚输入的数据更新在数据库中了,停止调试再调试还是在的,但是我关闭VS2010后,再调试就没了。意思就是无法真正更新的数据源中。我用了两个办法都不行
一个是用sql语句“INSERT INTI WordsTalbe1 Values("","","")”
另一个用了sqldataadapter
两个都是上述情况,代码如下。
private void button1_Click(object sender, EventArgs e)
{
conn = new SqlConnection(Properties.Settings.Default.WordsDataConnectionString1);
adapter = new SqlDataAdapter("select * from WordsTable1", conn);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.InsertCommand = builder.GetInsertCommand();
//adapter.DeleteCommand = builder.GetDeleteCommand();//这两句报错
//adapter.UpdateCommand = builder.GetUpdateCommand();
table = new DataTable();
adapter.Fill(table);
dataGridView1.DataSource = table;
} private void button2_Click(object sender, EventArgs e)
{
DataRow row = table.NewRow();
row[0] = textBoxMeaning.Text.ToString();
row[1] = textBoxWords.Text.ToString();
row[2] = textBoxTime.Text.ToString();
table.Rows.Add(row);
} private void button3_Click(object sender, EventArgs e)//更新,关闭VS后再看就没用了
{
dataGridView1.EndEdit();
try
{
adapter.Update(table);
}
catch (SqlException er)
{
MessageBox.Show(er.Message);
}
}
一个是用sql语句“INSERT INTI WordsTalbe1 Values("","","")”
另一个用了sqldataadapter
两个都是上述情况,代码如下。
private void button1_Click(object sender, EventArgs e)
{
conn = new SqlConnection(Properties.Settings.Default.WordsDataConnectionString1);
adapter = new SqlDataAdapter("select * from WordsTable1", conn);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.InsertCommand = builder.GetInsertCommand();
//adapter.DeleteCommand = builder.GetDeleteCommand();//这两句报错
//adapter.UpdateCommand = builder.GetUpdateCommand();
table = new DataTable();
adapter.Fill(table);
dataGridView1.DataSource = table;
} private void button2_Click(object sender, EventArgs e)
{
DataRow row = table.NewRow();
row[0] = textBoxMeaning.Text.ToString();
row[1] = textBoxWords.Text.ToString();
row[2] = textBoxTime.Text.ToString();
table.Rows.Add(row);
} private void button3_Click(object sender, EventArgs e)//更新,关闭VS后再看就没用了
{
dataGridView1.EndEdit();
try
{
adapter.Update(table);
}
catch (SqlException er)
{
MessageBox.Show(er.Message);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货