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

yuna106 2004-11-05 05:10:42
把数据库操作写成一个类,谁有写好的代码
...全文
277 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
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也是中文的
悄悄的回来了 2004-11-05
  • 打赏
  • 举报
回复
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

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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