1

所以这是我的代码:

Private adoConnection As System.Data.SqlClient.SqlConnection
Private adoadapterFillDSUsers As New System.Data.SqlClient.SqlDataAdapter()
Private ServerName As String = ""
Private ConnectionString As String = ""

 Private Sub adoconn()
        adoConnection = New System.Data.SqlClient.SqlConnection()
        adoConnection.ConnectionString = DataManager.DataManager.ConnectionString
    End Sub
    Public Sub ContinueInit()
        ReadRegistry()
        Dim currRegKey As RegistryKey
        Dim newRegKey As RegistryKey
        currRegKey = Registry.CurrentConfig
        Try
            newRegKey = currRegKey.OpenSubKey("Hoh_Dev\Data")
        Catch ex As Exception
            Return
        End Try
        Try
            MREZA = newRegKey.GetValue("Mreza")
            ServerName = newRegKey.GetValue("ServerName")
        Catch ex As Exception
            Return
        End Try

        If MREZA = "D" Then
            ConnectionString = "Data Source=" & ServerName & ";Integrated Security = SSPI;"
        Else
            If MREZA = "W" Then
                ConnectionString = "Data Source=" & ServerName & ";Database=" & DataBaseName & ";user id=xxx;pwd=xxx;"
            End If
        End If
        adoconn()
    End Sub

自从每次我想连接到我的服务器并使用它时,我已经失去了理智:

If adoConnection.State = ConnectionState.Closed Then adoConnection.Open()
Try
  DataManager.DataManager.adoCommand.ExecuteNonQuery()
Catch OpenConnectionError As System.Data.SqlClient.SqlException
End Try

我收到了这个错误:

The ConnectionString property has not been initialized.

ContinueInit我从表单加载事件中调用sub:

 Private Sub LogIn_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ContinueInit()

        Me.TextBox1.Focus()
        Me.TransparencyKey = BackColor
        Me.Button2.Visible = False
        Me.Button1.Visible = False
        TextBox2.PasswordChar = "*"
    End Sub

代码破裂,adoConnection.Open()这意味着我什至无法打开与服务器的连接,因此我可以执行 SQL 查询,这只会导致我没有设置好的东西。

4

1 回答 1

1

您的连接对象没有ConnectionString填充。

不确定 DataManager.DataManager 是什么,但我猜您正在做一些事情,即在 DataManager 类中创建 DataManager 类的共享实例,并将其称为 DataManager。(非常令人困惑)但是,我猜您需要查看此内容并逐步执行您的代码以找出连接字符串为空字符串的原因。

于 2013-10-14T20:59:29.863 回答