2012-11-29 168 views
0

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

这里是我的代码

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()

+1

哪个例外? –

+0

在什么时候引发异常?什么是例外? – Ric

+0

使用消息框而不告诉至少是异常消息的是什么?将其更改为'MsgBox(“Data error:”+ ex.Message)'并发布相关消息 – Steve

回答

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 
+0

虽然你是对的,但我认为这不是例外的原因。 – Steve

+0

我想,错误应该在sql连接字符串中。尝试在try块内声明conn。 –

+0

@MohitPandey是否仍然如此。 – HShbib

0

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

我怀疑SqlServer不能识别用户Server,因此你得到一个失败的连接异常。你可以尝试从您的连接字符串中删除集成安全性=真,但是这意味着用户ID和密码是有效的服务器计算机

见文档上Integrated Security here at MSDN

When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication. If User ID and Password are specified and Integrated Security is set to true, the User ID and Password will be ignored and Integrated Security will be used.

0

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

相关问题