0

我正在尝试将我的网络应用程序中的一些数据插入到数据库表中。每当我运行代码时,它都会直接跳转到异常。即使我设置了断点,调试也不会停止以便我检查参数。我什至通过手动将相同的数据插入表及其工作来检查表是否会接受从我的 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()

4

3 回答 3

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
于 2012-11-29T11:00:20.743 回答
0

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

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

在 MSDN上查看有关 Integrated Security 的文档

如果为 false,则在连接中指定用户 ID 和密码。如果为 true,则使用当前 Windows 帐户凭据进行身份验证。如果指定了用户 ID 和密码并且集成安全设置为 true,则用户 ID 和密码将被忽略并使用集成安全。

于 2012-11-29T11:37:43.497 回答
0

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

于 2012-11-29T17:29:35.620 回答