一直在寫管理系統,需要長期與資料庫讀寫打交道。之前用的都是老土的辦法,最近突然發現了諸如 sqlhelper 之類的通用庫。搞了幾個用用。還挺不錯的。
但是我現在有一個想法,這些庫都是不需要new就可以用的。我想改成需要new,然后輸入引數,這樣我可以定義很多個,一個程式可以方便訪問多個資料庫。
源代碼如下:
using System;
using System.Data;
using System.Configuration.Assemblies;
using System.Configuration;
using System.Web;
using System.Data.OleDb;
namespace 加彈機工序展示系統
{
/// <summary>
/// AccessHelper 的摘要說明
/// </summary>
public class AccessHelper
{
protected static OleDbConnection conn = new OleDbConnection();
protected static OleDbCommand comm = new OleDbCommand();
public AccessHelper()
{
//
// TODO: 在此處添加建構式邏輯
//
}
/// <summary>
/// 打開資料庫
/// </summary>
private static void openConnection()
{
if (conn.State == ConnectionState.Closed)
{
try
{
conn.Open();
}
catch (Exception e)
{ throw new Exception(e.Message); }
}
}
/// <summary>
/// 關閉資料庫
/// </summary>
private static void closeConnection()
{
conn.ConnectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={AppDomain.CurrentDomain.BaseDirectory}\\{FileName}.accdb;Jet OLEDB:Database PassWord={Password}";
comm.Connection = conn;
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
comm.Dispose();
}
}
/// <summary>
/// 執行sql陳述句
/// </summary>
/// <param name="sqlstr"></param>
public static void excuteSql(string sqlstr)
{
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
comm.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{ closeConnection(); }
}
/// <summary>
/// 回傳指定sql陳述句的OleDbDataReader物件,使用時請注意關閉這個物件。
/// </summary>
/// <param name="sqlstr"></param>
/// <returns></returns>
public static OleDbDataReader dataReader(string sqlstr)
{
OleDbDataReader dr = null;
try
{
openConnection();
comm.CommandText = sqlstr;
comm.CommandType = CommandType.Text;
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
try
{
dr.Close();
closeConnection();
}
catch { }
}
return dr;
}
/// <summary>
/// 回傳指定sql陳述句的OleDbDataReader物件,使用時請注意關閉
/// </summary>
/// <param name="sqlstr"></param>
/// <param name="dr"></param>
public static void dataReader(string sqlstr, ref OleDbDataReader dr)
{
try
{
openConnection();
comm.CommandText = sqlstr;
comm.CommandType = CommandType.Text;
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
try
{
if (dr != null && !dr.IsClosed)
dr.Close();
}
catch
{
}
finally
{
closeConnection();
}
}
}
/// <summary>
/// 回傳指定sql陳述句的dataset
/// </summary>
/// <param name="sqlstr"></param>
/// <returns></returns>
public static DataSet dataSet(string sqlstr)
{
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return ds;
}
/// <summary>
/// 回傳指定sql陳述句的dataset
/// </summary>
/// <param name="sqlstr"></param>
/// <param name="ds"></param>
public static void dataSet(string sqlstr, ref DataSet ds)
{
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
/// <summary>
/// 回傳指定sql陳述句的datatable
/// </summary>
/// <param name="sqlstr"></param>
/// <returns></returns>
public static DataTable dataTable(string sqlstr)
{
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return dt;
}
/// <summary>
/// 回傳指定sql陳述句的datatable
/// </summary>
/// <param name="sqlstr"></param>
/// <param name="dt"></param>
public static void dataTable(string sqlstr, ref DataTable dt)
{
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}
/// <summary>
/// 回傳指定sql陳述句的dataview
/// </summary>
/// <param name="sqlstr"></param>
/// <returns></returns>
public static DataView dataView(string sqlstr)
{
OleDbDataAdapter da = new OleDbDataAdapter();
DataView dv = new DataView();
DataSet ds = new DataSet();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
dv = ds.Tables[0].DefaultView;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return dv;
}
}
}
上面這個庫,我可以直接用 AccessHelper.dataReader(sql)
但是我想改成比如:
AccessHelper access1 = new AccessHelper("shujuku1");
AccessHelper access2 = new AccessHelper("shujuku2");
然后:
Datatable a1 = access1.dataTable(sql);
Datatable a2 = access2.dataTable(sql);
請各位大神幫幫忙!
uj5u.com熱心網友回復:
把方法static去掉就可以了,因為方法是靜態的,所以不用newuj5u.com熱心網友回復:
你使用new實體化類的時候都是呼叫了這個類的“建構式”。建構式就是那個方法名跟類名一樣,又沒有回傳值的那個,例:
public AccessHelper()
{
//
// TODO: 在此處添加建構式邏輯
//
}
如果你在里面加多載,就能傳引數過去,例如在下面加一個方法:
public AccessHelper(string aaa)
{
//
// TODO: 在此處添加建構式邏輯
//
}
呼叫這個類的時候就能:
AccessHelper ah=new AccessHelper("xxxx");
uj5u.com熱心網友回復:
你補習一下基礎知識。static去掉。你就知道了。
uj5u.com熱心網友回復:
初學者路過~~uj5u.com熱心網友回復:
13年你就提這種問題
uj5u.com熱心網友回復:
建構式傳參唄uj5u.com熱心網友回復:
sqlhelper并不會影響你連接多個資料庫。如果作為通用庫的sqlhelper都支持不了同時連接多個資料庫,那還叫通用庫嗎?
uj5u.com熱心網友回復:
去掉static即可uj5u.com熱心網友回復:
13年 還能問這問題......... 唉uj5u.com熱心網友回復:
加油!!基礎忘了,或不理解,正常。知之為知之,不懂裝懂才致命。王者是把專案搞定,領導信任你,不停的加薪加薪!!!
uj5u.com熱心網友回復:
沒仔細看你的提問,你要訪問多個資料庫,就多些幾個sqlhelper,比如訪問mysql,就增加MySqlHelper,訪問本地快取資料庫sqlite,就增加SQLiteHelper轉載請註明出處,本文鏈接:https://www.uj5u.com/net/77775.html
標籤:C#
上一篇:C#編程
