最近一直没有发表文章,是因为觉得没有什么好的想法可以写,不过还是做了一个数据库操作类,可以处理SqlServer、Oracle、OleDb连接方式的数据库。目前Odbc还没有加入,其中OleDb中的一些连接串无法测试,所以还望在使用的过程中慢慢的发现并改正。
成员一览表:
下面仅给出一些应该注意的地方的代码示例:
已更新到 2.2 版本
需要下的载的可以从这里进行下载:下载类库 版本:2.2
成员一览表:
// Methods
public void BeginTransaction();
public void Close();
public void CloseUpdateBag();
public void CommitTransaction();
public void Dispose();
public DataSet ExecuteDataset(string CommandText);
public DataSet ExecuteDataset(string CommandText, params Parameter[] Parameters);
public DataSet ExecuteDataset(string CommandText, ParameterCollection Parameters);
public DataSet ExecuteDataset(string CommandText, string TableName);
public DataSet ExecuteDataset(string CommandText, string TableName, ParameterCollection Parameters);
public DataSet ExecuteDataset(string CommandText, string TableName, params Parameter[] Parameters);
public DataTable ExecuteDataTable(string CommandText);
public DataTable ExecuteDataTable(string CommandText, ParameterCollection Parameters);
public DataTable ExecuteDataTable(string CommandText, params Parameter[] Parameters);
public DataTable ExecuteDataTable(string CommandText, string TableName);
public DataTable ExecuteDataTable(string CommandText, string TableName, params Parameter[] Parameters);
public DataTable ExecuteDataTable(string CommandText, string TableName, ParameterCollection Parameters);
public int ExecuteNonQuery(string CommandText);
public int ExecuteNonQuery(string CommandText, ParameterCollection Parameters);
public int ExecuteNonQuery(string CommandText, params Parameter[] Parameters);
public IDataReader ExecuteReader(string CommandText, params Parameter[] Parameters);
public IDataReader ExecuteReader(string CommandText, ParameterCollection Parameters);
public object ExecuteScalar(string CommandText);
public object ExecuteScalar(string CommandText, ParameterCollection Parameters);
public object ExecuteScalar(string CommandText, params Parameter[] Parameters);
public void FillDataset(string CommandText, DataSet DataSet);
public void FillDataset(string CommandText, DataSet DataSet, params Parameter[] Parameters);
public void FillDataset(string CommandText, DataSet DataSet, ParameterCollection Parameters);
public void FillDataset(string CommandText, DataSet DataSet, string TableName, ParameterCollection Parameters);
public void FillDataset(string CommandText, DataSet DataSet, string TableName, params Parameter[] Parameters);
public void OpenUpdateBag();
public void RollbackTransaction();
public bool TestConnection();
public void Update(DataSet DataSet);
public void Update(DataTable DataTable);
public void Update(DataSet DataSet, DataViewRowState DataViewRowState);
public void Update(DataTable DataTable, DataViewRowState DataViewRowState);
// Properties
public string ConnectionString { get; set; }
public ConnectionState State { get; }
}
public void BeginTransaction();
public void Close();
public void CloseUpdateBag();
public void CommitTransaction();
public void Dispose();
public DataSet ExecuteDataset(string CommandText);
public DataSet ExecuteDataset(string CommandText, params Parameter[] Parameters);
public DataSet ExecuteDataset(string CommandText, ParameterCollection Parameters);
public DataSet ExecuteDataset(string CommandText, string TableName);
public DataSet ExecuteDataset(string CommandText, string TableName, ParameterCollection Parameters);
public DataSet ExecuteDataset(string CommandText, string TableName, params Parameter[] Parameters);
public DataTable ExecuteDataTable(string CommandText);
public DataTable ExecuteDataTable(string CommandText, ParameterCollection Parameters);
public DataTable ExecuteDataTable(string CommandText, params Parameter[] Parameters);
public DataTable ExecuteDataTable(string CommandText, string TableName);
public DataTable ExecuteDataTable(string CommandText, string TableName, params Parameter[] Parameters);
public DataTable ExecuteDataTable(string CommandText, string TableName, ParameterCollection Parameters);
public int ExecuteNonQuery(string CommandText);
public int ExecuteNonQuery(string CommandText, ParameterCollection Parameters);
public int ExecuteNonQuery(string CommandText, params Parameter[] Parameters);
public IDataReader ExecuteReader(string CommandText, params Parameter[] Parameters);
public IDataReader ExecuteReader(string CommandText, ParameterCollection Parameters);
public object ExecuteScalar(string CommandText);
public object ExecuteScalar(string CommandText, ParameterCollection Parameters);
public object ExecuteScalar(string CommandText, params Parameter[] Parameters);
public void FillDataset(string CommandText, DataSet DataSet);
public void FillDataset(string CommandText, DataSet DataSet, params Parameter[] Parameters);
public void FillDataset(string CommandText, DataSet DataSet, ParameterCollection Parameters);
public void FillDataset(string CommandText, DataSet DataSet, string TableName, ParameterCollection Parameters);
public void FillDataset(string CommandText, DataSet DataSet, string TableName, params Parameter[] Parameters);
public void OpenUpdateBag();
public void RollbackTransaction();
public bool TestConnection();
public void Update(DataSet DataSet);
public void Update(DataTable DataTable);
public void Update(DataSet DataSet, DataViewRowState DataViewRowState);
public void Update(DataTable DataTable, DataViewRowState DataViewRowState);
// Properties
public string ConnectionString { get; set; }
public ConnectionState State { get; }
}
下面仅给出一些应该注意的地方的代码示例:
//创建SqlServer类型的实例
DataHelper dhp = SqlServer.Create(".", "mshop", "sa", "123");
//如果是Access则使用 OleDb.Access.Create()
//用DataSet填充DataGrid,并使用Update更新
DataSet dst = new DataSet();
dhp.OpenUpdateBag(); //打开更新包
dhp.FillDataset("select * from swxx", dst, "aa"); //填充数据 指定表名称
dhp.FillDataset("select * from user_info", dst, "bb");
dataGrid1.DataSource = dst;
dhp.CloseUpdateBag(); //关闭更新包
//..
//更新DataSet
DataSet dst = (DataSet)dataGrid1.DataSource;
dhp.Update(dst);
//事务操作
dhp.BeginTransaction(); //开始事务
try
{
dhp.ExecuteNonQuery("delete from swxx where id = 1");
dhp.ExecuteNonQuery("delete from swxx where id = 2");
dhp.CommitTransaction(); //提交
}
catch
{
dhp.RollbackTransaction(); //回滚
}
//执行存储过程
ParameterCollection pc = new ParameterCollection();
pc.Add("@id", 5);
//如果添加带返回的参数
pc.Add("@return", DbType.String, 20);
dhp.ExecuteNonQuery("deleteswxx", pc);
//执行带参数的sql语句
ParameterCollection pc = new ParameterCollection();
pc.Add("@name", "faib");
pc.Add("@sex", true);
pc.Add("@birthday", DateTime.Today);
dhp.ExecuteNonQuery(pc.Format("insert into userinfo({0}) values({1})"), pc);
//或者更新
dhp.ExecuteNonQuery(pc.Format("update userinfo set {0} where id = 1"), pc);
DataHelper dhp = SqlServer.Create(".", "mshop", "sa", "123");
//如果是Access则使用 OleDb.Access.Create()
//用DataSet填充DataGrid,并使用Update更新
DataSet dst = new DataSet();
dhp.OpenUpdateBag(); //打开更新包
dhp.FillDataset("select * from swxx", dst, "aa"); //填充数据 指定表名称
dhp.FillDataset("select * from user_info", dst, "bb");
dataGrid1.DataSource = dst;
dhp.CloseUpdateBag(); //关闭更新包
//..
//更新DataSet
DataSet dst = (DataSet)dataGrid1.DataSource;
dhp.Update(dst);
//事务操作
dhp.BeginTransaction(); //开始事务
try
{
dhp.ExecuteNonQuery("delete from swxx where id = 1");
dhp.ExecuteNonQuery("delete from swxx where id = 2");
dhp.CommitTransaction(); //提交
}
catch
{
dhp.RollbackTransaction(); //回滚
}
//执行存储过程
ParameterCollection pc = new ParameterCollection();
pc.Add("@id", 5);
//如果添加带返回的参数
pc.Add("@return", DbType.String, 20);
dhp.ExecuteNonQuery("deleteswxx", pc);
//执行带参数的sql语句
ParameterCollection pc = new ParameterCollection();
pc.Add("@name", "faib");
pc.Add("@sex", true);
pc.Add("@birthday", DateTime.Today);
dhp.ExecuteNonQuery(pc.Format("insert into userinfo({0}) values({1})"), pc);
//或者更新
dhp.ExecuteNonQuery(pc.Format("update userinfo set {0} where id = 1"), pc);
已更新到 2.2 版本
需要下的载的可以从这里进行下载:下载类库 版本:2.2