using System;
using System.Data;
using System.Data.SqlClient;
namespace DBClassLibrary
{
/// <summary>
/// DBBase 的摘要说明。
/// </summary>
public class DBBase
{
public SqlConnection Conn = new SqlConnection();
public SqlCommand Comm = new SqlCommand();
public SqlDataReader Reader ;
public Return _return = new Return();
/// <summary>
/// 返回默认初始化时间 1/1/1753 12:00:00 AM
/// </summary>
public DateTime DefaultDate
{
get { return DateTime.Parse("1/1/1753 12:00:00 AM"); }
}
public Return Return
{
get{ return _return;}
set{ _return=value;}
}
/// <summary>
/// 返回执行情况
/// </summary>
/// <returns></returns>
public Return SqlproThrow()
{
int result=int.Parse(Comm.Parameters["@result"].Value.ToString());
string reason=Comm.Parameters["@reason"].Value.ToString();
this.CommClose();
if (result==0)
{
return new Return(true,reason);
}
else
{
return new Return(false,reason);
}
}
/// <summary>
/// 抛出自定义异常
/// </summary>
/// <param name="s">自定义异常信息</param>
public void Mythrow(string s)
{
throw new System.Exception(s);
}
/// <summary>
/// 连接数据库 (请使用ConnClose()方法关闭)
/// </summary>
/// <param name="DbServerString">数据库连接字符串</param>
/// <param name="result">返回错误标记(非0表示失败)</param>
/// <param name="reason">返回错误信息</param>
/// <returns>返回SqlConnection对象</returns>
public void ConnOpen(string DbServerString)
{
//连接数据库
if (Conn.State!=System.Data.ConnectionState.Closed)
{
Conn.Close();
}
Conn.ConnectionString=DbServerString;
Conn.Open();
Comm.Connection=Conn;
}
public void ConnOpen(string host,string db,string user,string password)
{
//连接数据库
if (Conn.State!=System.Data.ConnectionState.Closed)
{
Conn.Close();
}
Conn.ConnectionString="Persist Security Info=False;Data Source="+host+";Initial Catalog="+db+";User ID="+user+";Password="+password;
Conn.Open();
Comm.Connection=Conn;
}
/// <summary>
/// 断开数据库连接
/// </summary>
/// <param name="result">返回错误标记</param>
/// <param name="reason">返回错误信息</param>
public void ConnClose()
{
Conn.Close();
Conn.Dispose();
}
/// <summary>
/// 释放DataBase资源
/// </summary>
/// <param name="result">返回错误标记</param>
/// <param name="reason">返回错误信息</param>
public void Dispose()
{
if (Reader!=null)
{
this.Reader.Close();
}
this.CommClose();
this.ConnClose();
}
/// <summary>
/// 返回内置存储参数的SqlCommand对象,否则总要手动输入存储过程参数的类型等等,怪麻烦的。
/// (请使用CommClose()方法关闭)
/// </summary>
/// <param name="DbServerString">数据库连接字符串</param>
/// <param name="ProString">存储过程名</param>
/// <param name="result">返回错误代码</param>
/// <param name="reason">返回错误信息</param>
/// <returns>返回SqlCommand对象</returns>
public void DPComm(string ProString)
{
Comm.CommandType = CommandType.StoredProcedure ;
Comm.CommandText = ProString;
Comm.Parameters.Clear();
SqlCommandBuilder.DeriveParameters(Comm);
for (int i=0;i<Comm.Parameters.Count;i++)
{
if (Comm.Parameters[i].Direction==ParameterDirection.InputOutput)
{
Comm.Parameters[i].Direction = ParameterDirection.Output; //把该参数的输入输出变成只输出,否则总要提示输入该参数的值。
}
}
}
/// <summary>
/// 断开数据库连接
/// </summary>
/// <param name="result">返回错误标记</param>
/// <param name="reason">返回错误信息</param>
public void CommClose()
{
Conn.Close();
Conn.Dispose();
Comm.Dispose();
}
/// <summary>
/// 执行Sql查询字符串
/// </summary>
/// <param name="DbServerString">数据库连接字符</param>
/// <param name="SqlString">Sql查询字符串</param>
/// <param name="arPrm">Sql查询的输入参数</param>
/// <returns>返回SqlCommand对象</returns>
public void DPSqlText(string SqlString)
{
Comm.CommandText = SqlString;
Comm.CommandType=System.Data.CommandType.Text;
Comm.Parameters.Clear();
}
/// <summary>
/// 获取DataSet数据集,主要是为了省略输入拉。。。
/// </summary>
/// <param name="Comm">SqlCommand对象</param>
/// <returns></returns>
public DataSet GetDs()
{
DataSet ds = new DataSet();
SqlDataAdapter ad = new SqlDataAdapter();
ad.SelectCommand = this.Comm;
ad.Fill(ds);
this.CommClose();
ad.Dispose();
return ds;
}
/// <summary>
/// 将结果集装入Reader
/// (请使用ReaderClose()方法关闭)
/// </summary>
public void DPReader()
{
this.Reader = this.Comm.ExecuteReader();
}
/// <summary>
/// 关闭记录集读取对象
/// </summary>
/// <param name="result">返回错误标记</param>
/// <param name="reason">返回错误信息</param>
public void ReaderClose()
{
Reader.Close();
Comm.Dispose();
Conn.Close();
Conn.Dispose();
}
/// <summary>
/// 清除多余的表
/// </summary>
/// <param name="ds">欲清除多余表的数据集</param>
/// <returns>返回清除掉多余表数据集</returns>
public DataSet dsClear(DataSet ds)
{
//判断数据集中是否有表
if (ds.Tables.Count<1)
{
ds.Tables.Clear();
return ds=new DataSet();
}
//删除返回错误条目的表
for (int i=0;i<ds.Tables.Count;i++)
{
int result=int.Parse(ds.Tables[i].Rows[0]["result"].ToString());
if (result!=0)
{
ds.Tables.RemoveAt(i);
}
}
//如果数据集中的表都被删除则退出操作
if (ds.Tables.Count<1)
{
ds.Tables.Clear();
return ds=new DataSet();
}
//将所有剩余的表整合到第一个表中
for (int i=1;i<ds.Tables.Count;i++)
{
if (ds.Tables[0].Rows[0].ItemArray.LongLength==ds.Tables[i].Rows[0].ItemArray.LongLength)
{
ds.Tables[0].Rows.Add(ds.Tables[i].Rows[0].ItemArray);
}
else
{
return ds=new DataSet();
}
}
//删除除第一个表以外的所有表
for (int i=1;i<ds.Tables.Count;i++)
{
ds.Tables.RemoveAt(i);//删除多余的表
}
return ds;
}
public DBBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/* 样板
/// <summary>
/// 返回account信息
/// </summary>
/// <param name="account">帐号</param>
/// <param name="password">密码</param>
/// <returns>数据集</returns>
[WebMethod(Description="方法说明")]
public DataSet Login1(string account,string password)
{
//连接数据库
db.ConnOpen(this.dbOwnConnstring.ToString());
//调用存储过程
db.DPComm("accountLoad");
//给存储过程参数 @account 付值
db.Comm.Parameters["@account"].Value=account;
//给存储过程参数 @password 付值
db.Comm.Parameters["@password"].Value=password;
//返回数据集(该GetDs方法会同时关闭Comm对象和Conn对象)
return db.GetDs();
}
/// <summary>
/// 返回account信息
/// </summary>
/// <param name="account">帐号</param>
/// <param name="password">密码</param>
/// <returns>数据集</returns>
[WebMethod(Description="方法说明")]
public bool Login2(string account,string password,out string name,out int age)
{
//连接数据库
db.ConnOpen(this.dbOwnConnstring.ToString());
//调用存储过程
db.DPComm("accountLoad");
//给存储过程参数 @account 付值
db.Comm.Parameters["@account"].Value=account;
//给存储过程参数 @password 付值
db.Comm.Parameters["@password"].Value=password;
//执行Comm返回给reader
db.DPReader();
//判断reader是否有行记录
bool t=db.Reader.Read();
if (t)
{
//取reader的列值
name=db.Reader["name"].ToString();
age=int.Parse(db.Reader["age"].ToString());
}
else
{
name="";
age=0;
}
//关闭reader对象 (ReaderClose方法会同时关闭Comm和Conn对象)
db.ReaderClose();
return t;
}
/// <summary>
/// 返回account信息
/// </summary>
/// <param name="account">帐号</param>
/// <param name="password">密码</param>
/// <returns>数据集</returns>
[WebMethod(Description="方法说明")]
public bool Login3(string account,string password,out string name,out int age)
{
//连接数据库
db.ConnOpen(this.dbOwnConnstring.ToString());
//调用存储过程
db.DPSqlText("select * from account where account=@account and password=@password");
//创建一个参数对象
System.Data.SqlClient.SqlParameter parValue1 = new System.Data.SqlClient.SqlParameter();
//给Sql查询字符串参数 @account 付值
parValue1.ParameterName="@account";
parValue1.Direction=System.Data.ParameterDirection.Input;
parValue1.SqlDbType=SqlDbType.Char;
parValue1.Value=account;
System.Data.SqlClient.SqlParameter parValue2 = new System.Data.SqlClient.SqlParameter();
//给Sql查询字符串参数 @password 付值
parValue2.ParameterName="@password";
parValue2.Direction=System.Data.ParameterDirection.Input;
parValue2.SqlDbType=SqlDbType.Char;
parValue2.Value=password;
db.Comm.Parameters.Add(parValue1);
db.Comm.Parameters.Add(parValue2);
//执行Comm返回给reader
db.DPReader();
//判断reader是否有行记录
bool t=db.Reader.Read();
if (t)
{
//取reader的列值
name=db.Reader["name"].ToString();
age=int.Parse(db.Reader["age"].ToString());
}
else
{
name="";
age=0;
}
//关闭reader对象 (ReaderClose方法会同时关闭Comm和Conn对象)
db.ReaderClose();
return t;
}
*/
}
}
//---------------------------------------------------------------------------------------------------------------------------
using System;
namespace DBClassLibrary
{
/// <summary>
/// DBConnString 的摘要说明。
/// </summary>
public class DBConnString
{
public DBConnString(string host,string dbase,string user,string password)
{
this._host=host;
this._dbase=dbase;
this._user=user;
this._password=password;
}
public DBConnString()
{
}
private string _host;
private string _dbase;
private string _user;
private string _password;
/// <summary>
/// 主机地址
/// </summary>
public string host
{
get {return this._host;}
set {this._host=value;}
}
public string dbase
{
get {return this._dbase;}
set {this._dbase=value;}
}
/// <summary>
/// 用户
/// </summary>
public string user
{
get {return this._user;}
set {this._user=value;}
}
/// <summary>
/// 密码
/// </summary>
public string password
{
get {return this._password;}
set {this._password=value;}
}
/// <summary>
/// 数据库连接字符串
/// </summary>
public string getString
{
get {return "Persist Security Info=False;Data Source="+this._host+";Initial Catalog="+this._dbase+";User ID="+this._user+";Password="+this._password;}
}
/// <summary>
/// 返回数据库连接字符串
/// </summary>
/// <returns></returns>
public override string ToString()
{
return this.getString;
}
}
}
//------------------------------------------------------------------------------------------------------------------------------------------
using System;
namespace DBClassLibrary
{
/// <summary>
/// Return 的摘要说明。
/// </summary>
public class Return
{
private bool result;
private string reason;
/// <summary>
/// 成功/失败
/// </summary>
public bool Result
{
get
{
return result;
}
set
{
result=value;
}
}
/// <summary>
/// 失败原因
/// </summary>
public string Reason
{
get
{
return reason;
}
set
{
reason=value;
}
}
/// <summary>
/// 构造函数逻辑
/// </summary>
public Return(bool result,string reason)
{
this.Result=result;
this.Reason=reason;
}
/// <summary>
/// 构造函数逻辑
/// </summary>
public Return()
{
this.Result=true;
this.Reason="";
}
}
}