見下面的代碼,自己換成真實(shí)的數(shù)據(jù)庫(kù)及表即可
創(chuàng)新互聯(lián)建站一直在為企業(yè)提供服務(wù),多年的磨煉,使我們?cè)趧?chuàng)意設(shè)計(jì),成都營(yíng)銷網(wǎng)站建設(shè)到技術(shù)研發(fā)擁有了開發(fā)經(jīng)驗(yàn)。我們擅長(zhǎng)傾聽企業(yè)需求,挖掘用戶對(duì)產(chǎn)品需求服務(wù)價(jià)值,為企業(yè)制作有用的創(chuàng)意設(shè)計(jì)體驗(yàn)。核心團(tuán)隊(duì)擁有超過10余年以上行業(yè)經(jīng)驗(yàn),涵蓋創(chuàng)意,策化,開發(fā)等專業(yè)領(lǐng)域,公司涉及領(lǐng)域有基礎(chǔ)互聯(lián)網(wǎng)服務(wù)中國(guó)電信云錦天府、成都app軟件開發(fā)公司、手機(jī)移動(dòng)建站、網(wǎng)頁設(shè)計(jì)、網(wǎng)絡(luò)整合營(yíng)銷。
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ù)庫(kù)1.mdb")?'定義連接1---這里請(qǐng)更改為實(shí)際數(shù)據(jù)庫(kù)路徑及名稱?
Public?cn2?As?New?OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data?Source=C:\ACCESS數(shù)據(jù)庫(kù)2.mdb")?'定義連接2---這里請(qǐng)更改為實(shí)際數(shù)據(jù)庫(kù)路徑及名稱?
Public?DataBaseRST1?As?Integer?'用來返回?cái)?shù)據(jù)庫(kù)執(zhí)行結(jié)果?
Public?DataBaseRST2?As?Integer?'用來返回?cái)?shù)據(jù)庫(kù)執(zhí)行結(jié)果
Public?Function?DataModify(ByVal?str_cmd1?As?String,?ByVal?str_cmd2?As?String)?As?Boolean?'進(jìn)行數(shù)據(jù)庫(kù)修改操作函數(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í)行的結(jié)果?
DataBaseRST2?=?cmdinsert2.ExecuteNonQuery()?'用來返回執(zhí)行的結(jié)果?
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
1、VB.NET是一個(gè)高級(jí)程序設(shè)計(jì)語言
2、ACCESS是一個(gè)辦公型的數(shù)據(jù)庫(kù)
3、只要設(shè)置正確、代碼完整是完全可以操作的
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個(gè)查詢值
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