求vb.net 链接access的例子
本人是搞javascript方面开发的,对vb.net一窍不通,网上搜了下关于vb.net 链接access的例子,但是就是调试不成功,这里希望各位帮忙,发下具体调用实例,最好是完整的页面代码都贴出来,因为对vb.net语法也不熟。
数据库:e:\test\testdb.mdb
表:products
谢谢了,在线等 --------------------编程问答-------------------- 自己顶一个~~ --------------------编程问答-------------------- 这是一个操作access的类。
操作步骤:申明一个DataAccess的类。然后通过指定连接数据库的路径。然后用OpenConnection来建立连接。用QueryDatabase来查询,用UpdateDatabase来操作数据库。
给出一个例子:
Dim dataTable As DataTable
Dim strDataPath As String
Dim dataFaceCls As New DataAccess
dataFaceCls.DataPath = "C:\1.mdb"
dataFaceCls.OpenConnection()
类代码如下:
--------------------编程问答-------------------- 不管怎么说 先谢了
Imports System.io
Imports System.Data
Imports System.Data.OleDb
Public Class DataAccess
Public DataPath As String '数据库连接的路径
Public UserID As String '数据库连接的用户名
Public UserPwd As String '数据库连接的密码
Private adapter As OleDbDataAdapter
Private conn As OleDbConnection
Private Sub Initial()
Dim connString As String
'connString = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Password=" & UserPwd & ";User ID=" & UserID & ";Data Source=" & DataPath & ";Persist Security Info=True"
conn = New OleDbConnection(connString)
adapter = New OleDbDataAdapter
adapter.SelectCommand = New OleDbCommand("", conn)
End Sub
'Update database ON SQL
Public Sub UpdateDatabase(ByVal sqlText As String)
Try
adapter.SelectCommand.CommandText = sqlText
adapter.SelectCommand.ExecuteNonQuery()
Catch ex As Exception
Throw ex
Finally
' CloseConnection()
End Try
End Sub
'Update database ON Table
Public Sub UpdateDatabase(ByVal dt As DataTable)
Try
Dim oCB As New OleDbCommandBuilder(adapter)
adapter.Fill(dt)
adapter.Update(dt)
Catch ex As Exception
Throw ex
Finally
' CloseConnection()
End Try
End Sub
'Return Data in DataSet on SQL and TableName
Public Function QueryDatabase(ByVal sqlText As String) As DataTable
Dim ds As New DataSet
Try
adapter.SelectCommand.CommandText = sqlText
adapter.Fill(ds, "DATA_TABLE")
Return ds.Tables("DATA_TABLE")
Catch ex As Exception
Throw ex
End Try
End Function
'Open database connection.
Public Sub OpenConnection()
Initial()
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
End Sub
'Close database connection.
Public Sub CloseConnection()
If conn.State = ConnectionState.Open Then
conn.Close()
End If
End Sub
Public Function BeginTransaction() As OleDb.OleDbTransaction
Dim tx As OleDb.OleDbTransaction
Me.OpenConnection()
tx = conn.BeginTransaction
adapter.SelectCommand.Transaction = tx
Return tx
End Function
Public Function GET_COLUMN_MSG(ByVal v_table_name As String, ByVal v_num As String, ByVal v_type As String) As String
OpenConnection()
Dim adjCmd As New OleDbCommand
Dim v_owner As String = "ARM"
Dim strSQL As String
Dim v_column_name As String
adjCmd.Connection = conn
Select Case v_type
Case "TITLE" '表示取得是抬頭
strSQL = "SELECT COLUMN_NAME FROM " & _
" ( select TO_CHAR( ROWNUM ) NUM,COLUMN_NAME FROM ALL_TAB_COLUMNS " & _
" WHERE OWNER ='" & v_owner & "' AND TABLE_NAME = '" & v_table_name & "')" & _
" WHERE (NUM =" & v_num & ")"
adjCmd.CommandText = strSQL
v_column_name = adjCmd.ExecuteScalar().ToString()
strSQL = " Select SUBSTR(COMMENTS, 1, 20) FROM ALL_COL_COMMENTS " & _
" WHERE OWNER='" & v_owner & "' AND TABLE_NAME='" & v_table_name & "' AND " & _
" COLUMN_NAME = '" & v_column_name & "'"
Case "LEN" ' 表示取得是長度
strSQL = "SELECT DATA_LENGTH FROM " & _
" ( select TO_CHAR( ROWNUM ) NUM,DATA_LENGTH FROM ALL_TAB_COLUMNS " & _
" WHERE OWNER ='" & v_owner & "' AND TABLE_NAME ='" & v_table_name & "')" & _
" WHERE NUM=" & v_num
Case "NAME" ' 表示取得是名字
strSQL = "SELECT COLUMN_NAME FROM " & _
" ( select TO_CHAR( ROWNUM ) NUM,COLUMN_NAME FROM ALL_TAB_COLUMNS " & _
" WHERE OWNER ='" & v_owner & "' AND TABLE_NAME = '" & v_table_name & "')" & _
" WHERE (NUM =" & v_num & ")"
End Select
Return CStr(adjCmd.ExecuteScalar)
End Function
Public Function GetCmd(ByVal sqlText As String) As OleDbCommand
Dim cmd As New OleDbCommand(sqlText, conn)
Return cmd
End Function
Public Function GetOnceValue(ByVal sqlText As String) As String
Try
adapter.SelectCommand.CommandText = sqlText
Dim oValue As Object = adapter.SelectCommand.ExecuteScalar
If oValue Is DBNull.Value Or oValue Is Nothing Then
Return ""
Else
Return CType(oValue, String)
End If
Catch ex As Exception
Throw ex
Finally
End Try
End Function
'GetLoginPersonNo: 根據登陸號獲取用戶ID
'輸入參數:
' seq: 用戶登陸號
'返回值:String 用戶ID
Public Function GetLoginPersonNo(ByVal seq As String) As String
Dim ds As DataTable
ds = QueryDatabase("select USERID from login where LOGIN_NO='" & seq & "'")
If ds.Rows.Count > 0 Then
Return CStr(ds.Rows(0)(0))
Else
Return ""
End If
End Function
'Public Function GetPersonName(ByVal USERID As String) As String
' Dim ds As DataTable
' Return Me.GetOnceValue("SELECT PERSON_NAME FROM PERSON WHERE PERSON_NO='" & USERID & "'")
'End Function
'Public Function GetGRPName(ByVal USERID As String) As String
' Dim ds As DataTable
' Return Me.GetOnceValue("SELECT G.GRP_NAME FROM GRP G,PERSON_GRP PG WHERE PG.PERSON_NO='" & USERID & "' AND G.GRP_NO=PG.GRP_NO")
'End Function
Public Function IsAqua(ByVal personNo As String) As Boolean
Dim strSql As String = "SELECT NVL(COUNT(*),0) FROM PERSON_GRP WHERE GRP_NO IN ('000006','000009') AND PERSON_NO='" & personNo & "'"
Return Int32.Parse(GetOnceValue(strSql)) > 0
End Function
End Class
但是对于一点vb.net基础都没有的我来说,你这代码是不是也忒多了点? --------------------编程问答-------------------- 多是多,但是操作起来非常方便。你把上面的代码保存为一个DataAccess.vb文件,然后添加到项目里面去,然后调用就可以啦。
--------------------编程问答-------------------- <%
Dim con As OleDb.OleDbConnection
Dim strConnect As String="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:\LG\aspx\products.mdb"
con= New System.Data.OleDb.OleDbConnection(strConnect)
dim rs as OleDb.OleDbDataAdapter = new OleDb.OleDbDataAdapter("select * from products",con)
Dim ds As New DataSet
Dim dr As DataRow
Dim dt As DataTable
rs.Fill(ds)
dt = ds.Tables(0)
dr = dt.Rows(0)
'dt.Rows.Count
%>
这里的dt.Rows.Count是该表的记录集数目
我想知道怎样循环查找每条记录?
asp里是
While Not rsCat.EOF
...
rsTep.MoveNext
Wend
vb.net里呢?
--------------------编程问答-------------------- 先定义变量
Public gcnnGdzc As New System.Data.OleDb.OleDbConnection '连接器
Public gcomGdzc As New System.Data.OleDb.OleDbCommand '数据表命令
Public gdstGdzc As New System.Data.DataSet '数据集
Public gdadGdzc As New System.Data.OleDb.OleDbDataAdapter(gcomGdzc) '适配器
gcnnGdzc = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:\test\testdb.mdb")
gcomGdzc.Connection = gcnnGdzc
gcnnGdzc.Open() '打开数据库连接
gcomGdzc.CommandText = "SELECT * FROM products"
gdadGdzc.Fill(gdstGdzc, "T_products")
gdstGdzc.Tables("T_products")即为需要连接的表 --------------------编程问答-------------------- Dim conn as new oledb.oledbconnectio("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=e:\test\testdb.mdb")
conn.open() --------------------编程问答-------------------- 楼上正解,但是
sqlCat = New OleDbCommand("select * from category", con)
con.Open()
rsCat = sqlCat.ExecuteReader
怎样依次循环查询表中的记录呢?
我知道asp里是
<%
While Not rsCat.EOF
...
rsCat.MoveNext
Wend
%>
.net呢?麻烦各位~~ --------------------编程问答-------------------- Do While rsCat.Read()
...
Loop
这个对么? --------------------编程问答-------------------- 在补充一下,刚才查阅资料,好像.Read()这种方法在下次查询表之前要关闭,也就是说在查询tab1中数据的同时,不能通过Read()方法查询tab2中的数据,是这样么?怎么解决? --------------------编程问答-------------------- 是这样的。你可以把第一个read()读出来的值先保存起来,然后关闭,再调用下一次查询。然后再使用上次保存的值。 --------------------编程问答-------------------- 学习 --------------------编程问答-------------------- 实例下载
http://jerrysoft.net/thread-228-1-1.html
补充:.NET技术 , VB.NET