程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> c# SQLHelper(for winForm)實現代碼

c# SQLHelper(for winForm)實現代碼

編輯:C#基礎知識
SQLHelper.cs
代碼如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace HelloWinForm.DBUtility
{
class SQLHelper
{
#region 通用方法
// 數據連接池
private SqlConnection con;
/// <summary>
/// 返回數據庫連接字符串
/// </summary>
/// <returns></returns>
public static String GetSqlConnection()
{
String conn = ConfigurationManager.AppSettings["connectionString"].ToString();
return conn;
}
#endregion
#region 執行sql字符串
/// <summary>
/// 執行不帶參數的SQL語句
/// </summary>
/// <param name="Sqlstr"></param>
/// <returns></returns>
public static int ExecuteSql(String Sqlstr)
{
String ConnStr = GetSqlConnection();
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
return 1;
}
}
/// <summary>
/// 執行帶參數的SQL語句
/// </summary>
/// <param name="Sqlstr">SQL語句</param>
/// <param name="param">參數對象數組</param>
/// <returns></returns>
public static int ExecuteSql(String Sqlstr, SqlParameter[] param)
{
String ConnStr = GetSqlConnection();
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
cmd.Parameters.AddRange(param);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
return 1;
}
}
/// <summary>
/// 返回DataReader
/// </summary>
/// <param name="Sqlstr"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(String Sqlstr)
{
String ConnStr = GetSqlConnection();
SqlConnection conn = new SqlConnection(ConnStr);//返回DataReader時,是不可以用using()的
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
conn.Open();
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);//關閉關聯的Connection
}
catch //(Exception ex)
{
return null;
}
}
/// <summary>
/// 執行SQL語句並返回數據表
/// </summary>
/// <param name="Sqlstr">SQL語句</param>
/// <returns></returns>
public static DataTable ExecuteDt(String Sqlstr)
{
String ConnStr = GetSqlConnection();
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
DataTable dt = new DataTable();
conn.Open();
da.Fill(dt);
conn.Close();
return dt;
}
}
/// <summary>
/// 執行SQL語句並返回DataSet
/// </summary>
/// <param name="Sqlstr">SQL語句</param>
/// <returns></returns>
public static DataSet ExecuteDs(String Sqlstr)
{
String ConnStr = GetSqlConnection();
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
conn.Close();
return ds;
}
}
#endregion
#region 操作存儲過程
/// <summary>
/// 運行存儲過程(已重載)
/// </summary>
/// <param name="procName">存儲過程的名字</param>
/// <returns>存儲過程的返回值</returns>
public int RunProc(string procName)
{
SqlCommand cmd = CreateCommand(procName, null);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}
/// <summary>
/// 運行存儲過程(已重載)
/// </summary>
/// <param name="procName">存儲過程的名字</param>
/// <param name="prams">存儲過程的輸入參數列表</param>
/// <returns>存儲過程的返回值</returns>
public int RunProc(string procName, SqlParameter[] prams)
{
SqlCommand cmd = CreateCommand(procName, prams);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters[0].Value;
}
/// <summary>
/// 運行存儲過程(已重載)
/// </summary>
/// <param name="procName">存儲過程的名字</param>
/// <param name="dataReader">結果集</param>
public void RunProc(string procName, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, null);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
/// <summary>
/// 運行存儲過程(已重載)
/// </summary>
/// <param name="procName">存儲過程的名字</param>
/// <param name="prams">存儲過程的輸入參數列表</param>
/// <param name="dataReader">結果集</param>
public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, prams);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
/// <summary>
/// 創建Command對象用於訪問存儲過程
/// </summary>
/// <param name="procName">存儲過程的名字</param>
/// <param name="prams">存儲過程的輸入參數列表</param>
/// <returns>Command對象</returns>
private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
// 確定連接是打開的
Open();
//command = new SqlCommand( sprocName, new SqlConnection( ConfigManager.DALConnectionString ) );
SqlCommand cmd = new SqlCommand(procName, con);
cmd.CommandType = CommandType.StoredProcedure;
// 添加存儲過程的輸入參數列表
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
// 返回Command對象
return cmd;
}
/// <summary>
/// 創建輸入參數
/// </summary>
/// <param name="ParamName">參數名</param>
/// <param name="DbType">參數類型</param>
/// <param name="Size">參數大小</param>
/// <param name="Value">參數值</param>
/// <returns>新參數對象</returns>
public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}
/// <summary>
/// 創建輸出參數
/// </summary>
/// <param name="ParamName">參數名</param>
/// <param name="DbType">參數類型</param>
/// <param name="Size">參數大小</param>
/// <returns>新參數對象</returns>
public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
/// <summary>
/// 創建存儲過程參數
/// </summary>
/// <param name="ParamName">參數名</param>
/// <param name="DbType">參數類型</param>
/// <param name="Size">參數大小</param>
/// <param name="Direction">參數的方向(輸入/輸出)</param>
/// <param name="Value">參數值</param>
/// <returns>新參數對象</returns>
public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
SqlParameter param;
if (Size > 0)
{
param = new SqlParameter(ParamName, DbType, Size);
}
else
{
param = new SqlParameter(ParamName, DbType);
}
param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
{
param.Value = Value;
}
return param;
}
#endregion
#region 數據庫連接和關閉
/// <summary>
/// 打開連接池
/// </summary>
private void Open()
{
// 打開連接池
if (con == null)
{
//這裡不僅需要using System.Configuration;還要在引用目錄裡添加
con = new SqlConnection(GetSqlConnection());
con.Open();
}
}
/// <summary>
/// 關閉連接池
/// </summary>
public void Close()
{
if (con != null)
con.Close();
}
/// <summary>
/// 釋放連接池
/// </summary>
public void Dispose()
{
// 確定連接已關閉
if (con != null)
{
con.Dispose();
con = null;
}
}
#endregion
}
}

簡單用一下:
代碼如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using HelloWinForm.DBUtility;
namespace HelloWinForm.DAL
{
class Student
{
public string test()
{
string str = "";
SqlDataReader dr = SQLHelper.ExecuteReader("select * from Student");
while (dr.Read())
{
str += dr["StudentNO"].ToString();
}
dr.Close();
return str;
}
}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved