加上SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);

解决方案 »

  1.   

    还是直接看msdn的示例吧  很详细了
      

  2.   

    朋友:
            SqlDataAdapter实例的UpdateCommand的用法是: 获取或设置一个 Transact-SQL 语句或存储过程,用于更新数据源中的记录。
           你上面的写法有一个严重的问题:当两个人同时访问你的程序时,更新时就要错位了,你仔细考虑一下是不是?
           建议你利用存储过程或SQL语句来更新,利用一个或几个关键字来更新
      

  3.   

    定义一个Sqlcommand对象,给它Transact-SQL 语句或存储过程,再把SqlDataAdapter的UpdateCommand指向这个Sqlcommand就OK了.
      

  4.   

    to panning:
    我利用你的那句话,倒是更新了数据,可是数据库中的其它数据也都变成同样的数据了,这是为什么呢?
    to lightnings:
    我试过好几种方法了,有一种代码如下,但提示索引出错,请予以指教 SqlConnection MyConnection=new SqlConnection();
    MyConnection.ConnectionString="server=ICELEAN;database=Score;uid=sa;pwd=1982";
    SqlDataAdapter MyAda=new SqlDataAdapter("select * from score",MyConnection);
    DataSet MyDataSet=new DataSet();
    MyAda.Fill(MyDataSet,"score");
    DataGrid1.DataSource =MyDataSet;

    DataGrid1.DataSource=dataSet11;


    DataGrid1.DataKeyField=ID;
    string ksbh,xm, zzll,wgy,ywk1,ywk2,zf;
    string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
    TextBox tb; tb = (TextBox)(e.Item.Cells[2].Controls[0]);
    ksbh= tb.Text;
    tb = (TextBox)(e.Item.Cells[3].Controls[0]);
    xm = tb.Text;
    tb = (TextBox)(e.Item.Cells[4].Controls[0]);
    zzll = tb.Text; tb = (TextBox)(e.Item.Cells[5].Controls[0]);
    wgy= tb.Text;
    tb = (TextBox)(e.Item.Cells[6].Controls[0]);
    ywk1 = tb.Text;
    tb = (TextBox)(e.Item.Cells[7].Controls[0]);
    ywk2= tb.Text;

    tb = (TextBox)(e.Item.Cells[8].Controls[0]);
    zf= tb.Text;
    DataSet1.scoreRow r;
    r = dataSet11.score.FindByID(int.Parse(key));




    r.KSBH = ksbh;

    r.XM = xm;
    r.ZZLL=System.Convert.ToInt32(zzll);
    r.WGY=System.Convert.ToInt32(wgy);
    r.YWK1=System.Convert.ToInt32(ywk1);
    r.YWK2=System.Convert.ToInt32(ywk2);
    r.ZF=System.Convert.ToInt32(zf);
    sqlDataAdapter1.Update(dataSet11);
    DataGrid1.EditItemIndex = -1;
    DataGrid1.DataBind();
      

  5.   

    换种方式试试看!
    string id(你自己数据中的ID) = DataGrid1.DataKeys[e.Item.ItemIndex].ToString().Trim();
                                string ksbn = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
    …中间省略…
                               string zf = ((TextBox)e.Item.Cells[8].Controls[0]).Text;
    string strUpd = "更新语句";
    SqlConnection con = new SqlConnection("server=ICELEAN;database=Score;uid=sa;pwd=1982");
    SqlCommand com = new SqlCommand(strUpd,con); com.CommandTimeout=15;
    com.CommandType=CommandType.Text; try
    {
    con.Open();
    com.ExecuteNonQuery();
    con.Close();
    }
    catch(Exception ex)
    {

    throw ex;
    }
    ……
      

  6.   

    建议用到的SQL语句,采用存储过程,比在程序中写SQL要好的多!
      

  7.   

    我给你写了一段:
    SqlConnection MyConnection=new SqlConnection();
    MyConnection.ConnectionString="server=ICELEAN;database=Score;uid=sa;pwd=1982";
    SqlDataAdapter MyAda=new SqlDataAdapter();
    MyAda.UpdateCommand=new SqlCommand("update score set KSBH=@KSBH,XM=@XM,ZZLL=@ZZLL,WGY=@WGY,YWK1=@YWK1,YWK2=@YWK2,ZF=@ZF  where 关键字=@关键字",MyConnection);
    MyConnection.Open();
    MyAda.UpdateCommand.Parameters.Add("@KSBH", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[2].Controls[0]).Text );
    MyAda.UpdateCommand.Parameters.Add("@XM", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[3].Controls[0]).Text );
    MyAda.UpdateCommand.Parameters.Add("@ZZLL", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[4].Controls[0]).Text );
    MyAda.UpdateCommand.Parameters.Add("@WGY", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[5].Controls[0]).Text );
    MyAda.UpdateCommand.Parameters.Add("@YWK1", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[6].Controls[0]).Text );
    MyAda.UpdateCommand.Parameters.Add("@YWK2", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[7].Controls[0]).Text );
    MyAda.UpdateCommand.Parameters.Add("@ZF", SqlDbType.VarChar, 50,(TextBox)(e.Item.Cells[8].Controls[0]).Text );
    MyAda.UpdateCommand.Parameters.Add("@关键字", SqlDbType.VarChar, 50,??你自已去设置);
    int Result=MyAda.UpdateCommand.ExecuteNonQuery();  //Result为影响的行数
    MyAda.SelectCommand=new SqlCommand("select * from score",MyConnection);
     DataSet MyDataSet=new DataSet();
    MyAda.Fill(MyDataSet,"score");
    DataGrid1.DataSource =MyDataSet;
    DataGrid1.DataBind();
      

  8.   

    谢谢大家的帮忙,等我调完会把分给各位热心帮忙的人我不太会用存储过程,另外问一下,怎样用DataRow 来定义一个与指定数据库表有关的newRow呢?
     我用过 foreach(DataRow newRow7 in MyDataSet.Tables["score"].Rows),但这样会把所有的数据都更新了,怎样只更新一行呢?to lightnings
    我利用你的方法,但提示说(@KSBH varchar(50),@XM varchar(50),@ZZLL varchar(50),@WGY varcha' 需要参数 @KSBH,但未提供该参数。 
      

  9.   

    你把它更改为:
    MyAda.UpdateCommand.Parameters.Add("@KSBH", SqlDbType.VarChar, 50).value=(TextBox)(e.Item.Cells[2].Controls[0]).Text ;
    其它的也是
      

  10.   

    找MSDN上有实例,或者找一本书,都讲的有
      

  11.   

    怎样用DataRow 来定义一个与指定数据库表有关的newRow呢?数据集对象名(不是实例名!).表的名称.NewRow()