vb.net数据库读写,vbnet操作数据库

vb.net数据库操作

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

创新互联是专业的德保网站建设公司,德保接单;提供成都网站设计、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行德保网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

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中,读取和写入文件

写入:Dim sr As New IO.StreamWriter(Application.StartupPath "/写入的文本.txt")

sr.WriteLine("写入的内容") sr.Close()读取:If (File.Exists(Application.StartupPath "/msg.txt")) Then

Dim fm As New IO.FileStream(Application.StartupPath "/读取的文本.txt", FileMode.Open)

Dim sr As IO.StreamReader = New IO.StreamReader(fm)

Do While sr.Peek() = 0

TextBox1.Text = sr.ReadLine() (读取文本到文本框)

Loop end if

求用vb.net写一个读取数据库数据的简单操作。

Option Explicit On

Option Strict On

Imports System

Imports System.Data

Imports System.Data.SqlClient

Public Class Program

Public Shared Sub Main()

Dim connectionString As String = _

"Data Source=(local);Initial Catalog=Northwind;" _

"Integrated Security=true"

' Provide the query string with a parameter placeholder.

Dim queryString As String = _

"SELECT ProductID, UnitPrice, ProductName from dbo.Products " _

"WHERE UnitPrice @pricePoint " _

"ORDER BY UnitPrice DESC;"

' Specify the parameter value.

Dim paramValue As Integer = 5

' Create and open the connection in a using block. This

' ensures that all resources will be closed and disposed

' when the code exits.

Using connection As New SqlConnection(connectionString)

' Create the Command and Parameter objects.

Dim command As New SqlCommand(queryString, connection)

command.Parameters.AddWithValue("@pricePoint", paramValue)

' Open the connection in a try/catch block.

' Create and execute the DataReader, writing the result

' set to the console window.

Try

connection.Open()

Dim dataReader As SqlDataReader = _

command.ExecuteReader()

Do While dataReader.Read()

Console.WriteLine( _

vbTab "{0}" vbTab "{1}" vbTab "{2}", _

dataReader(0), dataReader(1), dataReader(2))

Loop

dataReader.Close()

Catch ex As Exception

Console.WriteLine(ex.Message)

End Try

Console.ReadLine()

End Using

End Sub

End Class

这是我在vs2010中微软自带的MSDN示例代码里面拷的,是关于ADO.net连接sql的操作。

希望对你有帮助。 如果你还需要其他的,我也可以再拷给你看。

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读取Access数据库的方法

建立数据集,绑定到窗口。不会的话,把要用的表从数据集拖到窗口,再删除就行了

提取方法

数据及名称.表格名称.rows(2).item(3).tostring 就是了

vb.NET 关于数据库的读写 - 高分

Dim da As New OleDb.OleDbDataAdapter(‘’输入2/3/4其中的答案)

Dim ds As New DataSet

da.Fill(ds, "Table1")

Dim dt As DataTable

dt = ds.Tables("Table1")

//2. Cmd.CommandText = "select 序号 from 账号 where 姓名='小李'"

//3. Cmd.CommandText = "select 序号 from 账号 where 姓名='小张'"

//4. Cmd.CommandText = "insert into 账号(姓名,年龄,性别,序号) values('张三','22','男','4')"

DataGridView1.DataSource = dt '建立datagridview来显示资料

myCn.Close()


名称栏目:vb.net数据库读写,vbnet操作数据库
文章起源:http://bzwzjz.com/article/hcsdeh.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 古蔺网站建设 响应式网站设计 自适应网站建设 网站设计 成都网站建设公司 成都网站设计 成都营销网站制作 成都网站制作 广安网站设计 成都网站建设 高端定制网站设计 手机网站制作 成都网站建设 自适应网站设计 定制网站建设 成都网站制作 企业网站建设 成都网站建设 定制网站建设多少钱 四川成都网站制作 成都网站建设流程 成都网站设计