ExecuteNonQuery: la propiedad de conexión no se ha inicializado.
Por la tarde, así que he estado en este único problema durante horas y realmente no puedo superar esta última joroba. A continuación se muestra el código para este programa que estoy escribiendo:
<code>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>
El Código se compila bien sin error ni advertencia, pero cuando voy a ejecutarlo, apenas llega a cmd.InsertCommand.ExecuteNonQuery (); Obtuve el siguiente error:
ExecuteNonQuery: la propiedad de conexión no se ha inicializado.
¿Alguna idea sobre lo que me perdí?