當前位置:首頁 » 操作系統 » vbnet讀取資料庫

vbnet讀取資料庫

發布時間: 2023-05-29 20:15:44

⑴ 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

熱點內容
phpmyadminlinux安裝 發布:2024-03-29 17:13:15 瀏覽:791
python中replace 發布:2024-03-29 17:08:17 瀏覽:652
mdb導入sql 發布:2024-03-29 17:07:36 瀏覽:127
java資料庫工具類 發布:2024-03-29 16:31:15 瀏覽:387
安卓機哪裡看型號 發布:2024-03-29 15:59:40 瀏覽:281
cad解壓錯誤 發布:2024-03-29 15:01:45 瀏覽:79
存儲指令集 發布:2024-03-29 14:39:27 瀏覽:649
資料庫表刪除數據 發布:2024-03-29 14:39:26 瀏覽:367
出c語言整除 發布:2024-03-29 14:28:22 瀏覽:572
芬尼壓縮機 發布:2024-03-29 14:24:11 瀏覽:464