ASP.NET封装的SQL数据库访问类

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace MyCorporation.DepartMent.DataBase
{
/// <summary>
/// 通用数据库类
/// </summary>
public class DataBase
{
private string ConnStr = null;
public DataBase()
{
ConnStr = ConfigurationSettings.AppSettings["ConnStr"];
}
public DataBase(string Str)
{
try
{
this.ConnStr = Str;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 返回connection对象
/// </summary>
/// <returns></returns>
public SqlConnection ReturnConn()
{
SqlConnection Conn = new SqlConnection(ConnStr);
Conn.Open();
return Conn;
}
public void Dispose(SqlConnection Conn)
{
if (Conn != null)
{
Conn.Close();
Conn.Dispose();
}
GC.Collect();
}
/// <summary>
/// 运行SQL语句
/// </summary>
/// <param name="SQL"></param>
public void RunProc(string SQL)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlCommand Cmd;
Cmd = CreateCmd(SQL, Conn);
try
{
Cmd.ExecuteNonQuery();
}
catch
{
throw new Exception(SQL);
}
Dispose(Conn);
return;
}
/// <summary>
/// 运行SQL语句返回DataReader
/// </summary>
/// <param name="SQL"></param>
/// <returns>SqlDataReader对象.</returns>
public SqlDataReader RunProcGetReader(string SQL)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlCommand Cmd;
Cmd = CreateCmd(SQL, Conn);
SqlDataReader Dr;
try
{
Dr = Cmd.ExecuteReader(CommandBehavior.Default);
}
catch
{
throw new Exception(SQL);
}
//Dispose(Conn);
return Dr;
}
/// <summary>
/// 生成Command对象
/// </summary>
/// <param name="SQL"></param>
/// <param name="Conn"></param>
/// <returns></returns>
public SqlCommand CreateCmd(string SQL, SqlConnection Conn)
{
SqlCommand Cmd;
Cmd = new SqlCommand(SQL, Conn);
return Cmd;
}
/// <summary>
/// 生成Command对象
/// </summary>
/// <param name="SQL"></param>
/// <returns></returns>
public SqlCommand CreateCmd(string SQL)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlCommand Cmd;
Cmd = new SqlCommand(SQL, Conn);
return Cmd;
}
/// <summary>
/// 返回adapter对象
/// </summary>
/// <param name="SQL"></param>
/// <param name="Conn"></param>
/// <returns></returns>
public SqlDataAdapter CreateDa(string SQL)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlDataAdapter Da;
Da = new SqlDataAdapter(SQL, Conn);
return Da;
}
/// <summary>
/// 运行SQL语句,返回DataSet对象
/// </summary>
/// <param name="procName">SQL语句</param>
/// <param name="prams">DataSet对象</param>
public DataSet RunProc(string SQL, DataSet Ds)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlDataAdapter Da;
//Da = CreateDa(SQL, Conn);
Da = new SqlDataAdapter(SQL, Conn);
try
{
Da.Fill(Ds);
}
catch (Exception Err)
{
throw Err;
}
Dispose(Conn);
return Ds;
}
/// <summary>
/// 运行SQL语句,返回DataSet对象
/// </summary>
/// <param name="procName">SQL语句</param>
/// <param name="prams">DataSet对象</param>
/// <param name="dataReader">表名</param>
public DataSet RunProc(string SQL, DataSet Ds, string tablename)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlDataAdapter Da;
Da = CreateDa(SQL);
try
{
Da.Fill(Ds, tablename);
}
catch (Exception Ex)
{
throw Ex;
}
Dispose(Conn);
return Ds;
}
/// <summary>
/// 运行SQL语句,返回DataSet对象
/// </summary>
/// <param name="procName">SQL语句</param>
/// <param name="prams">DataSet对象</param>
/// <param name="dataReader">表名</param>
public DataSet RunProc(string SQL, DataSet Ds, int StartIndex, int PageSize, string tablename)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlDataAdapter Da;
Da = CreateDa(SQL);
try
{
Da.Fill(Ds, StartIndex, PageSize, tablename);
}
catch (Exception Ex)
{
throw Ex;
}
Dispose(Conn);
return Ds;
}
/// <summary>
/// 检验是否存在数据
/// </summary>
/// <returns></returns>
public bool ExistDate(string SQL)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlDataReader Dr;
Dr = CreateCmd(SQL, Conn).ExecuteReader();
if (Dr.Read())
{
Dispose(Conn);
return true;
}
else
{
Dispose(Conn);
return false;
}
}
/// <summary>
/// 返回SQL语句执行结果的第一行第一列
/// </summary>
/// <returns>字符串</returns>
public string ReturnValue(string SQL)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
string result;
SqlDataReader Dr;
try
{
Dr = CreateCmd(SQL, Conn).ExecuteReader();
if (Dr.Read())
{
result = Dr[0].ToString();
Dr.Close();
}
else
{
result = "";
Dr.Close();
}
}
catch
{
throw new Exception(SQL);
}
Dispose(Conn);
return result;
}
/// <summary>
/// 返回SQL语句第一列,第ColumnI列,
/// </summary>
/// <returns>字符串</returns>
public string ReturnValue(string SQL, int ColumnI)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
string result;
SqlDataReader Dr;
try
{
Dr = CreateCmd(SQL, Conn).ExecuteReader();
}
catch
{
throw new Exception(SQL);
}
if (Dr.Read())
{
result = Dr[ColumnI].ToString();
}
else
{
result = "";
}
Dr.Close();
Dispose(Conn);
return result;
}
/// <summary>
/// 生成一个存储过程使用的sqlcommand.
/// </summary>
/// <param name="procName">存储过程名.</param>
/// <param name="prams">存储过程入参数组.</param>
/// <returns>sqlcommand对象.</returns>
public SqlCommand CreateCmd(string procName, SqlParameter[] prams)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlCommand Cmd = new SqlCommand(procName, Conn);
Cmd.CommandType = CommandType.StoredProcedure;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
{
if (parameter != null)
{
Cmd.Parameters.Add(parameter);
}
}
}
return Cmd;
}
/// <summary>
/// 为存储过程生成一个SqlCommand对象
/// </summary>
/// <param name="procName">存储过程名</param>
/// <param name="prams">存储过程参数</param>
/// <returns>SqlCommand对象</returns>
private SqlCommand CreateCmd(string procName, SqlParameter[] prams, SqlDataReader Dr)
{
SqlConnection Conn;
Conn = new SqlConnection(ConnStr);
Conn.Open();
SqlCommand Cmd = new SqlCommand(procName, Conn);
Cmd.CommandType = CommandType.StoredProcedure;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
Cmd.Parameters.Add(parameter);
}
Cmd.Parameters.Add(
new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null));
return Cmd;
}
/// <summary>
/// 运行存储过程,返回.
/// </summary>
/// <param name="procName">存储过程名</param>
/// <param name="prams">存储过程参数</param>
/// <param name="dataReader">SqlDataReader对象</param>
public void RunProc(string procName, SqlParameter[] prams, SqlDataReader Dr)
{
SqlCommand Cmd = CreateCmd(procName, prams, Dr);
Dr = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
return;
}
/// <summary>
/// 运行存储过程,返回.
/// </summary>
/// <param name="procName">存储过程名</param>
/// <param name="prams">存储过程参数</param>
public string RunProc(string procName, SqlParameter[] prams)
{
SqlDataReader Dr;
SqlCommand Cmd = CreateCmd(procName, prams);
Dr = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
if (Dr.Read())
{
return Dr.GetValue(0).ToString();
}
else
{
return "";
}
}
/// <summary>
/// 运行存储过程,返回dataset.
/// </summary>
/// <param name="procName">存储过程名.</param>
/// <param name="prams">存储过程入参数组.</param>
/// <returns>dataset对象.</returns>
public DataSet RunProc(string procName, SqlParameter[] prams, DataSet Ds)
{
SqlCommand Cmd = CreateCmd(procName, prams);
SqlDataAdapter Da = new SqlDataAdapter(Cmd);
try
{
Da.Fill(Ds);
}
catch (Exception Ex)
{
throw Ex;
}
return Ds;
}
}
}

(0)

相关推荐

  • ASP.NET数据库操作类实例

    本文实例讲述了ASP.NET数据库操作类.分享给大家供大家参考,具体如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Syst

  • ASP.NET2.0 SQL Server数据库连接详解

    Connection类有四种:SqlConnection,OleDbConnection,OdbcConnection和OracleConnection. SqlConnection类的对象连接SQL Server数据库:OracleConnection 类的对象连接Oracle数据库: OleDbConnection类的对象连接支持OLE DB的数据库,如Access:而OdbcConnection类的对象连接任何支持ODBC的数据库.与数据库的所有通讯最终都是通过Connection对象来完

  • asp.net Oracle数据库访问操作类

    复制代码 代码如下: using System;using System.Collections;using System.Collections.Specialized;using System.Data;using System.Data.OracleClient;using System.Configuration;using System.Data.Common;using System.Collections.Generic; /// <summary>    /// 数据访问抽象基

  • asp.net连接查询SQL数据库并把结果显示在网页上(2种方法)

    在ASP.NET中,使用C#连接SQL数据库,并使用SQL语句查询,以前从来没有接触过C#,最近用到了,摸索了两天终于运行起来了,Mark一下,不喜勿喷 有两种方法:(说的是第一种方法不安全,我也不清楚^_^) 第一种方法: 复制代码 代码如下: //建立ASP.NET Web 应用程序,直接在Page_load函数中加入一下代码,貌似就可以用了 public void Page_Load(object sender, EventArgs e) { using (SqlConnection co

  • ASP.NET对SQLServer的通用数据库访问类

    本文模仿实现数据库访问的通用类,代码清晰,而且很实用,包括了对数据库的所有的常用的操作. /// <summary> /// 数据库访问通用类 /// </summary> public class SqlHelper { private string connectionString; /// <summary> /// 设定数据库访问字符串 /// </summary> public string ConnectionString { set { con

  • ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法

    在ASP.NET开发的网站根目录,有一个名为web.config的文件,顾名思义,这是为整个网站进行配置的文件,其格式为XML格式.这里主要谈谈文件中的<connectionStrings>节. <connectionStrings>节是对连接到数据库的字符串进行配置,由于MS SQL Server与ASP.NET同属于微软的产品,因此是使用ASP.NET开发时首选的数据库是MS SQL Server,本文只讨论对MS SQL Server的连接字符串情况.第一种情况,本地开发时,

  • asp.net下Oracle,SQL Server,Access万能数据库通用类

    复制代码 代码如下: using System; using System.Collections; using System.Collections.Specialized; using System.Data; using System.Data.SqlClient; using System.Data.OleDb; using System.Data.OracleClient; using System.Configuration; using System.Reflection; nam

  • 一个ASP.NET的MYSQL的数据库操作类自己封装的

    /** * 类说明:对MYSQL数据库的操作类 */ using System; using System.Data; using MySql.Data.MySqlClient; namespace Niunan.BYLW.Utility { /// <summary>对MYSQL数据库的操作类 /// /// </summary> public class MYSQLHelper { private MySqlConnection conn = null; private MyS

  • ASP.NET 6种常用数据库的连接方法

    1.C#连接连接Access 程序代码:     复制代码 代码如下: using   System.Data; using   System.Data.OleDb;    .. string   strConnection="Provider=Microsoft.Jet.OleDb.4.0;";     strConnection+=@"Data   Source=C:BegASPNETNorthwind.mdb"; OleDbConnection   objCo

  • asp.net 数据库的连接和datatable类

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.D

  • asp.net下使用DbProviderFactories的数据库操作类

    封装数据库操作,并且提供事务处理. 复制代码 代码如下: 使用DbProviderFactories的数据库操作类 Imports System.Data Imports System.Configuration Imports System.Data.Common '******************************************************************* '* Page/Class Name:XPDBHelper.vb '* Title:使用DbP

  • asp.net 数据库连接类代码(SQL)

    复制代码 代码如下: public class SqlOperation { #region 属性 /// <summary> /// 保存在Web.config中的连接字符串 /// </summary> protected static string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["hao"].ConnectionString; /

  • Asp.net把图片存入数据库和读取图片的方法

    网上关于ASP.NET上传图片到数据库的资料非常多,常用的如下:存储图片类型数据有以下几种方式:1.将图片转换为二进制数组(byte[]) 复制代码 代码如下: byte[] fileData = this.FileUpload1.FileBytes; 2. 根据路径将文件转换为2进制数组 复制代码 代码如下: 代码 public byte[] returnbyte(string strpath){ // 以二进制方式读文件    FileStream fsMyfile = new FileSt

随机推荐