码迷,mamicode.com
首页 > 数据库 > 详细

using ADO.DB

时间:2016-06-27 10:20:28      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

1.add new class.

技术分享

2.write below code into this class:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kerry.K3.DB.Utility
{
public class DbHelper
{
private static string dbProviderName = ConfigurationManager.AppSettings["DataProvider"]; //get from config;
private static string dbConnectionString = ConfigurationManager.ConnectionStrings["K3UatEntitiesADO"].ToString();


private DbConnection connection;
public DbHelper()
{
this.connection = CreateConnection(DbHelper.dbConnectionString);
}
public DbHelper(string connectionString)
{
this.connection = CreateConnection(connectionString);
}
public static DbConnection CreateConnection()
{
DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbHelper.dbProviderName);
DbConnection dbconn = dbfactory.CreateConnection();
dbconn.ConnectionString = DbHelper.dbConnectionString;
return dbconn;
}
public static DbConnection CreateConnection(string connectionString)
{
DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbHelper.dbProviderName);
DbConnection dbconn = dbfactory.CreateConnection();
dbconn.ConnectionString = connectionString;
return dbconn;
}

public DbCommand GetStoredProcCommond(string storedProcedure)
{
DbCommand dbCommand = connection.CreateCommand();
dbCommand.CommandText = storedProcedure;
dbCommand.CommandType = CommandType.StoredProcedure;
return dbCommand;
}
public DbCommand GetSqlStringCommond(string sqlQuery)
{
DbCommand dbCommand = connection.CreateCommand();
dbCommand.CommandText = sqlQuery;
dbCommand.CommandType = CommandType.Text;
return dbCommand;
}

#region Add Parameter
public void AddParameterCollection(DbCommand cmd, DbParameterCollection dbParameterCollection)
{
foreach (DbParameter dbParameter in dbParameterCollection)
{
cmd.Parameters.Add(dbParameter);
}
}
public void AddOutParameter(DbCommand cmd, string parameterName, DbType dbType, int size)
{
DbParameter dbParameter = cmd.CreateParameter();
dbParameter.DbType = dbType;
dbParameter.ParameterName = parameterName;
dbParameter.Size = size;
dbParameter.Direction = ParameterDirection.Output;
cmd.Parameters.Add(dbParameter);
}
public void AddInParameter(DbCommand cmd, string parameterName, DbType dbType, object value)
{
DbParameter dbParameter = cmd.CreateParameter();
dbParameter.DbType = dbType;
dbParameter.ParameterName = parameterName;
dbParameter.Value = value;
dbParameter.Direction = ParameterDirection.Input;
cmd.Parameters.Add(dbParameter);
}
public void AddReturnParameter(DbCommand cmd, string parameterName, DbType dbType)
{
DbParameter dbParameter = cmd.CreateParameter();
dbParameter.DbType = dbType;
dbParameter.ParameterName = parameterName;
dbParameter.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(dbParameter);
}
public DbParameter GetParameter(DbCommand cmd, string parameterName)
{
return cmd.Parameters[parameterName];
}

#endregion

#region Execute
public DataSet ExecuteDataSet(DbCommand cmd)
{
//temporary set connection time out 6000 seconds
cmd.CommandTimeout = 6000;
DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbHelper.dbProviderName);
DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
dbDataAdapter.SelectCommand = cmd;
DataSet ds = new DataSet();
dbDataAdapter.Fill(ds);
return ds;
}

public DataTable ExecuteDataTable(DbCommand cmd)
{
DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbHelper.dbProviderName);
DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
dbDataAdapter.SelectCommand = cmd;
DataTable dataTable = new DataTable();
dbDataAdapter.Fill(dataTable);
return dataTable;
}

public DbDataReader ExecuteReader(DbCommand cmd)
{
cmd.Connection.Open();
DbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
public int ExecuteNonQuery(DbCommand cmd)
{
cmd.Connection.Open();
int ret = cmd.ExecuteNonQuery();
cmd.Connection.Close();
return ret;
}

public object ExecuteScalar(DbCommand cmd)
{
cmd.Connection.Open();
object ret = cmd.ExecuteScalar();
cmd.Connection.Close();
return ret;
}
#endregion

#region Execute transaction
public DataSet ExecuteDataSet(DbCommand cmd, Trans t)
{
cmd.Connection = t.DbConnection;
cmd.Transaction = t.DbTrans;
DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbHelper.dbProviderName);
DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
dbDataAdapter.SelectCommand = cmd;
DataSet ds = new DataSet();
dbDataAdapter.Fill(ds);
return ds;
}

public DataTable ExecuteDataTable(DbCommand cmd, Trans t)
{
cmd.Connection = t.DbConnection;
cmd.Transaction = t.DbTrans;
DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DbHelper.dbProviderName);
DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
dbDataAdapter.SelectCommand = cmd;
DataTable dataTable = new DataTable();
dbDataAdapter.Fill(dataTable);
return dataTable;
}

public DbDataReader ExecuteReader(DbCommand cmd, Trans t)
{
cmd.Connection.Close();
cmd.Connection = t.DbConnection;
cmd.Transaction = t.DbTrans;
DbDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
return reader;
}
public int ExecuteNonQuery(DbCommand cmd, Trans t)
{
cmd.Connection.Close();
cmd.Connection = t.DbConnection;
cmd.Transaction = t.DbTrans;
int ret = cmd.ExecuteNonQuery();
return ret;
}

public object ExecuteScalar(DbCommand cmd, Trans t)
{
cmd.Connection.Close();
cmd.Connection = t.DbConnection;
cmd.Transaction = t.DbTrans;
object ret = cmd.ExecuteScalar();
return ret;
}
#endregion
}

public class Trans : IDisposable
{
private DbConnection conn;
private DbTransaction dbTrans;
public DbConnection DbConnection
{
get { return this.conn; }
}
public DbTransaction DbTrans
{
get { return this.dbTrans; }
}

public Trans()
{
conn = DbHelper.CreateConnection();
conn.Open();
dbTrans = conn.BeginTransaction();
}
public Trans(string connectionString)
{
conn = DbHelper.CreateConnection(connectionString);
conn.Open();
dbTrans = conn.BeginTransaction();
}
public void Commit()
{
dbTrans.Commit();
this.Colse();
}

public void RollBack()
{
dbTrans.Rollback();
this.Colse();
}

public void Dispose()
{
this.Colse();
}

public void Colse()
{
if (conn.State == System.Data.ConnectionState.Open)
{
conn.Close();
}
}
}
}

 

3.add the db Link into config:

技术分享

eg:

<add name="K3UatEntitiesADO" connectionString="DATA SOURCE=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=172.18.9.141)(PORT=1521))(CONNECT_DATA=(SID=KFFUAT)));password=ta45uguh;persist security info=True;user id=KWDDEV" providerName="Oracle.ManagedDataAccess.Client" />
<add name="K35IntfEntitiesADO" connectionString="DATA SOURCE=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=KLHKHQSRV410)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=k3prod_srv)));password=RexEvaw2;persist security info=True;user id=K35INTF" providerName="Oracle.ManagedDataAccess.Client" />

using ADO.DB

标签:

原文地址:http://www.cnblogs.com/khongdk/p/5619238.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!