日期:2014-05-18  浏览次数:21043 次

c# winform表格数据更新问题
现有两个窗体,form1和form2,from1上有一个表格,显示数据库中数据,一个添加按钮,点添加后弹出form2,录入添加数据,点form2的确定后,数据填入数据库,同时form1上的表格更新

现在的问题是,每次添加新数据后,更新from1的表格显示数据时,我是重新读取数据库查询一次,当数据很多时,每次录入完成更新的时间都比较长,请教各位有什么好的更新办法么?

代码如下:
 string strConn = "data source=(local);integrated security=true;persist security info=False;initial catalog=SDJJ";
  SqlConnection myConnection;
  SqlDataAdapter sda;

  private void Form1_Load(object sender, EventArgs e)
  {
  string str = "select * from table1 x,table2 y where x.ID=y.ID";
  myConnection = new SqlConnection(strConn);
  sda = new SqlDataAdapter(str, myConnection);
  DataTable dt = new DataTable();
  sda.Fill(dt);
  dataGridView1.DataSource = dt;
  }

  private void button_Add_Click(object sender, EventArgs e)
  {
  Form2 fr=new Form2();
  if (fr.ShowDialog() == DialogResult.OK)
  {
  string str = "select * from table1 x,table2 y where x.ID=y.ID";
  myConnection = new SqlConnection(strConn);
  sda = new SqlDataAdapter(str, myConnection);
  DataTable dt = new DataTable();
  sda.Fill(dt);
  dataGridView1.DataSource = dt;

  }
  }



------解决方案--------------------
from1的表格增加分页,最好是修改数据源,让每次只读当页所要的几十条数据,效率会比较高.
------解决方案--------------------
string str = "select * from table1 x,table2 y where x.ID=y.ID"; 
这种查询方法应该很慢。你可以用存储过程啊。要不优化一下查询。可以提高速度。