ASP.NET封裝的SQL資料庫訪問類

來源:互聯網
上載者:User

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;
}
}
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.