程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> .NET經典的OracleHelper

.NET經典的OracleHelper

編輯:關於C#
 

using System;

using System.Data;

using System.Configuration;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data;

using System.Reflection;

using System.Configuration;

using System.Data.OracleClient;

using System.Collections.Generic;


namespace WebApplication1

{

/**

* 寫程序之前

* 首先引用System.Data.OracleClient

*/

public class OracleHelper

{

#region 變量

/// <summary>

/// 數據庫連接對象

/// </summary>

private static OracleConnection _con = null;

public static string constr = "數據庫連接地址";

#endregion


#region 屬性

/// <summary>

/// 獲取或設置數據庫連接對象

/// </summary>

public static OracleConnection Con

{

get

{

if (OracleHelper._con == null)

{

OracleHelper._con = new OracleConnection();

}

if (OracleHelper._con.ConnectionString == "")

{

OracleHelper._con.ConnectionString = OracleHelper.constr;

}

return OracleHelper._con;

}

set

{

OracleHelper._con = value;

}

}

#endregion


#region 方法

#region 執行返回一行一列的數據庫操作

/// <summary>

/// 執行返回一行一列的數據庫操作

/// </summary>

/// <param name="commandText">Oracle語句或存儲過程名</param>

/// <param name="commandType">Oracle命令類型</param>

/// <param name="param">Oracle命令參數數組</param>

/// <returns>第一行第一列的記錄</returns>

public static int ExecuteScalar(string commandText, CommandType commandType, params OracleParameter[] param)

{

int count = 0;

using (OracleHelper.Con)

{

using (OracleCommand cmd = new OracleCommand(commandText, OracleHelper.Con))

{

try

{

cmd.CommandType = commandType;

cmd.Parameters.AddRange(param);

OracleHelper.Con.Open();

count = Convert.ToInt32(cmd.ExecuteScalar());

}

catch (Exception ex)

{

count = 0;

}

}


}

return count;

}

#endregion


#region 執行不查詢的數據庫操作

/// <summary>

/// 執行不查詢的數據庫操作

/// </summary>

/// <param name="commandText">Oracle語句或存儲過程名</param>

/// <param name="commandType">Oracle命令類型</param>

/// <param name="param">Oracle命令參數數組</param>

/// <returns>受影響的行數</returns>

public static int ExecuteNonQuery(string commandText, CommandType commandType, params OracleParameter[] param)

{

int result = 0;

using (OracleHelper.Con)

{

using (OracleCommand cmd = new OracleCommand(commandText, OracleHelper.Con))

{

try

{

cmd.CommandType = commandType;

cmd.Parameters.AddRange(param);

OracleHelper.Con.Open();

result = cmd.ExecuteNonQuery();

}

catch (Exception ex)

{

result = 0;

}

}


}

return result;

}

#endregion


#region 執行返回一條記錄的泛型對象

/// <summary>

/// 執行返回一條記錄的泛型對象

/// </summary>

/// <typeparam name="T">泛型類型</typeparam>

/// <param name="reader">只進只讀對象</param>

/// <returns>泛型對象</returns>

private static T ExecuteDataReader<T>(IDataReader reader)

{

T obj = default(T);

try

{

Type type = typeof(T);

obj = (T)Activator.CreateInstance(type);//從當前程序集裡面通過反射的方式創建指定類型的對象

//obj = (T)Assembly.Load(OracleHelper._assemblyName).CreateInstance(OracleHelper._assemblyName + "." + type.Name);//從另一個程序集裡面通過反射的方式創建指定類型的對象

PropertyInfo[] propertyInfos = type.GetProperties();//獲取指定類型裡面的所有屬性

foreach (PropertyInfo propertyInfo in propertyInfos)

{

for (int i = 0; i < reader.FieldCount; i++)

{

string fieldName = reader.GetName(i);

if (fieldName.ToLower() == propertyInfo.Name.ToLower())

{

object val = reader[propertyInfo.Name];//讀取表中某一條記錄裡面的某一列信息

if (val != null && val != DBNull.Value)

propertyInfo.SetValue(obj, val, null);//給對象的某一個屬性賦值

break;

}

}

}

}

catch (Exception ex)

{

}

return obj;

}

#endregion


#region 執行返回一條記錄的泛型對象

/// <summary>

/// 執行返回一條記錄的泛型對象

/// </summary>

/// <typeparam name="T">泛型類型</typeparam>

/// <param name="commandText">Oracle語句或存儲過程名</param>

/// <param name="commandType">Oracle命令類型</param>

/// <param name="param">Oracle命令參數數組</param>

/// <returns>實體對象</returns>

public static T ExecuteEntity<T>(string commandText, CommandType commandType, params OracleParameter[] param)

{

T obj = default(T);

using (OracleHelper.Con)

{

using (OracleCommand cmd = new OracleCommand(commandText, OracleHelper.Con))

{

cmd.CommandType = commandType;

cmd.Parameters.AddRange(param);

OracleHelper.Con.Open();

OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

while (reader.Read())

{

obj = OracleHelper.ExecuteDataReader<T>(reader);

}

}

}

return obj;

}

#endregion


#region 執行返回多條記錄的泛型集合對象

/// <summary>

/// 執行返回多條記錄的泛型集合對象

/// </summary>

/// <typeparam name="T">泛型類型</typeparam>

/// <param name="commandText">Oracle語句或存儲過程名</param>

/// <param name="commandType">Oracle命令類型</param>

/// <param name="param">Oracle命令參數數組</param>

/// <returns>泛型集合對象</returns>

public static List<T> ExecuteList<T>(string commandText, CommandType commandType, params OracleParameter[] param)

{

List<T> list = new List<T>();

using (OracleHelper.Con)

{

using (OracleCommand cmd = new OracleCommand(commandText, OracleHelper.Con))

{

try

{

cmd.CommandType = commandType;

cmd.Parameters.AddRange(param);

OracleHelper.Con.Open();

OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

while (reader.Read())

{


T obj = OracleHelper.ExecuteDataReader<T>(reader);

list.Add(obj);

}

}

catch (Exception ex)

{

list = null;

}

}

}

return list;

}

#endregion

#endregion

}

}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved