数据库更新后,调用DataAdapter,DataSet和DataGridView显示数据库
问题描述:
public void ComonDataView_order_info()
{
try
{
DBConnect();
//连接数据库成功后的操作
//创建DataAdapter对象
SqlDataAdapter order_info_da = new SqlDataAdapter("select * from 订单详情", sqlCon);
//创建数据集(也可以直接利用.NET的DataSet 数据适配器控件)
DataSet order_info_ds = new DataSet();
order_info_da.AcceptChangesDuringFill = true;
//Fill方法填充
order_info_da.Fill(order_info_ds);
order_info_da.Update(order_info_ds);
//将DataSet数据集绑定到DataGridView
dataGridView1.AllowUserToAddRows = true;
dataGridView1.DataSource = order_info_ds.Tables[0];
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
//绑定数据源
dataGridView1.DataSource = order_info_ds.Tables[0].DefaultView;
}
catch (SystemException ex)
{
//连接数据库失败提示
MessageBox.Show("错误:" + ex.Message, "错误提示",
MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
finally
{
//如果处于与数据库连接状态
if (sqlCon.State == ConnectionState.Open)
{
//关闭SQL连接
sqlCon.Close();
//释放所占用的资源
sqlCon.Dispose();
}
}
}
如何实现每调用一次ComonDataView_order_info(),DataGridView就显示一次新的数据库内容?
为什么每插入一次数据,调用一次函数,但是只显示最后一次,其他时候都没有显示?
答
添加一个DataGridView.Refresh(),问题就解决了
答
是不是代码执行很快 看不出来
你在插入后更新一次然后Thread.sleep(2000);再插入再更新再Thread.sleep(2000); 看一下是否更新了
如果没有的话代码执行非常快 更不看不出阿狸的