2012-04-21 289 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 
     } 
相关问题