VS+access數(shù)據(jù)庫的,MSSQL數(shù)據(jù)庫的話你改下,反正就2種鏈接方式,書上都有。
成都創(chuàng)新互聯(lián),為您提供網(wǎng)站建設、網(wǎng)站制作、網(wǎng)站營銷推廣、網(wǎng)站開發(fā)設計,對服務成都建筑動畫等多個行業(yè)擁有豐富的網(wǎng)站建設及推廣經(jīng)驗。成都創(chuàng)新互聯(lián)網(wǎng)站建設公司成立于2013年,提供專業(yè)網(wǎng)站制作報價服務,我們深知市場的競爭激烈,認真對待每位客戶,為客戶提供賞心悅目的作品。 與客戶共同發(fā)展進步,是我們永遠的責任!
下面是代碼和解釋,另外我會把文件發(fā)到你郵箱里的,你把文件復制到自己的站點文件夾里面運行就可以看到效果了。
Imports System.Data.OleDb
Imports System.Data
Partial Class baidu
Inherits System.Web.UI.Page
'下面是添加記錄
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'定義“連接數(shù)據(jù)庫”和“執(zhí)行SQL”變量
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim dbname As String
dbname = Server.MapPath("baidu.mdb")
myConnection = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" dbname)
myConnection.Open()
'INSERT INTO "表格名" ("欄位1", "欄位2", ...) VALUES ("值1", "值2", ...)
'這里可以加上 昵稱 是否已經(jīng)存在的判斷語句,就是 SELECT "欄位名" FROM "表格名" WHERE "條件"
myCommand = New OleDbCommand("Insert INTO a1(昵稱) Values('" TextBox1.Text "')", myConnection)
myCommand.ExecuteNonQuery()
Response.AddHeader("Refresh", "0")
'關閉連接()
myConnection.Close()
'釋放資源
myCommand.Dispose()
End Sub
'下面是刪除記錄
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
'定義“連接數(shù)據(jù)庫”和“執(zhí)行SQL”變量
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim dbname As String
dbname = Server.MapPath("baidu.mdb")
myConnection = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" dbname)
myConnection.Open()
'DELETE FROM "表格名" WHERE {條件}
myCommand = New OleDbCommand("DELETE FROM a1 WHERE 昵稱 = '" TextBox1.Text "'", myConnection)
myCommand.ExecuteNonQuery()
Response.AddHeader("Refresh", "0")
'關閉連接()
myConnection.Close()
'釋放資源
myCommand.Dispose()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'定義“連接數(shù)據(jù)庫”和“執(zhí)行SQL”變量
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim dbname As String
dbname = Server.MapPath("baidu.mdb")
myConnection = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" dbname)
myConnection.Open()
myCommand = New OleDbCommand("select * FROM a1", myConnection)
GridView1.DataSource = myCommand.ExecuteReader()
GridView1.DataBind()
'關閉連接()
myConnection.Close()
'釋放資源
myCommand.Dispose()
End Sub
End Class
郵件已經(jīng)發(fā)生,請查收。記得給分哈
首先你本地要安裝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
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語句執(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_調用來源 ":執(zhí)行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語句執(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_調用來源 ":執(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_調用來源 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_調用來源 ":執(zhí)行sql語句失?。? Str_sql)
Fun_sql_exec = False
End Try
End Function
1、VB.NET是一個高級程序設計語言
2、ACCESS是一個辦公型的數(shù)據(jù)庫
3、只要設置正確、代碼完整是完全可以操作的
一般都是vb.net傳入?yún)?shù) 交給存儲過程去做的 你是要在vb.net前臺就執(zhí)行的例子 還是想要調用存儲過程的例子
前臺的一般就是這樣
Dim paras As New NameValueCollection
paras .Append("delete from T_AlarmConfig ")
paras .Append(" where AlarmConfigID=@AlarmConfigID")
調用存儲過程的就是給寫存儲過程了,這是調用存儲過程的例子,只能理解理解
''' 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)類型調用相應模塊的方法
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