.net 连接数据库的方式

qq_35994562 2017-06-02 09:32:34
1、查询方式
Public Function DB_QUERY(ByVal Statement As String, ByVal db As String) As DataTable
Dim ds As New DataSet
Try

If db = "ORACLE" Then
Dim da As New OracleDataAdapter(Statement, objConnection)
da.Fill(ds) ' 將資料填至資料集
End If
If db = "SQL" Then
Dim da As New SqlDataAdapter(Statement, sqlConnection)
da.Fill(ds) ' 將資料填至資料集
End If

Catch ex As Exception
MsgBox(ex.Message)
End Try

Return ds.Tables(0)

End Function
2、执行操作方式:
Public Function DB_EXECUTE(ByVal Statement As String) As Integer
Dim intRowAffected As Integer = 0

Dim sqlCmd As SqlCommand = New SqlCommand
Dim sqltran As SqlTransaction
'Dim objCmd As OracleCommand = New OracleCommand
'Dim objtran As OracleTransaction

sqlConnection.Open()
sqltran = sqlConnection.BeginTransaction()
sqlCmd = New SqlCommand(Statement, sqlConnection)
sqlCmd.Transaction = sqltran
Try
intRowAffected = sqlCmd.ExecuteNonQuery()
sqltran.Commit()
Catch ex As Exception
sqltran.Rollback()
objConnection.Close()
End Try

sqlCmd.Dispose()
sqlConnection.Close()
Return intRowAffected
End Function
3、connection方式:
(oracle)
Public objConnection As OracleConnection = New OracleConnection("Data Source=(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ***)(PORT = **21)) ) (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=PROD))) ;Persist Security Info=True;User ID=a**s;Password=""a**s"";")
(sql)
Public sqlConnection As SqlConnection = New SqlConnection("Data Source=10.240.*****;Persist Security Info=True;User ID=sa;Password=""S****"";")

Private objOracleConnection As OracleConnection = New OracleConnection("Data Source=(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.240.****)(PORT = 15**)) ) (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=*rpt))) ;Persist Security Info=True;User ID=ME*****;Password=""S****ic"";")
...全文
228 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
c999000 2017-06-03
  • 打赏
  • 举报
回复
赞赞赞
qq_35994562 2017-06-02
  • 打赏
  • 举报
回复
不错,谢谢楼主支持!

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧