尝试为文件附加自动命名的数据库失败

时间:2015-06-08 03:12:29

标签: c# linq

编译器给我这个错误:

  

尝试为文件附加自动命名的数据库失败。存在具有相同名称的数据库或指定的文件无法打开,或者它位于UNC共享上。

我一直在阅读其他stackoverflow帖子,他们提到它可能是一个连接字符串问题,但我在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 个答案:

答案 0 :(得分:1)

我明白了。通过数据源,我进入连接属性窗口,找到连接路径文件,我将其更改为正确的

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

它工作了。似乎错误是因为connectionString指向错误的路径文件。希望这有助于其他人。谢谢。

相关问题