2014-01-23 692 views
0

這是我的代碼:C#錯誤:「填寫:SelectCommand.Connection屬性尚未初始化。」只是在PC上

SqlDataAdapter adapter = new SqlDataAdapter(query, conn); 

該程序正在所有其他電腦除了一個我我得到了這個錯誤:

System.InvalidOperationException: Fill: SelectCommand.Connection property has not been initialized. 

    at System.Data.Common.DbDataAdapter.GetConnection3(DbDataAdapter adapter, IDbCommand command, String method) 
    at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) 
    at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) 
    at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) 
    at service_managment.V.Fill_data_set(String query) 
    at service_managment.V.get_first_of_tabel(String query) 
    at service_managment.login.button1_Click(Object sender, EventArgs e) 
    at service_managment.login.tbx_password_KeyDown(Object sender, KeyEventArgs e) 
    at System.Windows.Forms.Control.OnKeyDown(KeyEventArgs e) 
    at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m) 
    at System.Windows.Forms.Control.ProcessKeyMessage(Message& m) 
    at System.Windows.Forms.Control.WmKeyChar(Message& m) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.TextBoxBase.WndProc(Message& m) 
    at System.Windows.Forms.TextBox.WndProc(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 

我該如何解決它?

+1

你是否在調用'new SqlDataAdapter(query,conn)'之前初始化'conn'? –

+2

您能否請您發佈完整的代碼以便我們理解問題? – Khan

+1

你沒有將*連接*分配給你的*命令*對象。發佈完整的代碼以指出問題。 – Zeeshan

回答

0

也許你定義一個SqlConnection這樣的:

SqlConnection conn = new SqlConnection(); 

但你並沒有設置它的ConnectionString property.Make務必將您ConnectionString正確那麼它應該工作。

1

指定連接至您的指令對象將解決該問題。確保您已將連接分配給命令。例如

SqlConnection conn = new SqlConnection("connectionString"); 
cmd.connection = conn; 

必須解決問題。或者,如果你定義了你的命令對象爲:

SqlCommand command = new SqlCommand(commandtext, conn); 

那麼在這種情況下,您的康涅狄格州對象未初始化。請務必先初始化它,如:

SqlConnection conn = new SqlConnection("connectionString"); 
0

如果相同的代碼工作在不同的機器,但不是你的機器上,則有以下posibilities:

  1. 你沒有服務器的訪問。
  2. 可能是您的連接字符串訪問您的機器上沒有的本地數據庫文件。
  3. 您忘記爲連接對象設置連接字符串。
  4. 您忘記爲您的命令對象設置連接。
  5. 在填充數據適配器之前,您尚未初始化連接對象。

變更您的代碼並嘗試找到問題。

相關問題