第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ExecuteNonQuery:連接屬性尚未初始化。

ExecuteNonQuery:連接屬性尚未初始化。

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

3 回答

?
MMTTMM

TA貢獻1869條經(jīng)驗 獲得超4個贊

您不是在初始化連接,這就是為什么出現(xiàn)這種錯誤的原因。


您的代碼:


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


查看完整回答
反對 回復(fù) 2019-10-12
?
墨色風(fēng)雨

TA貢獻1853條經(jīng)驗 獲得超6個贊

這里有幾處錯誤。

  1. 您是否真的要為每個日志條目打開和關(guān)閉連接?

  2. 您不應(yīng)該使用SqlCommand代替SqlDataAdapter嗎?

  3. 數(shù)據(jù)適配器(或SqlCommand)完全需要錯誤消息告訴您丟失的消息:活動連接。僅僅因為您創(chuàng)建了一個連接對象,并不能神奇地告訴C#它是您要使用的對象(尤其是如果您尚未打開連接)。


查看完整回答
反對 回復(fù) 2019-10-12
  • 3 回答
  • 0 關(guān)注
  • 755 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號