vbnet讀取資料庫數據
㈠ 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