vbnet读取数据库
⑴ 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写一个读取数据库数据的简单操作。
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 ProctID, UnitPrice, ProctName from dbo.Procts " _
& "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数据库,读取查询并显示
给你写个例子,不明白,再问!!
'引入OLEDB命令空间
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'定义一个OLEDB连接并实例化它
Dim con As New OleDbConnection
'定义一个OLEDB命令并实例化他
Dim cmd As New OleDbCommand
'定义一个OLEDBReader方法来读取数据库
Dim dr As OleDbDataReader
'初始化con的连接属性,使用OLEDB模式,数据源为:你指定下路径,我的是在D盘
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\dataSample.mdb"
'打开OLEDB数据连接
con.Open()
'初始化OLEDB命令的连接属性为con,这个需要你理解下
cmd.Connection = con
'初始化OLEDB命令的语句 就是查询 什么字段从什么表 条件是ID等于你在t1中输入的内容
cmd.CommandText = "select keyss from table1 where ID=" & t1.Text & ""
'执行OLEDB命令以ExecuteReader()方式,并返回一个OLEDBReader,赋值给dr
dr = cmd.ExecuteReader()
'判断下dr中是否有数据。如果有就把第一个值赋值给t2的值
If dr.Read() Then
t2.Text = dr(0)
End If
'完成后关闭dr.con等释放资源
dr.Close()
con.Close()
End Sub
End Class