且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将sql数据库服务器连接到vb.net?

更新时间:2023-02-04 08:30:33

你可以像这样硬编码

You can hardcode it like this
var connection = New System.Data.SqlClient.SqlConnection(connectionString)
connection.Open()



或者你可以更独立地完成它


or you can do it more independently

var factory = System.Data.Common.DbProviderFactories.GetFactory(providerString)
var connection = factory.CreateConnection()
connection.ConnectionString = connectionString
connection.Open()





有很多那里的信息,然后我有一篇关于连接数据库的文章,没有硬编码你的提供者这里


将此代码粘贴到连接模块然后在项目中的任何地方获取连接。



您需要连接的地方简单



以下功能调用。

conn1 = GetConnectIcSoft



然后



conn1.open()



Paste this code in connection Module then and get connection anywhere in project.

where you need connection simple you

call below function like that.
conn1=GetConnectIcSoft

then

conn1.open()

Public Conn1 As SqlConnection
Public Function GetConnectIcSoft()
Conn1 = New SqlConnection(Data Source=SERVERNAME; Initial Catalog= DATABASE_NAME; User Id=sa; Password=YOUR_SQL_PASSWORD;Integrated Security = True;MultipleActiveResultSets=True;)
Return conn1
End Function