2011-10-30 21 views
-1

此函數將顯示「連接未正確初始化」錯誤。爲什麼?執行非查詢:正確連接尚未初始化

public void updateCustomer() 
{ 
    using (SqlCeConnection aConnection = new SqlCeConnection(@"Data Source=|DataDirectory|\Database.sdf")) 
    { 
      aConnection.Open(); 
      using (SqlCeCommand aCommand = new SqlCeCommand("UPDATE customer SET credit = @credit WHERE(ID = @ID)")) 
      { 
       aCommand.Parameters.Add(new SqlCeParameter("credit", SqlDbType.NVarChar, 100)).Value = getRemaining(); 
       aCommand.Parameters.Add(new SqlCeParameter("ID", SqlDbType.Int, 8)).Value = Convert.ToInt32(textBox2.Text); 

       int resultUpdate = aCommand.ExecuteNonQuery(); 
       if (resultUpdate != -1) 
        label9.Text = "customer updated sussessfully"; 
       else 
        label9.Text = "some error in updating customer"; 
       aConnection.Close(); 
      } 
    } 
} 

回答

3

需要設置爲

@"Data Source=|DataDirectory|\Database.sdf;Persist Security Info=False;" 

檢查Connection strings for SQL Server Compact Edition的SqlCeCommand

SqlCeCommand command = new SqlCeCommand(
    queryString, connection); 

集連接字符串連接以獲取更多信息

-1

我忘記了這事unhandledexception

許多倍的.rememeber,只要你每次使用的SqlCommand就是了CON作爲參數...

以其他方式使用命名SqlDataAdapter的更先進的工具...