网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

vb.net数据集操作,vb数据集控件

vb.net数据库操作

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

我们提供的服务有:网站设计、网站制作、微信公众号开发、网站优化、网站认证、八步ssl等。为数千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的八步网站制作公司

Imports System.Data

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

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

'建立数据行对象

Public rownumber As Integer

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

Public SearchSQL As String

Public cmd As OleDbCommandBuilder

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

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

#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 '设置信息为只读

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.Object, ByVal e As System.EventArgs) Handles BtFirst.Click

'指向第一条数据

rownumber = 0

ShowData()

End Sub

Private Sub BtPrev_Click(ByVal sender As System.Object, 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.Object, ByVal e As System.EventArgs) Handles BtNext.Click

'指向上一条数据

BtPrev.Enabled = True

rownumber = rownumber + 1

If rownumber mytable.Rows.Count - 1 Then

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

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

'指向最后一条数据

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 '关键字段只读

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( "请输入联系电话! ", 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( "请输入数字学号! ", vbOKOnly + vbExclamation, "警告 ")

Exit Sub

TxtSID.Focus()

End If

If Not IsDate(TxtBornDate.Text) Then

MsgBox( "出生时间应输入日期格式(yyyy-mm-dd)! ", vbOKOnly + vbExclamation, "警告 ")

Exit Sub

TxtBornDate.Focus()

End If

If Not IsDate(TxtRuDate.Text) Then

MsgBox( "入校时间应输入日期格式(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 ")

'对数据库进行更新

MsgBox( "修改学籍信息成功! ", 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 '重新设置信息为只读

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对象

'数据库查询函数

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 '设置为第一行

myrow = mytable.Rows.Item(rownumber)

'取得第一行数据

Catch

MsgBox(Err.Description)

End Try

End Function

End Class

请采纳。

vb.net 怎么操作数据库

如果楼主熟悉VB6,可以直接在项目中添加ADODB的Com引用,这样你就可以像VB6那样操作数据库了!

另外

.NET Framework中连接数据库要用到ADO.NET。如果要操作Access数据库,要用到System.Data.OleDb命名空间下的许多类。

比如按楼主所说,“我想在textbox1中显示表一中【一些数据】字段下的第一个内容”:

'首先导入命名空间

Imports System.Data

Imports System.Data.OleDb

'然后在某一个事件处理程序中写:

Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=数据库.accdb;Jet OLEDB:Database Password=MyDbPassword")

Dim command As New OleDbCommand("Select * From 数据表", conn)

conn.Open() '打开数据库连接

Dim reader As OleDbDataReader = command.ExecuteReader() '执行SQL语句,返回OleDbDataReader 对象

Do While reader.Read() '读取一条数据

textbox1.Text += reader("一些数据") VbCrLf

Loop

reader.Close() '关闭OleDbDataReader

conn.Close() '关闭连接

VB.NET 连接数据库后的操作

ACmd.CommandText = AFind 下面要加上一句执行

ACmd.ExecuteNonQuery()

-------------------------------------

要写查询到控件DataGridView的话,可以这样做:.net2.0或以上的环境中:

Dim AConnect As OleDb.OleDbConnection

Dim ACmd As OleDb.OleDbCommand

Dim dtResult As DataTable=new DataTable()

Dim ConnectStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" Application.StartupPath "\Enroll.mdb"

Dim AFind As String = "SELECT * FROM rj"

AConnect = New OleDb.OleDbConnection(ConnectStr)

AConnect.Open()

ACmd = New OleDb.OleDbCommand(AFind, AConnect)

ACmd.CommandText = AFind

Dim ADr as OleDb.OleDbDataReader=ACmd.ExcuteReader()

dtResult.Load(ADr)

AConnect.Close()

DataGridView.DataSource=dtResult

---------

希望能符合你的需求

用vb.net操作access数据库

mports System.Drawing

Imports System.ComponentModel

Imports System.Windows.Forms

Imports System.Data.OleDb

Imports System.Data

Public Class Form1

Inherits Form

Private components As System.ComponentModel.Container = Nothing

Private WithEvents lastrec As Button

Private WithEvents nextrec As Button

Private WithEvents previousrec As Button

Private WithEvents firstrec As Button

Private t_books As TextBox

Private t_nl As TextBox

Private t_xb As TextBox

Private t_xm As TextBox

Private t_id As TextBox

Private l_books As Label

Private l_nl As Label

Private l_xb As Label

Private l_xm As Label

Private l_id As Label

Private label1 As Label

Private myDataSet As DataSet

Private WithEvents button1 As Button

Private WithEvents button2 As Button

Private WithEvents button3 As Button

Private WithEvents button4 As Button

Private myBind As BindingManagerBase

Public Sub New ( )

MyBase.New ( )

GetConnected ( )

InitializeComponent ( )

End Sub

'清除在程序中使用过的资源

Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose ( )

End If

End If

MyBase.Dispose ( disposing )

End Sub

Public Sub GetConnected ( )

'创建一个数据连接

Dim strCon As String = " Provider = Microsoft.Jet.OLEDB.4.0 ;

Data Source = db.mdb "

Dim myConn As OleDbConnection = New OleDbConnection ( )

myConn.ConnectionString = strCon

Dim strCom As String = " SELECT * FROM person "

'创建一个 DataSet

myDataSet = New DataSet ( )

myConn.Open ( )

'通过OleDbDataAdapter对象得到一个数据集

Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter

( strCom , myConn )

'把Dataset绑定books数据表

myCommand.Fill ( myDataSet , "person" )

'关闭此数据连接

myConn.Close ( )

End Sub

'插入数据记录操作代码

Private Sub button2_Click (ByVal sender As Object , _

ByVal e As System.EventArgs) Handles button2.Click

'判断所有字段是否添完,添完则执行,反之弹出提示

If ( t_id.Text "" And t_xm.Text ""

And t_xb.Text "" And t_nl.Text ""

And t_books.Text "" ) Then

Dim myConn1 As String = " Provider = Microsoft.Jet.OLEDB.4.0 ;

Data Source = db.mdb"

Dim myConn As OleDbConnection = New OleDbConnection ( myConn1 )

myConn.Open ( )

Dim strInsert As String = " INSERT INTO person ( id , xm , xb , nl

, zip ) VALUES ( " _

t_id.Text + " , '" _

t_xm.Text + "' , '" _

t_xb.Text + "' , " _

t_nl.Text + " , " _

t_books.Text + ")"

Dim inst As OleDbCommand = New OleDbCommand ( strInsert , myConn )

inst.ExecuteNonQuery ( )

myConn.Close ( )

myDataSet.Tables ( "person" ).Rows ( myBind.Position ).BeginEdit ( )

myDataSet.Tables ( "person" ).Rows ( myBind.Position ).EndEdit ( )

myDataSet.Tables ( "person" ).AcceptChanges ( )

Else

MessageBox.Show ( "必须填满所有字段值!" , "错误!" )

End If

End Sub

VB.net怎么访问数据集?我是VS2019的。

可用ado.net访问。

换电脑数据还在要求有服务器,要么是局域网内一台服务器,要么是公网服务器。


分享标题:vb.net数据集操作,vb数据集控件
链接分享:http://bjjierui.cn/article/hceoec.html

其他资讯