當前位置:首頁 » 操作系統 » 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