dim
目前创新互联公司已为上千多家的企业提供了网站建设、域名、网络空间、网站托管、企业网站设计、鱼峰网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
myselectquery
as
string
=
"select
*
from
表1
where
姓名='小强'"
dim
mycommand
as
new
sqlcommand
(myselectquery,
conn)
'建立一个command控件,conn是你的sqlconnection对象
conn.open()'打开数据连接
dim
myreader
as
sqldatareader'定义一个reader用来读数据
myreader
=
mycommand.executereader()'运行你的查询,结果到myreader
if
myreader.read()
then
'如果查到了数据
msgbox(myreader.getstring(0))
'显示第一个字段
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,可以直接在项目中添加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()
'关闭连接
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款