数据库访问类-C#

  • 格式:doc
  • 大小:41.50 KB
  • 文档页数:5

数据库访问类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace XXXXX.DataAccess
{
public class SqlDB
{
private string conString;
private SqlConnection sqlcon;
public SqlDB() //获取数据库连接字符串
{
this.conString = System.Configuration.ConfigurationSettings.AppSettings["SqlConnectString"];
//获取配置文件中设置的服务器IP地址
this.sqlcon = new SqlConnection(this.conString);
}
private void SqlOpen() //打开数据库连接
{
this.sqlcon.Open();
}
private void SqlClose() //关闭数据库连接
{
this.sqlcon.Close();
}
public DataSet QueryDataSet(string sqlStr) //查询返回数据集
{
try
{
this.SqlOpen();
SqlDataAdapter Sqlda = new SqlDataAdapter(sqlStr, this.conString);
DataSet ds = new DataSet();
Sqlda.Fill(ds);
return ds;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
return null;
}
finally
{ this.SqlClose(); }
}
public DataTable QueryDataTable(string sqlStr) //查询返回数据表
{
try
{
this.SqlOpen();
SqlDataAdapter Sqlda = new SqlDataAdapter(sqlStr,this.conString);
DataTable dt = new DataTable();
Sqlda.Fill(dt);
return dt;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
return null;
}
finally
{ this.SqlClose(); }
}
public int ExecuteSql(string sqlStr) //可执行增删改SQL语句
{
try
{
this.SqlOpen();
SqlCommand SqlCmd = new SqlCommand(sqlStr,this.sqlcon);
mandType = CommandType.Text;
int Result = SqlCmd.ExecuteNonQuery();
return Result;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
return -1; //标记返回-1是执行失败
}
finally
{ this.SqlClose(); }
}
public int ExecuteSql(string sqlStr, SqlParameter[] sqlStrPa) //重载方法可执行增删改SQL语句{
try
{
this.SqlOpen();
SqlCommand SqlCmd = new SqlCommand(sqlStr, this.sqlcon);
mandType = CommandType.Text;
for (int i = 0; i < sqlStrPa.Length; i++)
{
SqlCmd.Parameters.Add(sqlStrPa[i]);
}
int Result = SqlCmd.ExecuteNonQuery();
return Result;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
return -1; //标记返回-1是执行失败
}
finally { this.SqlClose(); };
}
public bool SqlTransactionArray(string [] sqlStr)
{
try
{
this.SqlOpen();
SqlTransaction SqlTran = this.sqlcon.BeginTransaction(IsolationLevel.ReadCommitted);
//启用事务处理
try {
for (int i = 0; i < sqlStr.Length;i++ )
{
string sqlNotNull = sqlStr[i];
if (sqlNotNull != null)
{
SqlCommand SqlCmd = new SqlCommand(sqlStr[i], this.sqlcon, SqlTran);
SqlCmd.ExecuteNonQuery(); //执行SQL语句
}
}
mit(); //提交事务把数据写入数据库
return true;
}
catch
{
SqlTran.Rollback(); //回滚出错的数据
return false;
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message.ToString());
return false;
}
finally
{ this.SqlClose(); }
}
}
}
抽象方法类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XXXXX.DataAccess
{
public interface Interface_EntitySet //数据实体集的接口类{
void SelectAll(string sqlStr); //用来查询表
int AddNew(object Entity); //用来增加新记录
int AddNew(string[] sqlStr); //重载方法,事务处理
int UpDate(object Entity); //用来修改数据
int Delete(string sqlStr); //用来删除数据}
}
工厂方法类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XXXXX.DataAccess
{
public class EntitySetFactory //工厂方法类,为用户获取数据时创建数据实体集对象{
public Interface_EntitySet CreateEntity(string TypeName)
{
Type type = Type.GetType(TypeName);
if (type != null)
{
object obj = Activator.CreateInstance(type);
Interface_EntitySet InEnty = (Interface_EntitySet)obj;
return InEnty;
}
else
{ return null; }
}
}
}。