的ExecuteNonQuery:Connection属性尚未初始化。 [英] ExecuteNonQuery: Connection property has not been initialized.

查看:182
本文介绍了的ExecuteNonQuery:Connection属性尚未初始化。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午,
         所以,我一直在这一个问题上几个小时,并不能真正突破这个最后的驼背。下面是code这个程序,我写:

Afternoon, So I have been at this one issue for hours and can't really get past this last hump. Below is the code for this program that I am writing:

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

在code编译罚款没有错误或警告,但是当我去,因为它得到cmd.InsertCommand.ExecuteNonQuery尽快运行它,();我收到以下错误:

The Code compiles fine without error or warning but when i go to run it, as soon as it gets to cmd.InsertCommand.ExecuteNonQuery(); I get the following error:

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

ExecuteNonQuery: Connection property has not been initialized.

什么我错过任何想法?

推荐答案

您需要分配到的SqlCommand ,您可以使用构造或<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.connection.aspx\">property:

You need to assign the connection to the SqlCommand, you can use the constructor or the property:

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

我强烈建议使用 使用语句 任何类型的实施了IDisposable 的SqlConnection ,系统也将关闭连接:

I strongly recommend to use the using-statement for any type implementing IDisposable like SqlConnection, it'll also close the connection:

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

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

Apart from that you don't need to create a new connection and DataAdapter for every entry in the foreach, even if creating, opening and closing a connection does not mean that ADO.NET will create, open and close a physical connection but just looks into the connection-pool for an available connection. Nevertheless it's an unnecessary overhead.

这篇关于的ExecuteNonQuery:Connection属性尚未初始化。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆