vbnet数据库操作

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

vbnet数据库操作

参考一下下面这段代码就可以了。

Imports System.Data

'引入数据库操作类命名空间

Imports System.Data.OleDb

'引入操作命名空间

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

'建立数据行对象

Public rownumber As Integer

'定义一个整型变量来存放当前行数

Public SearchSQL As String

Public cmd As OleDbCommandBuilder

'======================================================

#Region " Windows 窗体设计器生成的代码 "

#End Region

'======================================================

Private Sub FrmModifystInfo_Load(ByVal sender As Ob ject, 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 '设置信息为只读 Dim tablename As String = "student_Info " SearchSQL = "select * from student_Info " ExecuteSQL(SearchSQL, tablename) '打开数据库 ShowData() '显示记录

End Sub

Private Sub ShowData()

'在窗口中的textbox中显示数据

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.Ob ject, ByVal e As System.EventArgs) Handles BtFirst.Click '指向第一条数据

rownumber = 0

ShowData()

End Sub

Private Sub BtPrev_Click(ByVal sender As System.Obj ect, ByVal e As System.EventArgs) Handles BtPrev.Click '指向上一条数据

BtNext.Enabled = True

rownumber = rownumber - 1

If rownumber < 0 Then

rownumber = 0 '如果到达记录的首部,行号设为零

BtPrev.Enabled = False

End If

ShowData()

End Sub

Private Sub BtNext_Click(ByVal sender As System.Obj ect, ByVal e As System.EventArgs) Handles BtNext.Click '指向上一条数据

BtPrev.Enabled = True

rownumber = rownumber + 1

If rownumber > mytable.Rows.Count - 1 The n

rownumber = mytable.Rows.Count - 1 '判断是否到达最后一条数据

BtNext.Enabled = False

End If

ShowData()

End Sub

Private Sub BtLast_Click(ByVal sender As System.Obj ect, ByVal e As System.EventArgs) Handles BtLast.Click '指向最后一条数据

rownumber = mytable.Rows.Count - 1

ShowData()

End Sub

Private Sub BtDelete_Click(ByVal sender As System.O bject, ByVal e As System.EventArgs) Handles BtDelete.Clic k

mytable.Rows.Item(rownumber).Delete() '删除记录 If MsgBox( "确定要删除改记录吗? ", MsgBoxStyle.O KCancel + 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.O bject, ByVal e As System.EventArgs) Handles BtModify.Clic k

TxtSID.Enabled = False '关键字段只读

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.O bject, ByVal e As System.EventArgs) Handles BtUpdate.Clic k

相关文档
最新文档