參考一下下面這段代碼就可以了。
成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站設(shè)計、網(wǎng)站制作、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)公安,10多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
Imports System.Data
'引入數(shù)據(jù)庫操作類命名空間
Imports System.Data.OleDb
'引入ADO.NET操作命名空間
Public Class FrmModifystInfo
Inherits System.Windows.Forms.Form
Public ADOcmd As OleDbDataAdapter
Public ds As DataSet = New DataSet()
'建立DataSet對象
Public mytable As Data.DataTable
'建立表單對象
Public myrow As Data.DataRow
'建立數(shù)據(jù)行對象
Public rownumber As Integer
'定義一個整型變量來存放當(dāng)前行數(shù)
Public SearchSQL As String
Public cmd As OleDbCommandBuilder
'======================================================
#Region " Windows 窗體設(shè)計器生成的代碼 "
#End Region
'======================================================
Private Sub FrmModifystInfo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'窗體的載入
TxtSID.Enabled = False
TxtName.Enabled = False
ComboSex.Enabled = False
TxtBornDate.Enabled = False
TxtClassno.Enabled = False
TxtRuDate.Enabled = False
TxtTel.Enabled = False
TxtAddress.Enabled = False
TxtComment.Enabled = False '設(shè)置信息為只讀
Dim tablename As String = "student_Info "
SearchSQL = "select * from student_Info "
ExecuteSQL(SearchSQL, tablename) '打開數(shù)據(jù)庫
ShowData() '顯示記錄
End Sub
Private Sub ShowData()
'在窗口中的textbox中顯示數(shù)據(jù)
myrow = mytable.Rows.Item(rownumber)
TxtSID.Text = myrow.Item(0).ToString
TxtName.Text = myrow.Item(1).ToString
ComboSex.Text = myrow.Item(2).ToString
TxtBornDate.Text = Format(myrow.Item(3), "yyyy-MM-dd ")
TxtClassno.Text = myrow.Item(4).ToString
TxtTel.Text = myrow.Item(5).ToString
TxtRuDate.Text = Format(CDate(myrow.Item(6)), "yyyy-MM-dd ")
TxtAddress.Text = myrow.Item(7).ToString
TxtComment.Text = myrow.Item(8).ToString
End Sub
Private Sub BtFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtFirst.Click
'指向第一條數(shù)據(jù)
rownumber = 0
ShowData()
End Sub
Private Sub BtPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtPrev.Click
'指向上一條數(shù)據(jù)
BtNext.Enabled = True
rownumber = rownumber - 1
If rownumber 0 Then
rownumber = 0 '如果到達(dá)記錄的首部,行號設(shè)為零
BtPrev.Enabled = False
End If
ShowData()
End Sub
Private Sub BtNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtNext.Click
'指向上一條數(shù)據(jù)
BtPrev.Enabled = True
rownumber = rownumber + 1
If rownumber mytable.Rows.Count - 1 Then
rownumber = mytable.Rows.Count - 1 '判斷是否到達(dá)最后一條數(shù)據(jù)
BtNext.Enabled = False
End If
ShowData()
End Sub
Private Sub BtLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtLast.Click
'指向最后一條數(shù)據(jù)
rownumber = mytable.Rows.Count - 1
ShowData()
End Sub
Private Sub BtDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtDelete.Click
mytable.Rows.Item(rownumber).Delete() '刪除記錄
If MsgBox( "確定要刪除改記錄嗎? ", MsgBoxStyle.OKCancel + vbExclamation, "警告 ") = MsgBoxResult.OK Then
cmd = New OleDbCommandBuilder(ADOcmd)
'使用自動生成的SQL語句
ADOcmd.Update(ds, "student_Info ")
BtNext.PerformClick()
End If
End Sub
Private Sub BtModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtModify.Click
TxtSID.Enabled = False '關(guān)鍵字段只讀
TxtName.Enabled = True '可讀寫
ComboSex.Enabled = True
TxtBornDate.Enabled = True
TxtClassno.Enabled = True
TxtRuDate.Enabled = True
TxtTel.Enabled = True
TxtAddress.Enabled = True
TxtComment.Enabled = True
End Sub
Private Sub BtUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtUpdate.Click
If Not Testtxt(TxtName.Text) Then
MsgBox( "請輸入姓名! ", vbOKOnly + vbExclamation, "警告 ")
TxtName.Focus()
Exit Sub
End If
If Not Testtxt(ComboSex.Text) Then
MsgBox( "請選擇性別! ", vbOKOnly + vbExclamation, "警告 ")
ComboSex.Focus()
Exit Sub
End If
If Not Testtxt(TxtClassno.Text) Then
MsgBox( "請選擇班號! ", vbOKOnly + vbExclamation, "警告 ")
TxtClassno.Focus()
Exit Sub
End If
If Not Testtxt(TxtTel.Text) Then
MsgBox( "請輸入聯(lián)系電話! ", vbOKOnly + vbExclamation, "警告 ")
TxtTel.Focus()
Exit Sub
End If
If Not Testtxt(TxtAddress.Text) Then
MsgBox( "請輸入家庭住址! ", vbOKOnly + vbExclamation, "警告 ")
TxtAddress.Focus()
Exit Sub
End If
If Not IsNumeric(Trim(TxtSID.Text)) Then
MsgBox( "請輸入數(shù)字學(xué)號! ", vbOKOnly + vbExclamation, "警告 ")
Exit Sub
TxtSID.Focus()
End If
If Not IsDate(TxtBornDate.Text) Then
MsgBox( "出生時間應(yīng)輸入日期格式(yyyy-mm-dd)! ", vbOKOnly + vbExclamation, "警告 ")
Exit Sub
TxtBornDate.Focus()
End If
If Not IsDate(TxtRuDate.Text) Then
MsgBox( "入校時間應(yīng)輸入日期格式(yyyy-mm-dd)! ", vbOKOnly + vbExclamation, "警告 ")
TxtRuDate.Focus()
Exit Sub
End If
myrow.Item(0) = Trim(TxtSID.Text)
myrow.Item(1) = Trim(TxtName.Text)
myrow.Item(2) = Trim(ComboSex.Text)
myrow.Item(3) = Trim(TxtBornDate.Text)
myrow.Item(4) = Trim(TxtClassno.Text)
myrow.Item(5) = Trim(TxtTel.Text)
myrow.Item(6) = Trim(TxtRuDate.Text)
myrow.Item(7) = Trim(TxtAddress.Text)
myrow.Item(8) = Trim(TxtComment.Text)
mytable.GetChanges()
cmd = New OleDbCommandBuilder(ADOcmd)
'使用自動生成的SQL語句
ADOcmd.Update(ds, "student_Info ")
'對數(shù)據(jù)庫進(jìn)行更新
MsgBox( "修改學(xué)籍信息成功! ", vbOKOnly + vbExclamation, "警告 ")
TxtName.Enabled = False
ComboSex.Enabled = False
TxtBornDate.Enabled = False
TxtClassno.Enabled = False
TxtRuDate.Enabled = False
TxtTel.Enabled = False
TxtAddress.Enabled = False
TxtComment.Enabled = False '重新設(shè)置信息為只讀
End Sub
Private Sub BtCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtCancel.Click
TxtSID.Enabled = False
TxtName.Enabled = False
ComboSex.Enabled = False
TxtBornDate.Enabled = False
TxtClassno.Enabled = False
TxtRuDate.Enabled = False
TxtTel.Enabled = False
TxtAddress.Enabled = False
TxtComment.Enabled = False
End Sub
Public Function ExecuteSQL(ByVal SQL As String, ByVal table As String)
Try
'建立ADODataSetCommand對象
'數(shù)據(jù)庫查詢函數(shù)
ADOcmd = New OleDbDataAdapter(SQL, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\student.mdb ")
'建立ADODataSetCommand對象
ADOcmd.Fill(ds, table) '取得表單
mytable = ds.Tables.Item(0) '取得名為table的表
rownumber = 0 '設(shè)置為第一行
myrow = mytable.Rows.Item(rownumber)
'取得第一行數(shù)據(jù)
Catch
MsgBox(Err.Description)
End Try
End Function
End Class
oledb的使用網(wǎng)上的確很多.\x0d\x0a如果只是查詢. 寫入. 刪除,我們常用的就是\x0d\x0aOleDbConnection '用于寫數(shù)據(jù)庫的連接\x0d\x0aOleDbCommand '用于數(shù)據(jù)的操作 接收SQL語句 并執(zhí)行\(zhòng)x0d\x0aOleDbDataReader '查詢了,我們就用這個類來讀取查詢的對象\x0d\x0a1).OleDbConnection \x0d\x0a dim conn as new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;Data Source =數(shù)據(jù)庫的路徑")'連接字符串一般就這樣 \x0d\x0a 查詢前在打開 \x0d\x0a conn.open\x0d\x0a 2).OleDbCommand \x0d\x0a dim cmd as new OleDbCommand ("SQL語句",conn)'SQL語句決定了你是查詢寫入還是更新刪除\x0d\x0a '''\x0d\x0a如果是查詢,你還要用到OleDbDataReader \x0d\x0a 這個是不用新建的不能用New \x0d\x0adim rd as OleDbDataReader=cmd.ExecuteReader'cmd.ExecuteReader 方法就是讀取查詢的內(nèi)容\x0d\x0acmd.ExecuteNonQuery用于執(zhí)行操作 返回變動的條數(shù) ,一般用于 添加 刪除 更新 等其它操作\x0d\x0a \x0d\x0a其實(shí)就這些了,就是要學(xué)點(diǎn)SQL語句就行了.\x0d\x0a標(biāo)準(zhǔn)執(zhí)行過程就是\x0d\x0a Dim conn As New OleDbConnection("連接字符串")\x0d\x0a conn.Open()\x0d\x0a Dim cmd As New OleDbCommand("SQL語句", conn)\x0d\x0a '如果是寫入 更新 刪除 則\x0d\x0a cmd.ExecuteNonQuery()\x0d\x0a 否則\x0d\x0a Dim rd As OleDbDataReader = cmd.ExecuteReader\x0d\x0a \x0d\x0a If rd.Read Then\x0d\x0a Dim d As Object = rd.Item("字段名")\x0d\x0a End If\x0d\x0a 結(jié)束如果\x0d\x0a rd.Close()\x0d\x0a cmd.Dispose()\x0d\x0a conn.Close()\x0d\x0a \x0d\x0a上面要有 Imports System.Data.OleDb
1、首先打開Visual Studio 2008代碼窗口,添加引用。
2、輸入以下代碼:Public conn1 ?As SqlConnection = New SqlConnection 。
3、聲明關(guān)鍵字 Public;(因?yàn)槭侨肿兞浚杂肞ublic 來聲明)。
4、如果SQL 數(shù)據(jù)庫就在本機(jī),則用以下代碼連接。
5、如果代碼太長,影響可讀性,可以用空格加"_"后,回車換行即可。
網(wǎng)站題目:vb點(diǎn)虐
數(shù)據(jù)庫操作 vb操作數(shù)據(jù)庫實(shí)例
文章鏈接:http://m.newbst.com/article18/ddshcgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、網(wǎng)站設(shè)計公司、網(wǎng)站維護(hù)、網(wǎng)站建設(shè)、網(wǎng)站改版、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)