网络实例错误

时间:2015-05-03 15:35:57

标签: vb.net

以下是我在vb.net中的代码。这个连接字符串的问题是系统读取它,编译它但是当我试图将任何输入传递到数据库时它会告诉我network instance error.

您在此处可以看到连接的原始代码:

  Module MyFunction
Public cnn As SqlClient.SqlConnection
Public Sub MakeConnection()
    cnn = New SqlClient.SqlConnection
    If Not cnn.State = ConnectionState.Open Then

        cnn.ConnectionString = System.IO.File.ReadAllText(Application.StartupPath & "\cnn.txt")
        cnn.Open()
    End If 

这给了我"executereader is not initialized"的错误 所以我改变了这样:

    Public cnn As SqlClient.SqlConnection
Public Sub MakeConnection()
    cnn = New SqlClient.SqlConnection("Data Source=USARES-PC\SQLEXPRESS;Initial Catalog=HotelDB;Integrated Security=True")
    cnn.Open()
    '  MessageBox.Show("Connection opened!")
End Sub 

我也尝试过这种方式:

       Public cnn As SqlClient.SqlConnection
    Public Sub MakeConnection() 
     If cnn.State = ConnectionState.Open Then
        cnn.ConnectionString = "Data Source=USARES-PC\SQLEXPRESS;Initial       Catalog=HotelDB;Integrated Security=True"
          cnn.Close()
        End If

0 个答案:

没有答案