【发布时间】:2017-02-01 07:32:40
【问题描述】:
我还是 vb 的新手,想创建一个表单,在文本框中手动输入用户名和密码,并在组合框中选择服务器和数据库。我确实参考了这个链接testconnection,但我不想设置连接字符串,我想在手动输入服务器、数据库、用户名和密码后测试连接。这是我得到的错误,即使我输入了正确的数据After button test is clicked 我的编码有什么问题吗?
`
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connectString1 As String
Dim cnn As SqlConnection
server = Me.cmbServers.Text
database = Me.cmbDatabases.Text
username = Me.TextBoxUsername.Text
password = Me.TextBoxPassword.Text
'Set connection string with selected server and integrated security
connectString1 = "Data Source=" & cmbServers.Text & ";Initial Catalog=" & cmbDatabases.Text & ";User ID=" & TextBoxUsername.Text & ";Password=" & TextBoxPassword.Text & " Integrated Security=True"
cnn = New SqlConnection(connectString1)
Try
cnn.Open()
MsgBox("Connection Open ! ")
cnn.Close()
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub`
【问题讨论】:
-
本质上,您要求的是 ADO.NET 教程。其中有很多——这些概念自 2002 年的 .NET 1.0 以来一直保持不变。从设置中加载连接字符串也是如此。提示:转到您项目的设置并检查“设置”选项卡。
-
检查例如 MSDN 中的 How to: Save and Edit Connection Strings、文档的 ADO.Net 部分或通过 Visual Studio Dev Essentials 免费提供的 Pluralsight 课程之一
-
你的编码有什么问题吗?你告诉我们 - 当你运行它时它会起作用吗?
-
我确实打开了很多与之相关的教程,但没有一个有助于解决我的问题。每次单击按钮时,都会出现“无法打开连接”的消息。是不是因为我设置了错误的连接字符串?
-
您需要向我们提供来自
Catch语句的Exception对象的错误消息。您可以通过设置休息时间或使用以下命令来做到这一点:MsgBox(ex.Message)。按 Ctrl-C 将 MsgBox 文本复制到剪贴板。
标签: sql-server vb.net