MyException - 我的异常网
当前位置:我的异常网» C# » 求1性能较好的c#数据库操作类

求1性能较好的c#数据库操作类(2)

www.MyException.Cn  网友分享于:2013-12-20  浏览:45次

throw ex;
}
}

/// <summary>
/// 执行读数据集操作,以DataReader的形式返回
/// </summary>
/// <param name= "procName "> 存储过程名称 </param>
/// <returns> DataReader对象 </returns>
public static SqlDataReader ExecuteReader(string procName)
{
SqlCommand cmd = new SqlCommand();
SqlConnection con = GetConObject();

try
{
CreateCommand(cmd, con, null, procName, null);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return dr;
}
catch(Exception ex)
{
cmd.Dispose();
Close(con);
throw ex;
}
}

/// <summary>
/// 执行读数据集操作,以DataSet的形式返回
/// </summary>
/// <param name= "procName "> 存储过程名称 </param>
/// <param name= "cmdParms "> 存储过程所使用的参数 </param>
/// <returns> DataSet对象 </returns>
public static DataSet ExecuteDataSet(string procName, SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
SqlConnection con = GetConObject();
DataSet ds = new DataSet();

try
{
CreateCommand(cmd, con, null, procName, cmdParms);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
cmd.Parameters.Clear();
return ds;
}
catch(Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
Close(con);
}
}


/// <summary>
/// 执行读数据集操作,以DataSet的形式返回
/// </summary>
/// <param name= "procName "> 存储过程名称 </param>
/// <returns> DataReader对象 </returns>
public static DataSet ExecuteDataSet(string procName)
{
SqlCommand cmd = new SqlCommand();
SqlConnection con = GetConObject();
DataSet ds = new DataSet();

try
{
CreateCommand(cmd, con, null, procName, null);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
cmd.Parameters.Clear();
return ds;
}
catch(Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
Close(con);
}
}
}
------解决方案--------------------
上网搜一下:Microsoft.ApplicationBlocks.Data
------解决方案--------------------
MS最近的几个例子好象都是没存储过程的
------解决方案--------------------
我这儿有个不用存储过程的,需要自已定义SQl语句

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

namespace Aptech.Student.Common
{

public class DataBaseOperate:IDisposable
{
// 保存数据库连接字符串
private string m_sConnectionString;

// 数据库连接标志
private bool m_bConnected;

// 存储过程开启标志
private bool m_bTransaction;


//数据库连接对象
private SqlConnection m_sqlConnection;
private SqlCommand m_sqlCommand;
private SqlTransaction m_sqlTransaction;
private SqlDataAdapter m_sqlDataAdapter;

/// <summary>
/// 构造函数,初始化内部变量
/// </summary>
public DataBaseOperate()
:this( "user id=sa;password=510902;data source=\ "YAOYANG\\YAOYANG\ ";initial catalog=Student ")

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有