見下面的代碼,自己換成真實的數(shù)據(jù)庫及表即可
創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比遂溪網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式遂溪網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋遂溪地區(qū)。費用合理售后完善,十年實體公司更值得信賴。
Imports?System.Data
Imports?System.IO
Imports?System.Data.OleDb
Module?Module1
Public?cn1?As?New?OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data?Source=C:\ACCESS數(shù)據(jù)庫1.mdb")?'定義連接1---這里請更改為實際數(shù)據(jù)庫路徑及名稱?
Public?cn2?As?New?OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data?Source=C:\ACCESS數(shù)據(jù)庫2.mdb")?'定義連接2---這里請更改為實際數(shù)據(jù)庫路徑及名稱?
Public?DataBaseRST1?As?Integer?'用來返回數(shù)據(jù)庫執(zhí)行結果?
Public?DataBaseRST2?As?Integer?'用來返回數(shù)據(jù)庫執(zhí)行結果
Public?Function?DataModify(ByVal?str_cmd1?As?String,?ByVal?str_cmd2?As?String)?As?Boolean?'進行數(shù)據(jù)庫修改操作函數(shù)?
Dim?cmdinsert1?As?New?OleDbCommand
Dim?cmdinsert2?As?New?OleDbCommand
Try
cmdinsert1.CommandText?=?str_cmd1
cmdinsert2.CommandText?=?str_cmd2
cmdinsert1.Connection?=?cn1
cmdinsert2.Connection?=?cn2
If?cn1.State?=?ConnectionState.Closed?Then?cn1.Open()
If?cn2.State?=?ConnectionState.Closed?Then?cn2.Open()
DataBaseRST1?=?cmdinsert1.ExecuteNonQuery()?'用來返回執(zhí)行的結果?
DataBaseRST2?=?cmdinsert2.ExecuteNonQuery()?'用來返回執(zhí)行的結果?
cn1.Close()
cn2.Close()
Return?True
Catch?ex?As?Exception
MessageBox.Show(Err.Description,?"Error",?MessageBoxButtons.OK,?MessageBoxIcon.Error)
Return?False
End?Try
End?Function
End?Module?
Public?Class?Form1?
Private?Sub?Button1_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click
DataModify("insert?into?aa?values?('1','2')",?"insert?into?aa?values?('1','2')")?'調(diào)用方法
End?Sub
End?Class
Imports System.Data.OleDb
'Imports System.Data.OracleClient
Imports System.Data.SqlClient
'鏈接Oracle
'cn = New SqlConnection(""Data Source=TNS名稱;Persist Security Info=True;User ID=用戶名;Password=密碼;" ")
'Imports System.Data.OracleClient
'Dim oOracleConn As OracleConnection = New OracleConnection()
'oOracleConn.ConnectionString = ""Data Source=MyOracleDB; User Id=username; Password=passwd; Integrated Security=no; "
'oOracleConn.Open()
'鏈接SQL Server
'cn = New SqlConnection("Data Source=.;Initial Catalog=Sales;Integrated Security=False;User ID=sa;Password=;")
'鏈接Access
'cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" db_path ";Persist Security Info=False")
Public Class Fun_DataBase
Dim cn As OleDbConnection
Dim cm As OleDbCommand
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim db_path As String = "D:\Documents\Visual Studio 2013\Projects\VBnet1\VBnet1\Resources\Database1.mdb"
Function Fun_sql1(Str_sql As String, V_調(diào)用來源 As String) '按指定sql,反饋1個查詢值
Try
'Dim cn As New OleDbConnection("Data Source=.;Initial Catalog=Sales;Integrated Security=False;User ID=sa;Password=123456;")
'鏈接Access
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" db_path ";Persist Security Info=False")
cn.Open() 'sql語句執(zhí)行前,必須打開連接
cm = New OleDbCommand(Str_sql, cn)
cm.ExecuteNonQuery()
Dim strValue As String = cm.ExecuteScalar.ToString
'MessageBox.Show(strValue)
Fun_sql1 = strValue
cn.Close()
Fun_sql1 = True
Catch ex As Exception
MessageBox.Show(V_調(diào)用來源 ":執(zhí)行sql語句失敗!" Str_sql)
Fun_sql1 = False
End Try
End Function
Function Fun_sqln(Str_sql As String, V_調(diào)用來源 As String) '按指定sql,反饋1行n列查詢值
Try
'鏈接Access
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" db_path ";Persist Security Info=False")
cn.Open() 'sql語句執(zhí)行前,必須打開連接
Dim cmd As New OleDbCommand("select * from grade", cn)
'Dim dr As SqlDataReader
Dim dr As OleDbDataReader
dr = cmd.ExecuteReader()
Dim strDisplay As String = ""
While dr.Read()
Fun_sqln = dr(0).ToString "|"
End While
cn.Close()
Fun_sqln = True
Catch ex As Exception
MessageBox.Show(V_調(diào)用來源 ":執(zhí)行sql語句失?。? Str_sql)
Fun_sqln = False
End Try
'Dim s = Split(F.Fun_sqln("ef"), "|")
'Dim i As Integer
'i = UBound(s)
'For i = 0 To UBound(s)
' MessageBox.Show(s(i))
'Next
End Function
Public Function Fun_sql_exec(Str_sql As String, V_調(diào)用來源 As String) '執(zhí)行指定sql,反饋T|F
Try
'鏈接Access
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" db_path ";Persist Security Info=False")
cn.Open() 'sql語句執(zhí)行前,必須打開連接
cm = New OleDbCommand(Str_sql, cn)
cm.ExecuteNonQuery()
cn.Close()
Fun_sql_exec = True
Catch ex As Exception
MessageBox.Show(V_調(diào)用來源 ":執(zhí)行sql語句失??!" Str_sql)
Fun_sql_exec = False
End Try
End Function
首先你本地要安裝MySQL Connector Net
工程引用mysql.data
連接數(shù)據(jù)庫:
?Imports MySql.Data
Imports MySql.Data.MySqlClient
‘連接字符串dbServerName 是ip,下面的英文應該懂得吧。
dim sA as string
sA = "server=" Me.dbServerName "; port=3306; database=" Me.dbDataBaseName _
? ? ? ? ? ? ? ? ? "; user id=" Me.dbUserName "; password=" Me.dbPassWord "; "
dim mvardbConnectionMysql As MySqlConnection
? ? ? ? ? With mvardbConnectionMysql
? ? ? ? ? ? ? ? .ConnectionString = sA
? ? ? ? ? ? ? .Open()
? ? ? ? ? End With
4. 讀取數(shù)據(jù)
dim xx As MySqlDataReader
Dim oC As MySqlCommand = New MySqlCommand(sSql, Me.mvardbConnectionMysql)
oC.CommandType = CommandType.Text
xx= oC.ExecuteReader()
if xx.HasRows
do while xx.read() ? ’一次讀取一行
msgbox xx.item(2) ? '取出當前行的第三個字段的值顯示。
loop
end if
5. 寫入數(shù)據(jù)
? Dim oT As MySqlTransaction = Me.mvardbConnectionMysql.BeginTransaction()
? Try
? ? ? Dim oC As MySqlCommand
? ?'下面的commandtext是執(zhí)行的更新語句,例如Insert into 語句。
? ? ? oC = New MySqlCommand(CommandText, Me.mvardbConnectionMysql, oT)
? ? ? oC.CommandType = CommandType.Text
? ? ? oC.ExecuteNonQuery()
? ? ? oT.Commit()
? ? ? oC = Nothing
? ? ? oT = Nothing
? Catch oe As Exception
? ? ? oT.Rollback()
? End Try
一般都是vb.net傳入?yún)?shù) 交給存儲過程去做的 你是要在vb.net前臺就執(zhí)行的例子 還是想要調(diào)用存儲過程的例子
前臺的一般就是這樣
Dim paras As New NameValueCollection
paras .Append("delete from T_AlarmConfig ")
paras .Append(" where AlarmConfigID=@AlarmConfigID")
調(diào)用存儲過程的就是給寫存儲過程了,這是調(diào)用存儲過程的例子,只能理解理解
''' summary
''' 刪除數(shù)據(jù)
''' /summary
''' param name="procName"存儲過程名/param
''' param name="paras"存儲過程參數(shù)/param
''' remarks
''' 2006-5-29 zhuzh Created
''' /remarks
Public Sub DeleteData(ByVal procName As String, _
ByVal paras As NameValueCollection, _
Optional ByVal timeout As Integer = 30)
eventCount += 1
'存儲過程參數(shù)名數(shù)組
Dim paraNames As String() = {}
'存儲過程參數(shù)值數(shù)組
Dim paraValues As String() = {}
'傳入的參數(shù)轉化成數(shù)組
Me.ConvertNameValueCollection(paras, paraNames, paraValues, STATUS_UPDATE)
'判斷系統(tǒng)類型調(diào)用相應模塊的方法
If isWebSys Then
DeleteDelegateInstance = AddressOf webGw.ExecProc
Else
DeleteDelegateInstance = AddressOf AdoGw.ExecProc
End If
Dim ret As Integer = 0
Dim ds As New DataSet
'----guojun 2008-1-2 add-----
If timeout 60000 Then
timeout = 60000
End If
'--------防止超時異常--------
DeleteDelegateInstance.BeginInvoke(procName, ret, ds, paraNames, paraValues, timeout, _
AddressOf DataDeletedCallBack, Nothing)
End Sub