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:连接属性尚未初始化。

任何想法,我错过了什么?

您需要将连接分配给SqlCommand ,您可以使用构造函数或属性 :

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

我强烈build议使用using-statement来实现像SqlConnection这样的IDisposabletypes的任何types,它也会closures连接:

 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 } } 

除此之外,即使创build,打开和closures连接, 也不意味着ADO.NET将创build,打开和closures物理连接,而只是看起来像在每个条目中创build一个新的连接和DataAdapter 连接到连接池以获得可用的连接。 不过这是不必要的开销。

你并没有初始化连接,这就是为什么这种错误正在向你传递。

你的代码:

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

实际上这个错误发生在服务器build立连接时,但由于识别连接function标识符失败而无法build立。 这个问题可以通过在代码中键入连接函数来解决。 为此我举一个简单的例子。 在这种情况下function是con你可能会不同。

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

这里有一些错误。

  1. 你真的想打开和closures每个日志条目的连接吗?

  2. 你不应该使用SqlCommand而不是SqlDataAdapter

  3. 数据适配器(或SqlCommand )正好需要错误消息告诉你缺less的东西:活动连接。 仅仅因为你创build了一个连接对象,并不奇怪地告诉C#它是你想要使用的(特别是如果你还没有打开连接的话)。

我强烈推荐一个C#/ SQL Server教程。

打开和closures连接需要很长时间。 并使用“使用”作为另一个成员build议。 我稍微改了一下你的代码,但是把SQL的创build和打开和closures都放到你的循环之外。 这应该加快执行一下。

  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 } 

试试这个..

您需要在执行ExecuteNonQuery()之前使用SqlCommand.Connection对象上的connection.open()打开连接,

双击你的表单来创buildform_load事件。然后在这个事件里写command.connection =“你的连接名”;