ExecuteNonQuery: propriedade de conexão não foi inicializada.
Tarde, Então eu tenho estado nessa questão por horas e não posso realmente superar essa última corcunda. Abaixo está o código para este programa que estou escrevendo:
<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>
O código compila bem sem erro ou aviso, mas quando eu vou executá-lo, assim que chega ao cmd.InsertCommand.ExecuteNonQuery (); Estou tendo o erro a seguir:
ExecuteNonQuery: propriedade de conexão não foi inicializada.
Alguma idéia sobre o que eu perdi?