【问题标题】:Connect SQL Server with VB.net on button click单击按钮将 SQL Server 与 VB.net 连接
【发布时间】: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


【解决方案1】:

在集成安全的情况下,您不必使用用户名和密码。

【讨论】:

  • 如果我想同时包含密码和用户名,是否需要删除集成安全性?
【解决方案2】:

您是尝试使用“sa”登录名还是特定用户进行连接?如果您尝试使用“sa”登录,请确保“sa”登录已激活。

如果您已经激活“sa”登录或尝试使用自己的用户名登录,只需将 Integrated Security=true 更改为“Integrated Security=SSPI”

【讨论】:

    猜你喜欢
    • 2016-01-10
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 2011-10-28
    相关资源
    最近更新 更多