• 全部
  • .NET Framework
  • ASP
  • Web Services
  • .NET互联网桌面应用
  • VB
  • 图表区
  • 分析与设计
  • 组件/控件开发
  • AppLauncher
  • 问答

把数据库操作写成一个类,谁有写好的代码

yuna106 2004-11-05 05:10:42
把数据库操作写成一个类,谁有写好的代码
...全文
216 点赞 收藏 13
写回复
13 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
shoutor 2004-11-06
使用Petshop里面的吧,跟上面差不多,但相对简单些
回复
lonelydreamsym 2004-11-06
up
回复
nmlvjun 2004-11-06
blog/csdn.net/nmlvjun
回复
hbb0b0 2004-11-06
using System;
using System.Data.SqlClient;
using System.Data;
namespace DataAccess
{
/// <summary>
/// It is used to asscess database
/// </summary>
public class Database:IDisposable
{
/// <summary>
/// It's store the error message
/// </summary>
string _errorMessage;
/// <summary>
///It's store the Exception
/// </summary>
System.Exception _exception;
/// <summary>
/// It is marked run state.
/// </summary>
int result;
/// <summary>
/// store Connection String
/// </summary>
//string _connectionString;

SqlConnection myConnection;

SqlCommand myCommand;

string _ConfigurationConnectionString=null;


#region IDisposable 成员

public void Dispose()
{
if(this.myConnection==null)
myConnection.Dispose();
else
myConnection=null;
System.GC.SuppressFinalize(this);



}

#endregion
/// <summary>
/// get or set Exception as string .
/// </summary>
///
public string ErrorMessage
{
get{ return _errorMessage;}
set{ _errorMessage=value;}


}
public int Result
{
get{return result;}
}

/// <summary>
/// get or set Exception.
/// </summary>
public Exception Exception
{
get{return _exception;}
set{_exception=value;}

}
/// <summary>
/// Set ConnectionString where in Web.Config
/// </summary>
public string ConfigurationConnectionString
{
get{return this._ConfigurationConnectionString;}
set{this._ConfigurationConnectionString=value;}
}

/// <summary>
/// get Connetion string from web.Config.if you hava not set the ConfigurationConnection.
/// it will get AppSettings by connectionString,if you want set the ConnectionString you
/// can use it.
/// </summary>
private string ConnectionString
{
get{
if(this._ConfigurationConnectionString==null)
{
return (string)System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
}
else
{
return (string)this._ConfigurationConnectionString;

}



}

}


/// <summary>
/// Create OutPut SqlParameter.
/// </summary>
/// <param name="_name">Parameter's Name</param>
/// <param name="_value">Parameter's Value</param>
/// <returns></returns>
public SqlParameter MakeParameter(string _name,object _value)
{

SqlParameter myParameter=new SqlParameter(_name,_value);
myParameter.Direction=ParameterDirection.Input;
return myParameter;
}
/// <summary>
///
/// </summary>
/// <param name="_name"></param>
/// <param name="_value"></param>
/// <param name="_direction"></param>
/// <returns></returns>
public SqlParameter MakeParameter(string _name,object _value,ParameterDirection _direction)
{
SqlParameter myParameter=new SqlParameter(_name,_value);
myParameter.Direction=_direction;
return myParameter;

}
/// <summary>
/// The Procedure which have no parameter and
/// return scalar.
/// </summary>
/// <param name="_procedureName"></param>
/// <returns></returns>
public void RunProcedure(string _procedureName)
{
try
{
myConnection=new SqlConnection(this.ConnectionString);
myCommand=new SqlCommand(_procedureName,myConnection);
myCommand.CommandType=CommandType.StoredProcedure;
myConnection.Open();
//this.result=myCommand.ExecuteNonQuery();
myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

}
catch(Exception myEx)
{
this.ErrorMessage=myEx.Message;
this.Exception=myEx;
this.result =-1;


}
finally
{
myConnection.Close();


}

}

/// <summary>
/// The Procedure which have none parameter and return SqlDataReader.
/// </summary>
/// <param name="_procedureName"></param>
/// <returns></returns>
public void RunProcedure(string _procedureName, ref SqlDataReader _datareader)
{
try
{
myConnection=new SqlConnection(this.ConnectionString);
myCommand=new SqlCommand(_procedureName,myConnection);
myCommand.CommandType=CommandType.StoredProcedure;
myConnection.Open();
_datareader=myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
catch(Exception myEx)
{
this.ErrorMessage=myEx.Message;
this.Exception=myEx;

this.result=-1;

}
}
/// <summary>
/// The Procedure which have none parameter and return DataSet.
/// </summary>
/// <param name="_procedureName"></param>

public void RunProcedure(string _procedureName,ref DataSet _dataset)
{
try{
myConnection=new SqlConnection(this.ConnectionString);
myCommand=new SqlCommand(_procedureName,myConnection);
myCommand.CommandType=CommandType.StoredProcedure;
myConnection.Open();
SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
myAdapter.Fill(_dataset);
}
catch(Exception myEx)
{
this.ErrorMessage=myEx.Message;
this.Exception=myEx;

result=-1;

}


}
/// <summary>
/// The Procedure which has Parameter's Array and return SqlDataReader.
/// </summary>
/// <param name="_procedureName"></param>
/// <param name="_parameters"></param>
/// <returns></returns>
public void RunProcedure(string _procedureName,SqlParameter[] _parameters,ref SqlDataReader _sqldataReader)
{
try
{
myConnection=new SqlConnection(this.ConnectionString);
myCommand=new SqlCommand(_procedureName,myConnection);
myCommand.CommandType=CommandType.StoredProcedure;
foreach(SqlParameter param in _parameters)
myCommand.Parameters.Add(param);

myConnection.Open();
_sqldataReader=myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
catch(Exception myEx)
{
this.ErrorMessage=myEx.Message;
this.Exception=myEx;
result=-1;

}
}
/// <summary>
/// The Procedure which has Parameter's Array and return DataSet.
/// </summary>
/// <param name="_procedureName"></param>
/// <param name="_parameters"></param>
/// <returns></returns>
public void RunProcedure(string _procedureName,SqlParameter[] _parameters,ref DataSet _dataset)
{
try
{
myConnection=new SqlConnection(this.ConnectionString);
myCommand=new SqlCommand(_procedureName,myConnection);
myCommand.CommandType=CommandType.StoredProcedure;
foreach(SqlParameter param in _parameters)
myCommand.Parameters.Add(param);

myConnection.Open();
SqlDataAdapter myAdapter=new SqlDataAdapter(myCommand);
myAdapter.Fill(_dataset);
}
catch(Exception myEx)
{
this.ErrorMessage=myEx.Message;
this.Exception=myEx;
this.result=-1;

}



}
/// <summary>
/// The Procedure which has Parameter's Array and return Scale.
/// </summary>
/// <param name="_procedureName"></param>
/// <param name="_parameters"></param>
/// <returns></returns>
public void RunProcedure(string _procedureName,SqlParameter[] _parameters)
{
try
{
myConnection=new SqlConnection(this.ConnectionString);
myCommand=new SqlCommand(_procedureName,myConnection);
myCommand.CommandType=CommandType.StoredProcedure;
foreach(SqlParameter param in _parameters)
myCommand.Parameters.Add(param);

myConnection.Open();
//this.result=myCommand.ExecuteNonQuery();
myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

}
catch(Exception myEx)
{
this.ErrorMessage=myEx.Message;
this.Exception=myEx;
this.result=-1;

}





}













}
}

看看
回复
yuna106 2004-11-06
回复
wangrenda 2004-11-05
http://blog.csdn.net/huangyaoshifog/archive/2004/08/27/86263.aspx
回复
yuna106 2004-11-05
Data Access Application Block 哪有中文版??? 包括说明书CHM也是中文的
回复
duwamish 7.0
回复
v192 2004-11-05
那些都不怎么好啊,还是ORM,数据库对象映射好,Nhibernate,研究研究吧,我正看呢,啃得有点艰辛……5555
回复
okwinds 2004-11-05
Data Access Application Block

http://www.microsoft.com/china/msdn/archives/library/dnbda/html/daab-rm.asp
回复
dicklee1214 2004-11-05
高!实在是高!
回复
xueqs 2004-11-05
同上,可都是.net专家写的
回复
wang790809 2004-11-05
SqlHelper MicroSoft
回复
发帖
.NET技术社区
创建于2007-09-28

5.8w+

社区成员

.NET技术交流专区
申请成为版主
帖子事件
创建了帖子
2004-11-05 05:10
社区公告
暂无公告