最后一次. 开源.免费的数据库通用连接类

Aking1983 2003-10-16 10:36:00
*
This computer source code is Copyright 2003 AmoyStudio

All Rights Reserved.

Permission to use, copy, modify, and distribute this computer source code (Source Code),
but .Don't forget add this CopyRight info into source...
it's a freeware.. enjoy

btw: If you modified this code , you'd better send it to me .I will add it and republicsh it to everybody.Sure,I will sign your name too...

Use Example:

----------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
1.simply Exec Sqlstr:
public void DoSql()
{
SqlOperator.oSqlConnStr=@"password=noneed;user id=albert;data source=218.5.74.183;initial catalog=Albert"//<----Define default Connection string for this Appliction ,one Appliction only need define .in one place.

using(SqlOperator myOperator=new SqlStr("select * from Test"))
{
myOperator.Exec();
}
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
1.simply Exec SPr:
public void DoSqlSP()
{
SqlOperator.oSqlConnStr=@"password=noneed;user id=albert;data source=218.5.74.183;initial catalog=Albert"//<----Define default Connection string for this Appliction

using(SqlOperator myOperator=new SqlSP("SP Name"))
{
myOperator.AddP("ParameterName",Value)
myOperator.AddP("ParameterName2",Value)
myOperator.AddP("ParameterName3",Value)
myOperator.Exec();
}
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------


Melancholy
2003 Oct 10
Msn:aking83@msn.com

(Welcome Any Question,Any Ideal ^_^ )



版权信息:
恩。 小小的一个小东西没什么。
这份代码你可以任意修改,使用,发布。。。 :)enjoy
如果你修改了代码。。 如果你愿意,最好也能mail一份给我:)我将会把它添加到新的版本中。 让别人下载的:)

Melancholy
2003 Oct 10
Msn:aking83@msn.com

(Welcome Any Question,Any Ideal ^_^ )

*/

using System;
using System.Data.SqlClient;
using System.Data;

namespace AS.ToolBox
{
/// <summary>
/// Summary description for SqlToolClass.
/// </summary>
public abstract class SqlOperator:System.IDisposable
{
#region Public Var
/// <summary>
/// oSqlConnStr这个静态变量在整个程序里面只有赋值一次。。。 就可以全部用了。
/// 在WEBFROM里面。 我推荐放在。。。Global.asax.cs里面。。
/// </summary>
public static string oSqlConnStr;
public SqlConnection oSqlConn=null;
public SqlCommand oSqlCommand=null;
#endregion

#region Private Var
private SqlDataReader oSQLDataReader=null;
private SqlDataAdapter opSDA=null;
private DataSet opDS=null;
private bool disposed = false;
#endregion




#region
public SqlOperator()
{
oSqlConn=new SqlConnection(oSqlConnStr);
oSqlCommand=oSqlConn.CreateCommand();
oSqlCommand.Connection=oSqlConn;
if(ConnectionState.Open!=oSqlConn.State)
{
oSqlConn.Open();
}
else
{
oSqlConn.Close();
oSqlConn.Open();
}

}

public SqlOperator(string sConnString)
{
oSqlConn=new SqlConnection(oSqlConnStr);
oSqlConn.ConnectionString=sConnString;

oSqlCommand=oSqlConn.CreateCommand();
oSqlCommand.Connection=oSqlConn;
if(ConnectionState.Open!=oSqlConn.State)
{
oSqlConn.Open();
}
else
{
oSqlConn.Close();
oSqlConn.Open();
}

}

#endregion


#region
/// <summary>
/// 执行不返回
/// </summary>
public void Exec()
{

oSqlCommand.ExecuteNonQuery();

}
/// <summary>
/// 执行返回单值
/// </summary>
/// <returns></returns>
public object ExecOneValue()
{

object oTemp=null;

oTemp=oSqlCommand.ExecuteScalar();

return oTemp;
}
/// <summary>
/// 返回DATASET
/// </summary>
/// <returns></returns>
public DataSet ExecGetDataSet()
{
if(opDS==null)
{
opDS=new DataSet();
}

if(opSDA==null)
{
opSDA=new SqlDataAdapter(oSqlCommand);
}

opSDA.Fill(opDS);

return opDS;
}

/// <summary>
/// 返回DATAREADER
/// </summary>
/// <returns></returns>
public SqlDataReader ExecGetDataReader()
{
oSQLDataReader=oSqlCommand.ExecuteReader();
return oSQLDataReader;
}
#endregion


public void Dispose()
{
Dispose(true);
}

protected void Dispose(bool disposing)
{
if(!this.disposed)
{

if(disposing)
{
//Components.Dispose();
}
if(oSQLDataReader!=null)
{
if(!oSQLDataReader.IsClosed)
{
oSQLDataReader.Close();
}

}
oSqlConn.Close();
oSqlConn=null;
oSqlCommand=null;
opSDA=null;

}
this.disposed = true;
}

~ SqlOperator()
{
Dispose(false);
}

}

public class SqlStr:SqlOperator
{
private SqlStr() : base()
{

}
public SqlStr(string sSqlstr):base()
{
oSqlCommand.CommandText = sSqlstr;
oSqlCommand.CommandType = CommandType.Text;
}

public SqlStr(string sSqlstr,string sConnSql):base(sConnSql)
{
oSqlCommand.CommandText = sSqlstr;
oSqlCommand.CommandType = CommandType.Text;
}

}
public class SqlSP:SqlOperator
{
private SqlSP():base()
{

}

public SqlSP(string SPName)
{
oSqlCommand.CommandText = SPName;
oSqlCommand.CommandType = CommandType.StoredProcedure;
}
public SqlSP(string SPName,string sConnSql):base(sConnSql)
{
oSqlCommand.CommandText = SPName;
oSqlCommand.CommandType = CommandType.StoredProcedure;
}

/// <summary>
/// 很简单的添加参数。
/// </summary>
/// <param name="ParameterName"></param>
/// <param name="ParameterValue"></param>
public void AddP(string ParameterName,object ParameterValue)
{
SqlParameter oSParameter =new SqlParameter(ParameterName,ParameterValue);
oSqlCommand.Parameters.Add(oSParameter);
}

/// <summary>
/// 这个函数会自动把字符串的""转换成null不要乱用。
/// </summary>
/// <param name="ParameterName"></param>
/// <param name="ParameterValue"></param>
/// <param name="ValueDbType"></param>
/// <param name="ValueLength"></param>
/// <param name="IsNullAble"></param>
public void AddP(string ParameterName,object ParameterValue,SqlDbType ValueDbType,int ValueLength,bool IsNullAble)
{
SqlParameter oSParameter =new SqlParameter(ParameterName,ValueDbType,ValueLength);
oSParameter.IsNullable=IsNullAble;
if(!(ParameterValue.ToString()==""))
{
oSParameter.Value=ParameterValue;
}
else
{
oSParameter.Value=DBNull.Value;
}
oSqlCommand.Parameters.Add(oSParameter);
}
}

}

...全文
49 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Aking1983 2003-10-21
  • 打赏
  • 举报
回复
或许没有。
henryfan1 2003-10-16
  • 打赏
  • 举报
回复
DataAccessApplicationBlock有这个强吗?

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧