我无法使用VB.NET连接到我的SQL Server数据库

时间:2015-04-29 08:45:44

标签: sql-server vb.net

Protected Sub login_btn_Click(sender As Object, e As EventArgs) Handles login_btn.Click

    Dim connString As String
    Dim connection As SqlConnection
    Dim command As SqlCommand
    Dim sql As String
    connString = "Data Source=.\SQLExpress;Initial Catalog=Suivi_Invst;Integrated Security=True"
    sql = "select NET_ID, Password from User"
    connection = New SqlConnection(connString)

    Try
        connection.Open()
        command = New SqlCommand(sql, connection)
        Dim sqlReader As SqlDataReader = command.ExecuteReader()
        While sqlReader.Read()
            If (Password.Text = sqlReader("Password") And NET_ID.Text = sqlReader("NET_ID")) Then
                Response.Redirect("Creation.aspx")
            End If
        End While

        sqlReader.Close()
        command.Dispose()
        connection.Close()
    Catch ex As Exception
        MsgBox("Can not open connection ! ")
    End Try

End Sub

我总是得到msg信箱说连接无法建立。

我尝试在代码运行的同时打开SQL Server Management Studio,但我什么也没做。

3 个答案:

答案 0 :(得分:3)

从您的例外文字:

  

关键字'用户'

附近的语法不正确
SQL Server中的

User保留关键字

基本上不建议创建名称等于保留关键字的表格,但如果您真的需要此名称,则必须在查询中用方括号括起来,如下所示:

select NET_ID, Password from [User]

答案 1 :(得分:0)

我可以看到你的连接字符串看起来像是在创建你忘记逃避“\”字符的错误。 另外,您可以在try块中移动代码,以便获得特定的错误代码

所以要逃避你可以改变行成为

connString = @“Data Source =。\ SQLExpress; Initial Catalog = Suivi_Invst; Integrated Security = True”;

connString =“Data Source =。\\ SQLExpress; Initial Catalog = Suivi_Invst; Integrated Security = True”;

所以请将您的代码更新为:

Protected Sub login_btn_Click(sender As Object,e As EventArgs)处理login_btn.Click

Dim connString As String
Dim connection As SqlConnection
Dim command As SqlCommand
Dim sql As String


Try
        connString = @"Data Source=.\SQLExpress;Initial Catalog=Suivi_Invst;Integrated Security=True"

 //OR USE BELOW
 //connString = "Data Source=.\\SQLExpress;Initial Catalog=Suivi_Invst;Integrated Security=True"
sql = "select NET_ID, Password from User"
connection = New SqlConnection(connString)
    connection.Open()
    command = New SqlCommand(sql, connection)
    Dim sqlReader As SqlDataReader = command.ExecuteReader()
    While sqlReader.Read()
        If (Password.Text = sqlReader("Password") And NET_ID.Text = sqlReader("NET_ID")) Then
            Response.Redirect("Creation.aspx")
        End If
    End While

    sqlReader.Close()
    command.Dispose()
    connection.Close()
Catch ex As Exception
    MsgBox("Can not open connection ! ")
End Try

End Sub

答案 2 :(得分:0)

Protected Sub login_btn_Click(sender As Object, e As EventArgs) Handles login_btn.Click

    Dim connString As String
    Dim connection As SqlConnection
    Dim command As SqlCommand
    Dim sql As String
    connString = "Data Source=.\SQLExpress;Initial Catalog=Suivi_Invst;Integrated Security=True"
//Instead of 
sql = "select NET_ID, Password from User"
//I used this
 sql = "SELECT [NET_ID], [Password] FROM [User] "

它有效

    connection = New SqlConnection(connString)
    Try
        connection.Open()
        command = New SqlCommand(sql, connection)
        Dim sqlReader As SqlDataReader = command.ExecuteReader()
        While sqlReader.Read()
            If (Password.Text = sqlReader("Password") And NET_ID.Text = sqlReader("NET_ID")) Then
                Response.Redirect("~/Request/Creation.aspx")
            End If
        End While
        sqlReader.Close()
        command.Dispose()
        connection.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

谢谢大家