将ASP.NET应用程序连接到SQL数据库问题

时间:2012-11-29 10:55:46

标签: asp.net sql-server vb.net

我正在尝试将我的网络应用中的一些数据插入到数据库表中。每当我运行代码时,它都会直接跳转到异常。即使我设置断点,调试也不会停止,以便我检查参数。我甚至检查了表是否接受从我的Web应用程序插入的数据类型,方法是将相同的数据手动插入表中并使其正常工作。

这是我的代码

 Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim IP As String = TextBox3.Text
        Dim Country_Code As String = TextBox4.Text
        Dim Country As String = TextBox5.Text

        Dim conn As New SqlConnection("Data Source=***.***.***.***;Initial Catalog=IP_Loc;User ID=********;Password=************;Integrated Security=True")
        Dim cmd As New SqlCommand

        Try
            conn.Open()
            cmd = New SqlCommand("INSERT INTO IP_Info(IP, Country_Code, Country) VALUES (@IP, @Country_Code, @Country)", conn)
            cmd.Parameters.AddWithValue("@IP", IP)
            cmd.Parameters.AddWithValue("@Country_Code", Country_Code)
            cmd.Parameters.AddWithValue("@Country", Country)
            cmd.ExecuteNonQuery()
            conn.Dispose()
            conn.Close()

        Catch ex As Exception
            MsgBox("Database Connection Error")
        End Try

我已经覆盖了用户ID和密码,因为它是本地服务器。有关如何解决这个问题的任何建议?

以下是conn.open()

时出现的错误

System.Data.SqlClient.SqlException: Login failed for user 'Server'. at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject) at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart) at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open()

3 个答案:

答案 0 :(得分:0)

你已经两次声明了sqlcommand。更正的计划

 Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

            Dim IP As String = TextBox3.Text
            Dim Country_Code As String = TextBox4.Text
            Dim Country As String = TextBox5.Text             
                Try
        Dim conn As New SqlConnection("Data Source=***.***.***.***;Initial   Catalog=IP_Loc;User ID=********;Password=************;Integrated Security=True")
                conn.Open()
            Dim cmd As New SqlCommand("INSERT INTO IP_Info(IP, Country_Code, Country) VALUES (@IP, @Country_Code, @Country)", conn)
                cmd.Parameters.AddWithValue("@IP", IP)
                cmd.Parameters.AddWithValue("@Country_Code", Country_Code)
                cmd.Parameters.AddWithValue("@Country", Country)
                cmd.ExecuteNonQuery()
                conn.Dispose()
                conn.Close()

            Catch ex As Exception
                MsgBox("Database Connection Error")
            End Try

答案 1 :(得分:0)

您需要在服务器计算机上拥有一个有效的用户帐户才能使用该连接字符串。 您指定Integrated Security=True,这意味着尝试连接到指定IP地址的SqlServer的Windows当前用户(server)也应该是该计算机的有效Windows帐户。

我怀疑SqlServer无法识别用户Server,因此您会收到失败的连接异常。您可以尝试从连接字符串中删除Integrated Security = True,但这意味着用户ID和密码对服务器计算机有效

请参阅Integrated Security here at MSDN

上的文档
  

如果为false,则在连接中指定用户ID和密码。什么时候   是的,当前的Windows帐户凭据用于   认证。如果指定了用户ID和密码并已集成   安全性设置为true,将忽略用户ID和密码   将使用集成安全性。

答案 2 :(得分:0)

有同样的问题。右键单击解决方案资源管理器中的解决方案并选择clean。然后尝试再次使用断点运行。