VB.NET怎样连接数据库
发布网友
发布时间:2022-04-09 22:48
我来回答
共2个回答
热心网友
时间:2022-04-10 00:17
Imports System.Data
Imports System.Data.SqlClient
Mole 连接数据库
Public Server1 As String
Public DataBase1 As String
Public Function GetTable(ByVal sqlstr$) As DataTable
Dim SqlConn As SqlConnection = New SqlConnection("server=" & Server1 & ";database=" & DataBase1 & ";integrated security=true") '声明这些SQL的类
Dim facDataAdapter As SqlDataAdapter = New SqlDataAdapter
Dim SqlComm As SqlCommand = New SqlCommand(sqlstr, SqlConn)
facDataAdapter.SelectCommand = SqlComm
Dim dt As DataTable = New DataTable
facDataAdapter.Fill(dt)
Return dt
End Function
End Mole
热心网友
时间:2022-04-10 01:35
你看看下面这个例子,数据库Access
Imports System.Data.OleDb
Public Class ex_1
Dim MyConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\App_data\Class.mdb")
Dim MyCommand As OleDbCommand
Dim MyReader As OleDbDataReader
Dim dbset As New DataSet
Dim dataA As OleDbDataAdapter
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MyConnection.Open()
MyCommand1 = New OleDbCommand("Select * from info where cn='002'", MyConnection)
MyReader = MyCommand.ExecuteReader()
While MyReader.Read()
TextBox1.Text = MyReader("tutor")
End While
MyConnection.Close()
MyCommand1.Dispose()
MyReader.Close()
End Sub