2012-04-21 291 views
8

下午, 因此,我一直在這個問題上好幾個小時,並沒有真正擺脫這最後的坎坷。下面是這個程序,我寫的代碼:ExecuteNonQuery:連接屬性尚未初始化。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Diagnostics; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 

namespace Test 
{ 
    class Program 
    { 
    static void Main() 
    { 
     EventLog alog = new EventLog(); 
     alog.Log = "Application"; 
     alog.MachineName = "."; 
     foreach (EventLogEntry entry in alog.Entries) 
     { 
     SqlConnection connection1 = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=syslog2;Integrated Security=True"); 
     SqlDataAdapter cmd = new SqlDataAdapter(); 
     cmd.InsertCommand = new SqlCommand("INSERT INTO Application VALUES (@EventLog, @TimeGenerated, @EventType, @SourceName, @ComputerName, @InstanceId, @Message) "); 
     cmd.InsertCommand.Parameters.Add("@EventLog",SqlDbType.VarChar).Value = alog.Log; 
     cmd.InsertCommand.Parameters.Add("@TimeGenerated", SqlDbType.DateTime).Value = entry.TimeGenerated; 
     cmd.InsertCommand.Parameters.Add("@EventType", SqlDbType.VarChar).Value = entry.EntryType; 
     cmd.InsertCommand.Parameters.Add("@SourceName", SqlDbType.VarChar).Value = entry.Source; 
     cmd.InsertCommand.Parameters.Add("@ComputerName", SqlDbType.VarChar).Value = entry.MachineName; 
     cmd.InsertCommand.Parameters.Add("@InstanceId", SqlDbType.VarChar).Value = entry.InstanceId; 
     cmd.InsertCommand.Parameters.Add("@Message", SqlDbType.VarChar).Value = entry.Message; 
     connection1.Open(); 
     cmd.InsertCommand.ExecuteNonQuery(); 
     connection1.Close(); 
     } 
    } 
    } 
} 

代碼編譯沒有錯誤或警告罰款,但是當我去,因爲它得到cmd.InsertCommand.ExecuteNonQuery儘快運行它,();我收到以下錯誤:

ExecuteNonQuery: Connection property has not been initialized.

對我錯過了什麼的任何想法?

+1

cmd.InsertCommand.Connection = connection1; – Alan 2012-04-21 21:23:47

+0

(順便說一句,爲每個日誌條目打開一個新的連接是一個禁忌。) – Alan 2012-04-21 21:25:27

回答

24

您需要分配到SqlCommand的連接,你可以使用constructorproperty

cmd.InsertCommand = new SqlCommand("INSERT INTO Application VALUES (@EventLog, @TimeGenerated, @EventType, @SourceName, @ComputerName, @InstanceId, @Message) "); 
cmd.InsertCommand.Connection = connection1; 

我強烈建議使用using-statement任何類型實現IDisposableSqlConnection,它也將關閉連接:

using(var connection1 = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=syslog2;Integrated Security=True")) 
{ 
    SqlDataAdapter cmd = new SqlDataAdapter(); 
    using(var insertCommand = new SqlCommand("INSERT INTO Application VALUES (@EventLog, @TimeGenerated, @EventType, @SourceName, @ComputerName, @InstanceId, @Message) ")) 
    { 
     insertCommand.Connection = connection1; 
     cmd.InsertCommand = insertCommand; 
     //..... 
     connection1.Open(); 
     // .... you don't need to close the connection explicitely 
    } 
} 

除此之外,您不需要創建在每個條目一個新的連接和,即使創建,打開和關閉連接,不是意味着ADO.NET將創建,打開和關閉物理連接,但只是查看connection-pool以獲取可用連接。儘管如此,這是不必要的開銷。

1

這裏有一些錯誤。

  1. 你真的想打開和關閉每個日誌條目的連接嗎?

  2. 您不應該使用SqlCommand而不是SqlDataAdapter

  3. 數據適配器(或SqlCommand)正好需要錯誤消息告訴您缺少的內容:活動連接。僅僅因爲你創建了一個連接對象並不奇怪地告訴C#它是你想使用的那個(特別是如果你還沒有打開連接的話)。

我強烈推薦C#/ SQL Server教程。

-1

只是嘗試這個..

你需要執行ExecuteNonQuery()

8

您還沒有初始化connection.That的,爲什麼這種錯誤即將在你面前打開SqlCommand.Connection對象上使用connection.open()的連接。

您的代碼:

cmd.InsertCommand = new SqlCommand("INSERT INTO Application VALUES (@EventLog, @TimeGenerated, @EventType, @SourceName, @ComputerName, @InstanceId, @Message) "); 

更正代碼:

cmd.InsertCommand = new SqlCommand("INSERT INTO Application VALUES (@EventLog, @TimeGenerated, @EventType, @SourceName, @ComputerName, @InstanceId, @Message) ",connection1); 
1

當服務器進行連接,但可以因故障識別連接功能標識符不建立實際上發生此錯誤。這個問題可以通過在代碼中鍵入連接函數來解決。爲此我舉一個簡單的例子。在這種情況下,函數是con,你可能會有所不同。

SqlCommand cmd = new SqlCommand("insert into ptb(pword,rpword) values(@a,@b)",con); 
-1

表單上雙擊創建活動寫入command.connection =「你的連接名」裏面的Form_Load event.Then;

0

打開和關閉連接需要花費大量的時間。並使用「使用」作爲另一個成員建議。 我稍微改了一下你的代碼,但是把SQL的創建和打開和關閉放到你的循環之外。這應該加快執行一點。

static void Main() 
     { 
      EventLog alog = new EventLog(); 
      alog.Log = "Application"; 
      alog.MachineName = "."; 
      /* ALSO: USE the USING Statement as another member suggested 
      using (SqlConnection connection1 = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=syslog2;Integrated Security=True") 
      { 

       using (SqlCommand comm = new SqlCommand("INSERT INTO Application VALUES (@EventLog, @TimeGenerated, @EventType, @SourceName, @ComputerName, @InstanceId, @Message) ", connection1)) 
       { 
        // add the code in here 
        // AND REMEMBER: connection1.Open(); 

       } 
      }*/ 
      SqlConnection connection1 = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=syslog2;Integrated Security=True"); 
      SqlDataAdapter cmd = new SqlDataAdapter(); 
      // Do it one line 
      cmd.InsertCommand = new SqlCommand("INSERT INTO Application VALUES (@EventLog, @TimeGenerated, @EventType, @SourceName, @ComputerName, @InstanceId, @Message) ", connection1); 
      // OR YOU CAN DO IN SEPARATE LINE : 
      // cmd.InsertCommand.Connection = connection1; 
      connection1.Open(); 

      // CREATE YOUR SQLCONNECTION ETC OUTSIDE YOUR FOREACH LOOP 
      foreach (EventLogEntry entry in alog.Entries) 
      { 
       cmd.InsertCommand.Parameters.Add("@EventLog", SqlDbType.VarChar).Value = alog.Log; 
       cmd.InsertCommand.Parameters.Add("@TimeGenerated", SqlDbType.DateTime).Value = entry.TimeGenerated; 
       cmd.InsertCommand.Parameters.Add("@EventType", SqlDbType.VarChar).Value = entry.EntryType; 
       cmd.InsertCommand.Parameters.Add("@SourceName", SqlDbType.VarChar).Value = entry.Source; 
       cmd.InsertCommand.Parameters.Add("@ComputerName", SqlDbType.VarChar).Value = entry.MachineName; 
       cmd.InsertCommand.Parameters.Add("@InstanceId", SqlDbType.VarChar).Value = entry.InstanceId; 
       cmd.InsertCommand.Parameters.Add("@Message", SqlDbType.VarChar).Value = entry.Message; 
       int rowsAffected = cmd.InsertCommand.ExecuteNonQuery(); 
      } 
      connection1.Close(); // AND CLOSE IT ONCE, AFTER THE LOOP 
     } 
相關問題