2011-04-30 341 views
2

第一次我正在從ASP.NET/C#執行插入操作,並遇到一些小問題。每次運行此代碼時,我都會收到以下錯誤:「ExecuteNonQuery:CommandText屬性尚未初始化」有誰知道這意味着什麼,以及我如何修復它?C#數據庫插入(ASP.NET) - ExecuteNonQuery:CommandText屬性尚未初始化

在此先感謝!

string sqlQuery = "INSERT INTO ATI_LOG_IO (Date, Connect_Time, Disconnect_Time, ATI_Rep, Reason_For_Access, Property_Contact, Case_Number, Comments, Property_ID)"; 
sqlQuery += "VALUES (@Today, @Connect, @Disconnect, @Rep, @Reason, @Contact, @CaseNum, @Comments, @PropertyID)"; 
using (SqlConnection dataConnection = new SqlConnection(connectionString)) 
{ 
    using (SqlCommand dataCommand = dataConnection.CreateCommand()) 
    { 
     dataConnection.Open(); 
     dataCommand.CommandType = CommandType.Text; 
     dataCommand.CommandText = sqlQuery; 
     dataCommand.Parameters.Add("@Today", DateTime.Today.ToString()); 
     dataCommand.Parameters.Add("@Connect", txtInDate.Text + " " + fromHrs.Text + ":" + fromMins.Text + ":00"); 
     dataCommand.Parameters.Add("@Disconnect", txtOutdate.Text + " " + toHrs.Text + ":" + fromMins.Text + ":00"); 
     dataCommand.Parameters.Add("@Rep", repID); 
     dataCommand.Parameters.Add("@Reason", txtReason.Text); 
     dataCommand.Parameters.Add("@Contact", txtContact.Text); 
     dataCommand.Parameters.Add("@CaseNum", txtCaseNum.Text); 
     dataCommand.Parameters.Add("@Comments", txtComments.Text); 
     dataCommand.Parameters.Add("@PropertyID", lstProperties.SelectedValue); 
     dataCommand.ExecuteNonQuery(); 
     dataConnection.Close(); 
    } 
} 
+0

'SqlParameterCollection.Add方法(String,對象)'是過時的。 http://msdn.microsoft.com/en-us/library/9dd8zze1%28v=vs.80%29.aspx。試試這些:使用'@' - >'Parameters.Add(SqlParameter)','Parameters.AddRange(SqlParameter [])'或者沒有'@' - >'Parameters.AddWithValue(String,Object)' – Jaider 2013-01-02 21:23:53

回答

6
string sqlQuery = "INSERT INTO ATI_LOG_IO (Date, Connect_Time, Disconnect_Time, ATI_Rep, Reason_For_Access, Property_Contact, Case_Number, Comments, Property_ID)"; 
sqlQuery += " VALUES (@Today, @Connect, @Disconnect, @Rep, @Reason, @Contact, @CaseNum, @Comments, @PropertyID)"; 
using (SqlConnection dataConnection = new SqlConnection(connectionString)) 
{ 
    using (SqlCommand dataCommand = new SqlCommand(sqlQuery, dataConnection)) 
    { 
     dataCommand.Parameters.AddWithValue("Today", DateTime.Today.ToString()); 
     dataCommand.Parameters.AddWithValue("Connect", txtInDate.Text + " " + fromHrs.Text + ":" + fromMins.Text + ":00"); 
     dataCommand.Parameters.AddWithValue("Disconnect", txtOutdate.Text + " " + toHrs.Text + ":" + fromMins.Text + ":00"); 
     dataCommand.Parameters.AddWithValue("Rep", repID); 
     dataCommand.Parameters.AddWithValue("Reason", txtReason.Text); 
     dataCommand.Parameters.AddWithValue("Contact", txtContact.Text); 
     dataCommand.Parameters.AddWithValue("CaseNum", txtCaseNum.Text); 
     dataCommand.Parameters.AddWithValue("Comments", txtComments.Text); 
     dataCommand.Parameters.AddWithValue("PropertyID", lstProperties.SelectedValue); 

     dataConnection.Open(); 
     dataCommand.ExecuteNonQuery(); 
     dataConnection.Close(); 
    } 
} 

複製粘貼應該做的伎倆

+0

因此,從它的外觀你已經修復了我的代碼與上述建議,我得到了很多,但(對不起,如果這是一個noob問題)Parameters.Add和參數之間有什麼區別。 AddWithValue - 這是我的原始@sa偏好的結果的遺漏還是我錯誤地使用它們在第一個地方? – 2011-05-01 23:11:31

+0

我只使用傳入參數presenstantiated的方法的Parameters.Add(SqlParameter)重載。您使用的重載目前已被Microsoft過時。過去我從來沒有成功使用它。至於@符號,在VB中,我必須使用@符號指定參數名稱,當使用AddWithValue方法時,但在C#中它不是必需的,但我認爲仍然可以使用具有參數名稱的@符號得到相同的結果。如果你想使用Parameters.Add,使用Parameters.Add(String Name,SqlDbType Type)重載,然後以參數[「@ Name」]的形式傳入值。Value = Value – Dimitri 2011-05-02 14:24:10

+0

似乎已經處理了它。現在我得到了關於沒有INSERT權限的錯誤,這是我自己的愚蠢錯誤,至少我知道如何解決這個問題:) – 2011-05-02 15:48:46

1

這通常意味着你沒有設置CommandText屬性,但在你的情況下,你有。

你應該嘗試測試該字符串的SQLQuery其實並不是在這一行空:

dataCommand.CommandText = sqlQuery; 

附:作爲一個「最佳實踐」,你可能要考慮開盤後建立SqlCommand對象的連接,以儘量減少打開的連接所花費的時間:

dataCommand.CommandType = CommandType.Text; 
    dataCommand.CommandText = sqlQuery; 
    dataCommand.Parameters.Add("@Today", DateTime.Today.ToString()); 
    //... 
    dataConnection.Open(); 
    dataCommand.ExecuteNonQuery(); 
    dataConnection.Close(); 
+0

嘗試在設置對象之前和之後打開它,以防萬一與錯誤有關,忘記在發佈代碼之前將其移回。感謝提示雖然:) – 2011-05-01 23:07:47

1

看着你的字符串sql查詢,你不會在「INTO」部分和「VALUES」部分之間留出一個空格。

...............Property_ID)"; 
sqlQuery += "VALUES (@Today, .............. 

應該是:

...............Property_ID)"; 
sqlQuery += " VALUES (@Today, .............. 
+0

哦,親愛的,那肯定會導致一些問題。任何想法,如果它會導致這個具體問題? dataCommand是否在發送之前實際解析SQL,或者只是在我修復* this *錯誤的原因後返回SQL錯誤? – 2011-05-01 23:09:20

相關問題