vb.net 怎么操作数据库

2024-11-30 20:53:51
推荐回答(3个)
回答(1):

如果楼主熟悉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() '关闭连接

回答(2):

  Private stroledbprovider As String = "System.Data.OleDb" '大小写 -------更改此处可连接不同类型的数据库
'连接数据库的信息,更改连接不同数据库信息-------"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=datapath;Persist Security Info=false"
Private stroledbconn As String = "Provider=SQLOLEDB;Data Source=localhost,10000;Initial Catalog=haofefe;user id=sa ; password=123" 'Integrated Security=SSPI"
'*********************************************************************

'************生成Dbproviderfactory,idbconnection,idbcommand,and idatareader********
Dim cnfactory As IDbConnection
Dim drcustsreader As IDataReader
Dim cmfactory As IDbCommand
Dim dpfactory As DbProviderFactory
Public login As Boolean = False
Private Sub createconn()
Try
dpfactory = System.Data.Common.DbProviderFactories.GetFactory(stroledbprovider)
cnfactory = dpfactory.CreateConnection
cnfactory.ConnectionString = stroledbconn
cmfactory = cnfactory.CreateCommand
cmfactory.CommandType = CommandType.Text

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
'*********************************************************

'利用生成的连接
'****************查询数据**************
Public Function getsources(ByVal strcomm As String) As DataTable

Dim i As Integer
Try
Call createconn() '调用生成实例
cmfactory.CommandText = strcomm
getsources = New DataTable
cnfactory.Open()
drcustsreader = cmfactory.ExecuteReader(CommandBehavior.KeyInfo)
With drcustsreader
For i = 0 To .FieldCount - 1
getsources.Columns.Add(.GetName(i))
Next
While .Read
Dim objcells(.FieldCount - 1) As Object
.GetValues(objcells)
getsources.Rows.Add(objcells)
End While

End With
drcustsreader.Close()
'getsources.Load(drcustsreader)
Return getsources
cnfactory.Close()
Catch ex As Exception
cnfactory.Close()
Return New Data.DataTable
MsgBox(ex.ToString)
End Try
End Function
'**********************************
'-------------------------------------------------------------------------------------------------------------
'*******************查看已连接信息******************
Public Sub connectionstatistics(ByVal conn As SqlConnection)
Dim htstats As Hashtable
Try
htstats = CType(conn.RetrieveStatistics, Hashtable)
Dim strstats As String
strstats = "ServerVersion: " + conn.ServerVersion.ToString + ControlChars.CrLf
Dim ostat As Object
Dim strstat As String
For Each ostat In htstats.Keys
strstat = ostat.ToString
If InStr(strstat, "Time") > 0 Then
strstats = strstats + strstat + "=" + Microsoft.VisualBasic.Format(CLng(htstats(strstat)) / 1000, "#,##0.000") + " secs" + vbCrLf
Else
strstats = strstats + strstat + "=" + htstats(strstat).ToString + ControlChars.Cr + ControlChars.Lf
End If

Next
MsgBox(strstats, MsgBoxStyle.Information, "Connection Statistics")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

回答(3):

用top1