vb.net 如何在 ASP.NET VB 中选择和插入 Microsoft Access 数据库 (accdb)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19647109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select and insert In Microsoft Access database (accdb) in ASP.NET VB
提问by user2795514
Hey Everyone I am new to asp.net and I would like to select data from a database and store the data in a DataTable in VB not C#, but I just cant seem to understand how to connect. Can anyone help me try to connect to an access database? I am so lost and have been for days. if not its ok.
大家好我是asp.net的新手,我想从数据库中选择数据并将数据存储在VB而不是C#中的DataTable中,但我似乎无法理解如何连接。谁能帮我尝试连接到访问数据库?我很迷茫,已经好几天了。如果不是它确定。
Thank you for reading.
感谢您的阅读。
Brent
布伦特
Public Function CheckUser(ByVal p_strUserNAME As String, ByVal p_Password As String) As Boolean
Dim blnAdminUser As Boolean = False
Dim SQLQuery As String = "SELECT Username, Password FROM HomelessUsers WHERE Username = " & p_strUserNAME & " AND Password = " & p_Password
Dim MDBConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\HomelessCapstone\HomelessCapstone\HomelessCapstone\APP_Data\Homeless.accdb;Persist Security Info=True"
Dim ds As New DataSet
Dim cnn As OleDbConnection = New OleDbConnection(MDBConnectionString)
cnn.Open()
Dim cmd As New OleDbCommand(SQLQuery, cnn)
Dim da As New OleDbDataAdapter(cmd)
da.Fill(ds, "HomelessUsers")
cnn.Close()
'Dim DatatableTest As DataTable = ds.Tables("HomelessUsers")
'Dim Row As DataRow = Nothing
'Dim Item(2) As String
'For Each Row In DatatableTest.Rows
'item()
' Next
Return blnAdminUser
End Function
回答by Malcolm Salvador
Basically, there are two ways you can go about it. One is to use ADO (access database objects) which has an intiutive wizard that will help you connect your access database to your program and set datasource for your controls, Or you can use System.Data.OLEDb (I prefer this as it gives me more control over my system)
基本上,您可以通过两种方式进行处理。一种是使用 ADO(访问数据库对象),它有一个直观的向导,可以帮助您将访问数据库连接到您的程序并为您的控件设置数据源,或者您可以使用 System.Data.OLEDb(我更喜欢这个,因为它给了我更好地控制我的系统)
Dim accessconn As New _
System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & pathofAccessFile)
Try
accessconn.Open()
MsgBox("Access succesfully connected")
accessconn.Close()
Return 0
Catch ex As Exception
accessconn.Close()
MsgBox("There is something wrong with the path provided.")
Return 1
End Try
You will need an understanding of using OLEDb classes though, so here is something to help you out. http://msdn.microsoft.com/en-us/library/System.Data.OleDb(v=vs.110).aspx
不过,您需要了解使用 OLEDb 类,因此这里有一些可以帮助您的方法。 http://msdn.microsoft.com/en-us/library/System.Data.OleDb(v=vs.110).aspx
Take note of OLEDbCommand(Execute SQL strings), OLEDbDataAdapter(Translate data from DB to Datagridview) and OLEDbDataReader(I use this for selecting data from the Database).
记下 OLEDbCommand(执行 SQL 字符串)、OLEDbDataAdapter(将数据从 DB 转换为 Datagridview)和 OLEDbDataReader(我使用它从数据库中选择数据)。
Perhaps to help you more with understanding your problem, here is an example of how I use DataReader Class:
也许为了帮助您更好地理解您的问题,以下是我如何使用 DataReader 类的示例:
Public Sub AccesstoGridd(ByVal utos As String, ByVal pathh As String, ByVal gridd As DataGridView, ByVal ngalan As String)
Dim accessconn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & pathh)
Try
Dim sqlutos As New System.Data.OleDb.OleDbCommand(utos, accessconn)
Dim idcounter As Integer = 0
accessconn.Open()
Dim reader As System.Data.OleDb.OleDbDataReader
reader = sqlutos.ExecuteReader
While reader.Read
idcounter = idcounter + 1
gridd.Rows.Add(idcounter, reader(0), reader(1), reader(2), reader(3), reader(4), reader(5), ngalan)
End While
reader.Close()
accessconn.Close()
Catch ex As Exception
accessconn.Close()
MsgBox("There is something wrong with the Access file selected." & vbNewLine & ex.ToString, MsgBoxStyle.Exclamation, "MDB file unrecognized.")
End Try
End Sub

