当前位置:首页 » 操作系统 » vbnet读取数据库数据

vbnet读取数据库数据

发布时间: 2024-10-22 19:06:23

㈠ 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

㈡ 求用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 中如何使用SQL语句查询数据库中的数据

1、首先打开Visual Studio 2008代码窗口,添加引用。

㈣ VB.net语言编程,反复调用SQL语句编写的数据读取函数来读取数据库Access中的数据,出现崩溃的问题

下面这段代码中,myReader,myCon的关闭应在WHILE循环外。这还不是主要问题,如果你只读取首行首列不要用OleDbDataReader,直接用myCommand.ExecuteScalar就可以了,只要判断一下myCommand.ExecuteScalar返回是否为nothing就行。效率会高很多。
While myReader.Read
If myReader.Item(0) Is System.DBNull.Value Then
Return ""
Else
Return myReader.Item(0)
End If
myReader.Close()
myCon.Close()
End While

热点内容
python怎么打开idle 发布:2024-10-22 21:25:56 浏览:754
安卓开发版怎么设置中文 发布:2024-10-22 21:18:09 浏览:941
安卓系统未找到固件怎么办 发布:2024-10-22 21:18:03 浏览:183
解压捏烟 发布:2024-10-22 21:17:57 浏览:388
二维otsu算法 发布:2024-10-22 21:06:23 浏览:634
福禄寿源码 发布:2024-10-22 21:06:13 浏览:434
pythongoagent 发布:2024-10-22 20:42:05 浏览:78
matlabpid算法 发布:2024-10-22 20:37:51 浏览:332
文件遍历编程 发布:2024-10-22 20:37:47 浏览:622
nip服务器地址在哪 发布:2024-10-22 20:17:48 浏览:621