编译器给我这个错误:
为文件附加自动命名数据库的尝试失败。存在同名的数据库或无法打开指定的文件,或者该数据库位于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;
}
}
}
}
我明白了。通过数据源,我进入连接属性窗口并找到连接路径文件,我将其更改为正确的
Data Source=localhostsqlexpress;Initial Catalog=MMABooks;Integrated Security=True
它奏效了。似乎错误是因为connectionString指向错误的路径文件。希望这对其他人有所帮助。谢谢你!