2012-11-29 130 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

有同樣的問題。在解決方案資源管理器中右鍵單擊解決方案並選擇清理然後嘗試再次運行中斷點。

相關問題