应该有update 或是commit之类的方法把。

解决方案 »

  1.   

    参考如下:
    先取得数据,放到DataGrid里System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=194910");
    conn.Open();
    System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from customers",conn);
        dt = new System.Data.DataSet();
    da.Fill(dt,"customers");然后绑定数据集和DataGrid
    DataGrid.SetDataBinding(dt,"customers");
    然后进行数据的操作如:
    增加:
    this.BindingContext[dt,"customers"].AddNew();
    删除:
    this.BindingContext[dt,"customers"].RemoveAt(this.BindingContext[dt,"customers"].Position);
    最后把结果写回数据库:
    // 
    // sqlInsertCommand1
    // 
    this.sqlInsertCommand1.CommandText = "INSERT INTO student(stuno, name, sex, address) VALUES (@stuno, @name, @sex, @addr" +
    "ess); SELECT stuno, name, sex, address FROM student WHERE (stuno = @stuno)";
    this.sqlInsertCommand1.Connection = this.sqlConnection1;
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.Int, 4, "stuno"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.Int, 4, "name"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sex", System.Data.SqlDbType.Int, 4, "sex"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@address", System.Data.SqlDbType.Int, 4, "address"));
    // 
    // sqlUpdateCommand1
    // 
    this.sqlUpdateCommand1.CommandText = @"UPDATE student SET stuno = @stuno, name = @name, sex = @sex, address = @address WHERE (stuno = @Original_stuno) AND (address = @Original_address OR @Original_address IS NULL AND address IS NULL) AND (name = @Original_name OR @Original_name IS NULL AND name IS NULL) AND (sex = @Original_sex OR @Original_sex IS NULL AND sex IS NULL); SELECT stuno, name, sex, address FROM student WHERE (stuno = @stuno)";
    this.sqlUpdateCommand1.Connection = this.sqlConnection1;
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.Int, 4, "stuno"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.Int, 4, "name"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sex", System.Data.SqlDbType.Int, 4, "sex"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@address", System.Data.SqlDbType.Int, 4, "address"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));// sqlDeleteCommand1
    // 
    this.sqlDeleteCommand1.CommandText = @"DELETE FROM student WHERE (stuno = @Original_stuno) ";
    this.sqlDeleteCommand1.Connection = this.sqlConnection1;
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));try
    {
    sqlDa.Update(dt.GetChanges,"customers"); 
    return true;
    }
    catch(System.Data.SqlClient.SqlException ex)
    {
    myTran.Rollback();
    return false;

    finally
    {
    conn.Close();
    }


    // 
    // sqlInsertCommand1
    // 
    this.sqlInsertCommand1.CommandText = "INSERT INTO student(stuno, name, sex, address) VALUES (@stuno, @name, @sex, @addr" +
    "ess); SELECT stuno, name, sex, address FROM student WHERE (stuno = @stuno)";
    this.sqlInsertCommand1.Connection = this.sqlConnection1;
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.Int, 4, "stuno"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.Int, 4, "name"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sex", System.Data.SqlDbType.Int, 4, "sex"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@address", System.Data.SqlDbType.Int, 4, "address"));
    // 
    // sqlUpdateCommand1
    // 
    this.sqlUpdateCommand1.CommandText = @"UPDATE student SET stuno = @stuno, name = @name, sex = @sex, address = @address WHERE (stuno = @Original_stuno) AND (address = @Original_address OR @Original_address IS NULL AND address IS NULL) AND (name = @Original_name OR @Original_name IS NULL AND name IS NULL) AND (sex = @Original_sex OR @Original_sex IS NULL AND sex IS NULL); SELECT stuno, name, sex, address FROM student WHERE (stuno = @stuno)";
    this.sqlUpdateCommand1.Connection = this.sqlConnection1;
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.Int, 4, "stuno"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.Int, 4, "name"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sex", System.Data.SqlDbType.Int, 4, "sex"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@address", System.Data.SqlDbType.Int, 4, "address"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));// sqlDeleteCommand1
    // 
    this.sqlDeleteCommand1.CommandText = @"DELETE FROM student WHERE (stuno = @Original_stuno) ";
    this.sqlDeleteCommand1.Connection = this.sqlConnection1;
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));try
    {
    sqlDa.Update(dt.GetChanges,"customers"); 
    return true;
    }
    catch(System.Data.SqlClient.SqlException ex)
    {
    myTran.Rollback();
    return false;

    finally
    {
    conn.Close();
    }
      

  2.   

    http://expert.csdn.net/Expert/topic/2070/2070351.xml?temp=.7820856