Sunday, March 28, 2010

Updating a Data Source with a Dataset

my answer on stackoverflow question Updating a Data Source with a Dataset:

[WebMethod]
public bool SecureUpdateDataSet(DataSet delta)
{

     string connStr = WebConfigurationManager.ConnectionStrings["Employees"].ConnectionString;

     using(var conn = new SqlConnection(connStr))
     {
        conn.Open();

        string sql = "select * from tab1 where 1 = 0";

        var da = new SqlDataAdapter(sql, conn);

        var builder = new SqlCommandBuilder(ad);

        da.InsertCommand = builder.GetInsertCommand();
        da.UpdateCommand = builder.GetUpdateCommand();
        da.DeleteCommand = builder.GetDeleteCommand();

        da.Update(delta);

        return true;

    }
    return false;
}

No comments:

Post a Comment