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

dataGridView控件怎么把上面修改的数据写到sql2000里面
如题,我是用dataGridView添加数据源然后直接显示
但是直接在控件上更新数据以后不知道怎么把修改的信息重新写进数据库
private void change_Click(object sender, EventArgs e)
  {
  try 
  {
  this.link_fileTableAdapter.Update(this.mybaseDataSet);
  }catch(Exception ex)
  {
  MessageBox.Show(ex.ToString());
  }
  MessageBox.Show("修改成功!");
  }
网上看了些帖子
但是执行后数据库的数据没有改变
数据库用的是MS-SQL2000
数据名用的是mybase
修改的表叫link_file
不知道是不是更新的数据集不对
新手,请指教
是在winfrom下的编程
下面是我写的查询
 private void search_Click(object sender, EventArgs e)
  {
  SqlConnection mySqlConnection = new SqlConnection("Server=localhost; Integrated Security=SSPI;Initial Catalog=mybase;");
  try
  {
  mySqlConnection.Open();
  string sql = "Select * from link_file where Link_Num='" +text.Text + " '";
  SqlDataAdapter myAD = new SqlDataAdapter(sql, mySqlConnection);
  DataSet myDS = new DataSet();
  myAD.Fill(myDS, "project");
  this.dataGridView1.DataSource = myDS.Tables[0].DefaultView;//输出表
  }
  catch (Exception oe) { MessageBox.Show(oe.Message, "数据库出错!"); }
  finally
  {
  if (mySqlConnection.State == ConnectionState.Open)
  mySqlConnection.Close();
  }
  }

------解决方案--------------------
在同一个SqlDataAdapter 的情况下,用SqlCommandBuider 然后调用
this.link_fileTableAdapter.Update(this.mybaseDataSet); 

------解决方案--------------------
private void search_Click(object sender, EventArgs e) {
 SqlConnection mySqlConnection = new SqlConnection("Server=localhost; Integrated Security=SSPI;Initial Catalog=mybase;"); 
try 

mySqlConnection.Open(); 
string sql = "Select * from link_file where Link_Num='" +text.Text + " '"; 

this.link_fileTableAdapter = new SqlDataAdapter(sql, mySqlConnection);
SqlCommandBuilder cmb = new SqlCommandBuilder(this.link_fileTableAdapter);
DataSet myDS = new DataSet(); 
myAD.Fill(myDS, "project"); 
this.dataGridView1.DataSource = myDS.Tables[0].DefaultView;//输出表 

//...
}

------解决方案--------------------
private void search_Click(object sender, EventArgs e) { 
SqlConnection mySqlConnection = new SqlConnection("Server=localhost; Integrated Security=SSPI;Initial Catalog=mybase;"); 
try 

mySqlConnection.Open(); 
string sql = "Select * from link_file where Link_Num='" +text.Text + " '"; 

this.link_fileTableAdapter = new SqlDataAdapter(sql, mySqlConnection); 
SqlCommandBuilder cmb = new SqlCommandBuilder(this.link_fileTableAdapter); 

this.link_fileTableAdapter.Fill(this.mybaseDataSet, "project"); 
this.dataGridView1.DataSource = this.mybaseDataSet.Tables[0].DefaultView;//输出表 

//... 


查询和更新使用同一个dataadapter,datase