|
|
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
namespace sqlServer
{
/// <summary>
/// DataBase 的摘要说明。
/// </summary>
/// <summary>
/// 数据库连接类型
/// </summary>
public enum dbType
{
/// <summary>
/// SQL数据库
/// </summary>
sql,
/// <summary>
/// access数据库
/// </summary>
access
}
public class DataBase
{
// 连接数据源
private SqlConnection con = null;
private OleDbConnection conn = null;
//连接数据类型
dbType cType;
//多线程sql字符串
private string conString = null;
public DataBase(string conStr, dbType type)
{
this.cType = type;
if (type == dbType.sql)
con = new SqlConnection(conStr);
else if (type == dbType.access)
conn = new OleDbConnection(conStr);
}
/// <summary>
/// 根据SQL查询返回DataSet对象,如果没有查询到则返回NULL
/// </summary>
/// <param name="sql">查询语句</param>
/// <returns>DataSet</returns>
public DataSet returnDS(string sql)
{
DataSet ds = new DataSet();
try
{
if (cType == dbType.sql)
{
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandTimeout = 20;
this.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds, "tempTable");
}
else if (cType == dbType.access)
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.CommandTimeout = 20;
this.Open();
System.Data.OleDb.OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(ds, "tempTable");
}
}
catch (Exception e)
{
throw (e);
ds = null;
}
finally
{
this.Close();
}
return ds;
}
/// <summary>
/// 根据SQL查询返回DataSet对象,如果没有查询到则返回NULL
/// </summary>
/// <param name="sql">查询语句</param>
/// <param name="sRecord">开始记录数</param>
/// <param name="mRecord">最大记录数</param>
/// <returns>DataSet</returns>
public DataSet returnDS(string sql, int sRecord, int mRecord)
{
DataSet ds = new DataSet();
try
{
if (cType == dbType.sql)
{
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandTimeout = 20;
this.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds, sRecord, mRecord, "tempTable");
}
else if (cType == dbType.access)
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.CommandTimeout = 20;
this.Open();
System.Data.OleDb.OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(ds, sRecord, mRecord, "tempTable");
}
}
catch (Exception e)
{
throw (e);
ds = null;
}
finally
{
this.Close();
}
return ds;
}
/// <summary>
/// 对数据库的增,删,改的操作
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns>是否成功</returns>
public bool OperateDB(string sql)
{
bool succeed = false;
int cnt = 0;
try
{
if (cType == dbType.sql)
{
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandTimeout = 20;
this.Open();
cnt = cmd.ExecuteNonQuery();
}
else if (cType == dbType.access)
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.CommandTimeout = 20;
this.Open();
cnt = cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
throw (e);
}
finally
{
if (cnt > 0)
{
succeed = true;
}
this.Close();
}
return succeed;
}
/// <summary>
/// 获得该SQL查询返回的第一行第一列的值,如果没有查询到则返回NULL
/// </summary>
/// <param name="sql">查询语句</param>
/// <returns>返回的第一行第一列的值</returns>
public string getValue(string sql)
{
string str = null;
try
{
if (cType == dbType.sql)
{
SqlCommand cmd = new SqlCommand(sql, con);
this.Open();
str = cmd.ExecuteScalar().ToString();
}
else if (cType == dbType.access)
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
this.Open();
str = cmd.ExecuteScalar().ToString();
}
}
catch (Exception e)
{
throw (e);
}
finally
{
this.Close();
}
return str;
}
/// <summary>
/// 多线程版本获得该SQL查询返回的第一行第一列的值,如果没有查询到则返回NULL
/// </summary>
/// <param name="sql">查询语句</param>
/// <returns>返回的第一行第一列的值</returns>
public void getValue()
{
string str = null;
try
{
if (cType == dbType.sql)
{
SqlCommand cmd = new SqlCommand(conString, con);
this.Open();
str = cmd.ExecuteScalar().ToString();
}
else if (cType == dbType.access)
{
OleDbCommand cmd = new OleDbCommand(conString, conn);
this.Open();
str = cmd.ExecuteScalar().ToString();
}
}
catch (Exception e)
{
throw (e);
}
finally
{
this.Close();
}
//return str;
}
/// <summary>
/// 获得该SQL查询返回DataTable,如果没有查询到则返回NULL
/// </summary>
/// <param name="sql">查询语句</param>
/// <returns></returns>
public DataTable getTable(string sql)
{
DataTable tb = null;
DataSet ds = this.returnDS(sql);
if (ds != null)
{
tb = ds.Tables["tempTable"];
}
return tb;
}
/// <summary>
/// 打开数据库连接.
/// </summary>
public void Open()
{
if (cType == dbType.sql)
{
if (con.State == System.Data.ConnectionState.Closed)
{
con.Open();
}
else if (con.State == System.Data.ConnectionState.Broken)
{
con.Close();
con.Open();
}
}
else if (cType == dbType.access)
{
if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
}
else if (conn.State == System.Data.ConnectionState.Broken)
{
conn.Close();
conn.Open();
}
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
if (cType == dbType.sql)
{
if (con != null)
{
con.Close();
}
}
else if (cType == dbType.access)
{
if (conn != null)
{
conn.Close();
}
}
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
if (cType == dbType.sql)
{
// 确认连接是否已经关闭
if (con != null)
{
con.Dispose();
con = null;
}
}
else if (cType == dbType.access)
{
if (conn != null)
{
conn.Dispose();
conn = null;
}
}
}
}
} |
|