2015-06-08 27 views
0

編譯器給我這個錯誤:附加的autonamed數據庫文件失敗

An attempt to attach an auto-named database for file failed. A database with the same name exists or specified file cannot be open, or it is located on UNC share.

我一直在閱讀上的其他計算器職位,他們提到這可能是一個問題的ConnectionString,但我m在linq中編碼,而不是ado.net代碼。我不使用傳統的連接字符串。事實上,我必須使用名稱空間來調用tableAdapter,以便我訪問我需要的數據源。

正在使用的代碼如下:

using CustomerInvoices.MMABooksDataSetTableAdapters; 


namespace CustomerInvoices 
{ 


    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     MMABooksDataSet mmaBooksDataSet = new MMABooksDataSet(); 
     InvoicesTableAdapter invoicesTableAdapter = new InvoicesTableAdapter(); 
     CustomersTableAdapter customersTableAdapter = 
     new CustomersTableAdapter(); 


     private void Form1_Load(object sender, EventArgs e) 
     { 
      invoicesTableAdapter.Fill(mmaBooksDataSet.Invoices); 
      customersTableAdapter.Fill(mmaBooksDataSet.Customers); 

      var invoices = from invoice in mmaBooksDataSet.Invoices 
          join customer in mmaBooksDataSet.Customers 
          on invoice.CustomerID equals customer.CustomerID 
          orderby customer.Name, invoice.InvoiceTotal descending 
          select new 
          { 
           customer.Name, 
           invoice.InvoiceID, 
           invoice.InvoiceDate, 
           invoice.InvoiceTotal 
          }; 

      string customerName = ""; 
      int i = 0; 
      foreach (var invoice in invoices) 
      { 
       if (invoice.Name != customerName) 
       { 
        lvInvoices.Items.Add(invoice.Name); 
        customerName = invoice.Name; 
       } 
       else 
       { 
        lvInvoices.Items.Add(""); 
       } 

       lvInvoices.Items[i].SubItems.Add(invoice.InvoiceTotal.ToString()); 

       lvInvoices.Items[i].SubItems.Add(Convert.ToDateTime 
       (invoice.InvoiceDate).ToShortDateString()); 
       lvInvoices.Items[i].SubItems.Add 
       (invoice.InvoiceTotal.ToString("c")); 
       i += 1; 
      } 


     } 
    } 
} 

回答

1

理解了它。直通的數據源,我走進了連接屬性設置窗口,找到了連接路徑的文件,我把它改成正確的

Data Source=localhost\sqlexpress;Initial Catalog=MMABooks;Integrated Security=True 

和它的工作。看來錯誤是因爲connectionString指向錯誤的路徑文件。希望這有助於他人。謝謝。