dim
目前創(chuàng)新互聯(lián)公司已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、魚峰網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
myselectquery
as
string
=
"select
*
from
表1
where
姓名='小強(qiáng)'"
dim
mycommand
as
new
sqlcommand
(myselectquery,
conn)
'建立一個(gè)command控件,conn是你的sqlconnection對(duì)象
conn.open()'打開數(shù)據(jù)連接
dim
myreader
as
sqldatareader'定義一個(gè)reader用來讀數(shù)據(jù)
myreader
=
mycommand.executereader()'運(yùn)行你的查詢,結(jié)果到myreader
if
myreader.read()
then
'如果查到了數(shù)據(jù)
msgbox(myreader.getstring(0))
'顯示第一個(gè)字段
end
if
Dim Cnn As New Data.OleDb.OleDbConnection
Dim DbAdapter As New Data.OleDb.OleDbDataAdapter
Dim Cmd As New Data.OleDb.OleDbCommand
Dim dsAll As New Data.DataSet
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\webapp\test\web.mdb"
Cmd.Connection = Cnn
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'插入
Cmd.CommandText = "insert into main (about) values ('" + TextBox1.Text + "')"
UpdateTable()
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
'修改
Dim ids As Integer
ids = 1
Cmd.CommandText = "update main set about='" + TextBox1.Text + "' where id=" + ids.ToString()
UpdateTable()
End Sub
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
'刪除
Dim ids As Integer
ids = 1
Cmd.CommandText = "delete from main where id=" + ids.ToString()
UpdateTable()
End Sub
Public Sub UpdateTable()
Dim Trans As Data.OleDb.OleDbTransaction
Try
Cnn.Open()
Trans = Cnn.BeginTransaction()
Try
Cmd.Transaction = Trans
Cmd.ExecuteNonQuery()
Trans.Commit()
Catch
Trans.Rollback()
Finally
If (Cnn.State = System.Data.ConnectionState.Open) Then
Cnn.Close()
End If
End Try
Catch
'error message
End Try
End Sub
Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
'顯示
Cmd.CommandText = "select id, about from main"
DbAdapter.SelectCommand = Cmd
DbAdapter.Fill(dsAll, "table1")
If (dsAll.Tables("table1").Rows.Count 0) Then
TextBox1.Text = dsAll.Tables("table1").Rows(0)("about")
Else
TextBox1.Text = ""
End If
End Sub
如果樓主熟悉VB6,可以直接在項(xiàng)目中添加ADODB的Com引用,這樣你就可以像VB6那樣操作數(shù)據(jù)庫了!
另外
.NET
Framework中連接數(shù)據(jù)庫要用到ADO.NET。如果要操作Access數(shù)據(jù)庫,要用到System.Data.OleDb命名空間下的許多類。
比如按樓主所說,“我想在textbox1中顯示表一中【一些數(shù)據(jù)】字段下的第一個(gè)內(nèi)容”:
'首先導(dǎo)入命名空間
Imports
System.Data
Imports
System.Data.OleDb
'然后在某一個(gè)事件處理程序中寫:
Dim
conn
As
New
OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=數(shù)據(jù)庫.accdb;Jet
OLEDB:Database
Password=MyDbPassword")
Dim
command
As
New
OleDbCommand("Select
*
From
數(shù)據(jù)表",
conn)
conn.Open()
'打開數(shù)據(jù)庫連接
Dim
reader
As
OleDbDataReader
=
command.ExecuteReader()
'執(zhí)行SQL語句,返回OleDbDataReader
對(duì)象
Do
While
reader.Read()
'讀取一條數(shù)據(jù)
textbox1.Text
+=
reader("一些數(shù)據(jù)")
VbCrLf
Loop
reader.Close()
'關(guān)閉OleDbDataReader
conn.Close()
'關(guān)閉連接