1、VB.NET是一个高级程序设计语言
清涧网站制作公司哪家好,找成都创新互联公司!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。成都创新互联公司成立与2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联公司。
2、ACCESS是一个办公型的数据库
3、只要设置正确、代码完整是完全可以操作的
见下面的代码,自己换成真实的数据库及表即可
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数据库1.mdb") '定义连接1---这里请更改为实际数据库路径及名称
Public cn2 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\ACCESS数据库2.mdb") '定义连接2---这里请更改为实际数据库路径及名称
Public DataBaseRST1 As Integer '用来返回数据库执行结果
Public DataBaseRST2 As Integer '用来返回数据库执行结果
Public Function DataModify(ByVal str_cmd1 As String, ByVal str_cmd2 As String) As Boolean '进行数据库修改操作函数
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() '用来返回执行的结果
DataBaseRST2 = cmdinsert2.ExecuteNonQuery() '用来返回执行的结果
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')") '调用方法
End Sub
End Class
首先你本地要安装MySQL Connector Net
工程引用mysql.data
连接数据库:
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. 读取数据
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. 写入数据
Dim oT As MySqlTransaction = Me.mvardbConnectionMysql.BeginTransaction()
Try
Dim oC As MySqlCommand
'下面的commandtext是执行的更新语句,例如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传入参数 交给存储过程去做的 你是要在vb.net前台就执行的例子 还是想要调用存储过程的例子
前台的一般就是这样
Dim paras As New NameValueCollection
paras .Append("delete from T_AlarmConfig ")
paras .Append(" where AlarmConfigID=@AlarmConfigID")
调用存储过程的就是给写存储过程了,这是调用存储过程的例子,只能理解理解
''' summary
''' 删除数据
''' /summary
''' param name="procName"存储过程名/param
''' param name="paras"存储过程参数/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
'存储过程参数名数组
Dim paraNames As String() = {}
'存储过程参数值数组
Dim paraValues As String() = {}
'传入的参数转化成数组
Me.ConvertNameValueCollection(paras, paraNames, paraValues, STATUS_UPDATE)
'判断系统类型调用相应模块的方法
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
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_调用来源 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语句执行前,必须打开连接
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_调用来源 ":执行sql语句失败!" Str_sql)
Fun_sql1 = False
End Try
End Function
Function Fun_sqln(Str_sql As String, V_调用来源 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语句执行前,必须打开连接
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_调用来源 ":执行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_调用来源 As String) '执行指定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语句执行前,必须打开连接
cm = New OleDbCommand(Str_sql, cn)
cm.ExecuteNonQuery()
cn.Close()
Fun_sql_exec = True
Catch ex As Exception
MessageBox.Show(V_调用来源 ":执行sql语句失败!" Str_sql)
Fun_sql_exec = False
End Try
End Function