的ExecuteNonQuery:Connection属性尚未初始化。的ExecuteNonQuer

2019-05-13 14:22发布

下午,所以我一直在这一个问题了好几个小时,并不能真正闯过这最后的驼峰。 下面是这个程序,我写的代码:

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

什么我错过任何想法?

Answer 1:

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

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

我强烈建议使用using-statement用于任何类型实现IDisposable喜欢SqlConnection ,它也将关闭连接:

using(var connection1 = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=syslog2;Integrated Security=True"))
using(var 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
}

除此之外,你并不需要创建一个新的连接和DataAdapter在每个条目foreach ,即使创建,打开和关闭连接并不意味着ADO.NET将创建,打开和关闭物理连接,但只是看起来进入连接池的可用连接。 尽管如此,它是一个不必要的开销。



Answer 2:

您还没有初始化connection.That就是为什么这种类型的错误是向你走来。

您的代码:

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


Answer 3:

几件事情错在这里。

  1. 你真的要打开和关闭为每一个日志条目的连接?

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

  3. 数据适配器(或SqlCommand )需要什么错误信息告诉你它的丢失:活动连接。 仅仅因为你创建一个连接对象不会奇迹般地告诉C#,这是您要使用(尤其是如果你还没有打开连接)之一。

我强烈推荐一个C#/ SQL服务器的教程。



Answer 4:

当服务器进行连接,但不能建立因故障识别连接功能标识符实际发生该错误。 这个问题可以通过在代码中输入连接功能来解决。 为此,我举一个简单的例子。 在这种情况下,功能为CON你可能会有所不同。

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


Answer 5:

打开和关闭连接需要花费大量的时间。 并使用“使用”的另一位成员建议。 我稍微改变你的代码,而是把SQL创建和打开和关闭OUTSIDE你的循环。 应加快执行的位。

  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
        }


Answer 6:

只是尝试这个..

你需要使用打开连接connection.open()SqlCommand.Connection对象执行前ExecuteNonQuery()



Answer 7:

在窗体上双击创建活动写入command.connection =“你的连接名”里面的Form_Load event.Then;



文章来源: ExecuteNonQuery: Connection property has not been initialized.