VB.NET怎样连接数据库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VB.NET怎样连接数据库相关的知识,希望对你有一定的参考价值。

如果数据库是用WINDOWS默认的方式登录,VB.NET怎样连接数据库

参考技术A Imports System.Data
Imports System.Data.SqlClient

Module 连接数据库
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 Module本回答被提问者采纳
参考技术B 你看看下面这个例子,数据库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

以上是关于VB.NET怎样连接数据库的主要内容,如果未能解决你的问题,请参考以下文章

vb.net 连接access数据库

在vb.net中怎样浏览首记录 、上一条、下一条、尾记录、和数据库access 绑定的

vb.net如何连接远程db2数据库

用vb.net怎么连接其他电脑上的oracle数据库

关于VB.NET连接字符串

vb.net连接access数据库问题