VB.NET连接Oracle共通模块

前端之家收集整理的这篇文章主要介绍了VB.NET连接Oracle共通模块前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用方法

1、在解决方案的引用中添加以下引用
Imports system.Data.OracleClient

2、将DBLink.vb添加到现有项目中

———————————————————华丽的分割线—————————————————————

OptionStrictOff

OptionExplicitOn

ImportsSystem.Data.OracleClient

ModuleDBLink

#Region"自定义常量及变量"

PrivateOra_ConnectionAsNewOracleConnection

PrivateOra_CommandAsNewOracleCommand

PrivateStartTimeAsDateTime

PrivateEndTimeAsDateTime

PrivateTimeSpanAsNewTimeSpan

PrivateOra_DataAdapterAsOracleDataAdapter

PrivateOra_TransactionAsOracleTransaction

#EndRegion

#Region"打开数据库连接"

'''<summary>

'''打开数据库连接

'''</summary>

'''<remarks></remarks>

PublicSubOpen()

Try

Ora_Connection=NewOracleConnection(getConnction())

Ora_Connection.Open()

CatcheAsException

ThrowNewSystem.Exception(e.Message)

EndTry

EndSub

#EndRegion

#Region"得到连接字符串"

'''<summary>

'''得到连接字符串

'''</summary>

'''<returns></returns>

'''<remarks></remarks>

PrivateFunctiongetConnction()AsString

DimstrConnectionAsString=""

'设置要访问的数据库登录名、密码

CallGetCOMMAND()

strConnection="DataSource="DatabaseName";"_

&"userid=" UserName ";password="Password";"_

&"persistsecurityinfo=false"

ReturnstrConnection

EndFunction

#EndRegion

#Region"关闭数据库连接"

'''<summary>

'''关闭数据库连接

'''</summary>

'''<remarks></remarks>

PublicSubDBClose()

Try

IfOra_ConnectionIsNothingThen

Return

EndIf

IfOra_Connection.State=ConnectionState.ClosedThen

Return

EndIf

Ora_Command=NewOracleCommand

Ora_Connection.Close()

CatcheAsException

ThrowNewSystem.Exception(e.Message)

EndTry

EndSub

#EndRegion

#Region"释放资源"

'''<summary>

'''释放资源

'''</summary>

'''<remarks></remarks>

PublicSubDBdispose()

Try

Ora_Connection.Dispose()

Ora_Command.Dispose()

'Ora_CommandType=Nothing

CatcheAsException

ThrowNewSystem.Exception(e.Message)

EndTry

EndSub

#EndRegion

#Region"执行查询"

'''<summary>

'''执行查询

'''</summary>

'''<paramname="strsql">要执行的strsql语句</param>

'''<paramname="outDataTable">返回的DataTable</param>

'''<returns>纪录条数</returns>

'''<remarks></remarks>

PublicFunctionExcuteQuery(ByValstrsqlAsString,ByRefoutDataTableAsDataTable)AsInteger

DimintCountAsInteger=0

strsql=Trim(strsql)

Ifstrsql.Equals("")Then

Return0

EndIf

Try

CallCheckConnection()

Ora_Command.Connection=Ora_Connection

Ora_Command.CommandText=strsql

'Ora_Command.ExecuteReader()

strsql=GetsqlPara(strsql)

StartTime=DateTime.Now

Ora_DataAdapter=NewOracleDataAdapter(Ora_Command)

Ora_DataAdapter.Fill(outDataTable)

EndTime=DateTime.Now

TimeSpan=(EndTime-StartTime)

intCount=outDataTable.Rows.Count

ReturnintCount

CatcheAsException

ThrowNewSystem.Exception(e.Message)

Finally

'log.log_Add(FCHlog.logType.sql,TimeSpan.Seconds(),GetsqlPara(strsql),intCount)

EndTry

EndFunction

#EndRegion

#Region"检查连接"

'''<summary>

'''检查连接

'''</summary>

'''<remarks></remarks>

PrivateSubCheckConnection()

IfOra_ConnectionIsNothingThen

ThrowNewSystem.Exception("未建立数据库连接,数据库关闭~ ")

EndIf

IfOra_Connection.State=ConnectionState.ClosedThen

ThrowNewSystem.Exception("未建立数据库连接,数据库关闭~ ")

EndIf

EndSub

#EndRegion

#Region"sql文的参数与值的转化"

'''<summary>

'''sql文的参数与值的转化

'''</summary>

'''<paramname="strsql">sql</param>

'''<returns></returns>

'''<remarks></remarks>

PrivateFunctionGetsqlPara(ByValstrsqlAsString)AsString

IfOra_Command.Parameters.Count=0ThenReturnstrsql

DimstrValueAsString=""

DimiPosAsInteger=0

DimiTmpAsInteger=0

DimiSetPosAsInteger=0'最后一个 := 的位置

'是否没有参数的sql语句

iPos=strsql.IndexOf("'")

'"'"出现,视为无参数的sql语句

IfiPos>-1Then

Returnstrsql

EndIf

'":"视为无参数的sql语句

Ifstrsql.IndexOf(":")=-1Then

Returnstrsql

EndIf

'是否存储过程

iTmp=strsql.IndexOf("=>")

IfiTmp>-1Then

iTmp=strsql.IndexOf("(",0)

iPos=strsql.IndexOf(":",iTmp+1)

WhileiPos<>-1AndiTmp<>-1

strsql=strsql.Remove(iTmp+1,iPos-iTmp-1)

iTmp=strsql.IndexOf("=>")

IfiTmp<>-1Then

iPos=strsql.IndexOf(":",iTmp+1)

iTmp=strsql.LastIndexOf(",",iTmp+1)

EndIf

EndWhile

EndIf

'取得sql文的参数

ForiIndexAsInteger=0ToOra_Command.Parameters.Count-1

WithOra_Command.Parameters.Item(iIndex)

SelectCase.OracleType

CaseOracleType.Float

CaseOracleType.Double

CaseOracleType.Int16

CaseOracleType.Int32

CaseOracleType.LongRaw

strValue=.Value

CaseOracleType.DateTime

strValue="to_date('"&.Value&"','yyyy/MM/dd')"

CaseElse

strValue="'"&.Value&"'"

EndSelect

'最后一个 := 的位置

IfiIndex=0Then

iPos=strsql.IndexOf(":=")

WhileiPos>-1

iSetPos=iPos

iPos=strsql.IndexOf(":=",iPos+1)

EndWhile

EndIf

iPos=strsql.IndexOf(":",iSetPos+1)

'已经没有参数

IfiPos=-1Then

ExitFor

EndIf

iTmp=strsql.IndexOf(",iPos+1)

IfiTmp=-1Then

iTmp=strsql.IndexOf(")")

EndIf

IfTrim(.ParameterName).IndexOf("@#$%^&*(")=-1Then

strsql=strsql.Replace(":"&.ParameterName,strValue)

Else

strsql=strsql.Replace(strsql.Substring(iPos,iTmp-iPos),strValue)

EndIf

EndWith

Next

Returnstrsql

EndFunction

#EndRegion

#Region"开始事务"

'''<summary>

'''开始事务

'''</summary>

'''<remarks></remarks>

PublicSubBeginTransaction()

Try

CallCheckConnection()

Ora_Transaction=Ora_Connection.BeginTransaction()

Ora_Command.Transaction=Ora_Transaction

CatcheAsException

ThrowNewSystem.Exception(e.Message)

EndTry

EndSub

#EndRegion

#Region"回滚事务"

'''<summary>

'''回滚事务

'''</summary>

'''<remarks></remarks>

PublicSubRollBack()

Try

IfOra_TransactionIsNothingThenExitSub

Ora_Transaction.Rollback()

CatcheAsException

ThrowNewSystem.Exception(e.Message)

EndTry

EndSub

#EndRegion

#Region"提交事务"

'''<summary>

'''提交事务

'''</summary>

'''<remarks></remarks>

PublicSubCommit()

Try

IfOra_TransactionIsNothingThenExitSub

Ora_Transaction.Commit()

CatcheAsException

ThrowNewSystem.Exception(e.Message)

EndTry

EndSub

#EndRegion

#Region"执行更新,存储过程,参数为:String"

'''<summary>

'''执行更新,存储过程,参数为:String

'''</summary>

'''<paramname="strsql">要执行的strsql语句</param>

'''<returns>影响纪录条数</returns>

'''<remarks></remarks>

PublicFunctionExcuteUpdate(ByValstrsqlAsString)AsInteger

DimintCountAsInteger=0

strsql=Trim(strsql)

Ifstrsql.Equals("")Then

Return-1

EndIf

Try

CallCheckConnection()

Ora_Command.Connection=Ora_Connection

Ora_Command.CommandText=strsql

StartTime=DateTime.Now

intCount=Ora_Command.ExecuteNonQuery()

EndTime=DateTime.Now

TimeSpan=(EndTime-StartTime)

ReturnintCount

CatcheAsException

ThrowNewSystem.Exception(e.Message)

Finally

'log.log_Add(FCHlog.logType.sql,intCount)

EndTry

EndFunction

#EndRegion

#Region"执行更新,存储过程,参数为:StringDataSet"

'''<summary>

'''执行更新,存储过程,参数为StringDataSet

'''</summary>

'''<paramname="strsql">要执行的strsql语句</param>

'''<paramname="outDataSet">返回的纪录集</param>

'''<remarks></remarks>

PublicSubExcuteUpdate(ByValstrsqlAsString,ByRefoutDataSetAsDataSet)

DimintCountAsInteger=0

strsql=Trim(strsql)

Ifstrsql.Equals("")Then

Return

EndIf

Try

CallCheckConnection()

Ora_Command.Connection=Ora_Connection

Ora_Command.CommandText=strsql

StartTime=DateTime.Now

Ora_DataAdapter=NewOracleDataAdapter()

Ora_DataAdapter.SelectCommand=Ora_Command

Ora_DataAdapter.Fill(outDataSet)

EndTime=DateTime.Now

TimeSpan=(EndTime-StartTime)

CatcheAsException

ThrowNewSystem.Exception(e.Message)

Finally

'log.log_Add(FCHlog.logType.sql,intCount)

EndTry

EndSub

#EndRegion

#Region"执行更新,存储过程,参数为:StringDataTable"

'''<summary>

'''执行更新,存储过程,参数为:StringDataTable

'''</summary>

'''<paramname="strsql">要执行的strsql语句</param>

'''<paramname="outDataTable">返回的纪录集</param>

'''<remarks></remarks>

PublicSubExcuteUpdate(ByValstrsqlAsString,ByRefoutDataTableAsDataTable)

DimintCountAsInteger=0

strsql=Trim(strsql)

Ifstrsql.Equals("")Then

Return

EndIf

Try

CallCheckConnection()

Ora_Command.Connection=Ora_Connection

Ora_Command.CommandText=strsql

StartTime=DateTime.Now

Ora_DataAdapter=NewOracleDataAdapter()

Ora_DataAdapter.SelectCommand=Ora_Command

Ora_DataAdapter.Fill(outDataTable)

EndTime=DateTime.Now

TimeSpan=(EndTime-StartTime)

CatcheAsException

ThrowNewSystem.Exception(e.Message)

Finally

'log.log_Add(FCHlog.logType.sql,intCount)

EndTry

EndSub

#EndRegion

EndModule

原文链接:https://www.f2er.com/vb/259888.html

猜你在找的VB相关文章