简单实用的DataSet更新数据库的类+总结(c#)
2011-01-31 00:21
399 查看
this._strSql = strSql;
this.da = new SqlDataAdapter(this._strSql,this.GetConn());
this.ds.Clear();
this.da.Fill(ds,strTableName);
return ds;//返回填充了数据的DataSet,其中数据表以strTableName给出的字符串命名
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"数据库操作失败");
throw;
}
}
//数据库数据更新(传DataSet和DataTable的对象)
public DataSet UpdateDs(DataSet changedDs,string tableName)
{
try
{
this.da = new SqlDataAdapter(this._strSql,this.GetConn());
this.sqlCmdBuilder = new SqlCommandBuilder(da);
this.da.Update(changedDs,tableName);
changedDs.AcceptChanges();
return changedDs;//返回更新了的数据库表
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"数据库更新失败");
throw;
}
}
使用说明总结:
1. GetConn方法创建一个数据库连接,返回SqlConnection。
2.使用的select命令中必须包含主键,这点大家都知道的!
3. this.da.Fill(ds,strTableName) 填充数据集
4.构造CommandBuilder对象时,将DataAdapter对象作为构造函数参数传入:
this.sqlCmdBuilder = new SqlCommandBuilder(da);
5. 在调用UpdateDs()更新数据库前,请检查changedDs是否已经被更新过,用changedDs.[tableName] GetChanges() != null;
6.用this.da.Update(changedDs,tableName)方法更新数据,然后调用changedDs.AcceptChanges()才能真正的更新数据库,调用 changedDs.RejectChanges() 取消更新。
this.da = new SqlDataAdapter(this._strSql,this.GetConn());
this.ds.Clear();
this.da.Fill(ds,strTableName);
return ds;//返回填充了数据的DataSet,其中数据表以strTableName给出的字符串命名
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"数据库操作失败");
throw;
}
}
//数据库数据更新(传DataSet和DataTable的对象)
public DataSet UpdateDs(DataSet changedDs,string tableName)
{
try
{
this.da = new SqlDataAdapter(this._strSql,this.GetConn());
this.sqlCmdBuilder = new SqlCommandBuilder(da);
this.da.Update(changedDs,tableName);
changedDs.AcceptChanges();
return changedDs;//返回更新了的数据库表
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"数据库更新失败");
throw;
}
}
使用说明总结:
1. GetConn方法创建一个数据库连接,返回SqlConnection。
2.使用的select命令中必须包含主键,这点大家都知道的!
3. this.da.Fill(ds,strTableName) 填充数据集
4.构造CommandBuilder对象时,将DataAdapter对象作为构造函数参数传入:
this.sqlCmdBuilder = new SqlCommandBuilder(da);
5. 在调用UpdateDs()更新数据库前,请检查changedDs是否已经被更新过,用changedDs.[tableName] GetChanges() != null;
6.用this.da.Update(changedDs,tableName)方法更新数据,然后调用changedDs.AcceptChanges()才能真正的更新数据库,调用 changedDs.RejectChanges() 取消更新。
相关文章推荐
- 简单实用的DataSet更新数据库的类+总结(c#)
- 简单实用的DataSet更新数据库的类+总结(c#)
- 简单实用的DataSet更新数据库的类+总结(c#) 选择自 allen_21229 的 Blog
- 简单实用的DataSet更新数据库的类+总结(c#)
- 简单实用的DataSet更新数据库的类+总结(c#)
- 简单实用的DataSet更新数据库的类+总结(c#)
- 简单实用的DataSet更新数据库的类+总结(c#)
- 简单实用的DataSet更新数据库的类+总结(c#)
- 简单实用的DataSet更新数据库的类+总结(c#)
- 简单实用的DataSet更新数据库的类+总结(c#)
- 简单实用的DataSet更新数据库的类+总结(c#)
- [转]简单实用的DataSet更新数据库的类+总结
- 简单实用的DataSet更新数据库的类+总结(转~给懒得写sp的人)
- 使用 DataAdapter 和 DataSet 更新数据库 [C#]
- C#如何重DataSet的DataTable里面删除信息并更新到数据库
- DataSet更新到数据库总结
- C#程序连接数据库及读取数据库中字段的简单方法总结
- 【转载】C#连接数据库及读取数据库中字段的简单方法总结
- C#操作数据库,DataSet,DataGridView,更新数据库 [一] - ADO.NET入门之中
- 如何使用 Visual C# .NET 从 DataSet 对象更新数据库