2012-02-14 411 views
0

我已經做了幾個小時的研究,但似乎沒有什麼適用於我目前的情況。ExecuteReader:CommandText屬性尚未初始化

使用:Visual Studio 2010的.NET 4.0 語言:C#

問題:

我創建了一個表格,然後將它從我的數據源下降的特定表到形式有VS2010創建數據我的桌子。 當我嘗試使用數據表更改或向數據庫添加值時,出現上面列出的錯誤「ExecuteReader:CommandText屬性尚未初始化」。

從我以前的研究,不,我沒有定義CommandText,也不知道在哪裏創建一個考慮VS2010創建所有數據表的代碼,並沒有列出.cs文件本身。

感謝您的幫助!

代碼從形式擊中F7

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
namespace mineral_monitor.Manual_edits 
{ 
    public partial class mineral_stock : Form 
    { 

    public mineral_stock() 
    { 
     InitializeComponent(); 
    } 

    private void mineralsBindingNavigatorSaveItem_Click(object sender, EventArgs e) 
    { 
     this.Validate(); 
     this.mineralsBindingSource.EndEdit(); 
     this.tableAdapterManager.UpdateAll(this.ore_stockDataSet1); 

    } 

    private void mineral_stock_Load(object sender, EventArgs e) 
    { 
     // TODO: This line of code loads data into the 'ore_stockDataSet1.minerals' table. You can move, or remove it, as needed. 
     this.mineralsTableAdapter.Fill(this.ore_stockDataSet1.minerals); 

    } 

    } 
} 

此獲得的是通過在數據設計手動創建一個更新串解決。

+0

CommandText = ????請發佈您的代碼.. CommandType = ??? – MethodMan 2012-02-14 15:09:09

+0

將命令文本傳遞給命令對象是適配器的責任。因此,您可能無法使用適配器中的「SelectCommand」。 – 2012-02-14 15:49:16

+0

@WiktorZychla - 當我在數據設計器中查看TableAdapter屬性時,它確實聲明它具有選擇,插入和更新命令。 – Sirusx69 2012-02-14 15:52:21

回答

0

當您創建數據源時,VisualStudio會自動爲您創建選擇命令,但更新,插入和刪除命令不會自動創建。你將不得不手動指定它們或使用SqlCommandBuilder。查看this鏈接。

+0

我接受了您的建議並查看了MSDN網站。我做了一些搞亂,現在我得到「更新需要一個有效的UpdateCommand」,這是一個非常簡單的錯誤。經過我的研究,儘管每個人都說CommandString builer不能建立東西來更新ALL到表格。如何讓保存按鈕更新通過DataGridViewer所做的所有更改? – Sirusx69 2012-02-14 16:15:13

+0

如果您沒有單個表格,但是有一些Join或某物,則必須手動指定DataAdapter的UpdateCommand。在其他情況下,SqlCommandBuilder應該爲你自動生成更新命令。看看這個鏈接:http://stackoverflow.com/questions/588361/update-requires-a-valid-updatecommand-when-passed-datarow-collection-with-modifi – 2012-02-14 16:20:57

+0

它是一個單一的表。正如我所說的,我所做的只是從數據源窗口拖放到Windows窗體上,並自動設計了datagridview和修改按鈕(打開/保存等)。我也不太瞭解你與我聯繫的頁面。我已經有一個數據適配器作爲mineralsTableAdapter,但是當運行「mineralsTableAdapter.Fill(this.ore_stockDataSet1.minerals)」時,它仍會返回我沒有設置「更新命令」的情況。 – Sirusx69 2012-02-14 18:15:58

相關問題