下午,所以我在這個問題上待了好幾個小時,無法真正克服最后一個困難。以下是我正在編寫的該程序的代碼: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:連接屬性尚未初始化。關于我錯過的任何想法?
ExecuteNonQuery:連接屬性尚未初始化。
一只名叫tom的貓
2019-10-12 10:49:13