我试图通过基本的Linq,并有一个问题,简单地连接到提供的.mdf文件。
我在C:\DATA\NORTHWND.MDF保存了这个文件。我正在使用2013
代码如下所示:
using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.IO;
namespace LinqToSqlWithoutDesigner
{
[Table(Name="Customers")]
class Customer
{
[Column]
public string CustomerID;
[Column]
public string City;
}
class Program
{
static void Main(string[] args)
{
DataContext db = new DataContext(@"C:\DATA\NORTHWND.MDF");
var query = from c in db.GetTable<Customer>()
where c.City == "London"
select new { CustId = c.CustomerID, City = c.City };
foreach(var cust in query)
{
Console.WriteLine(cust);
}
}
}
}我还试图使用:
string connectionString = @"Data Source=.\SQLEXPRESS;
AttachDbFilename=c:\data\northwind.mdf;
Integrated Security=True;
Connect Timeout=30;
User Instance=True";
DataContext db = new DataContext(connectionString); 但我总是会犯错误:
未处理的异常: System.Data.SqlClient.SqlException:在建立到Server的连接时发生了与网络相关或特定于实例的错误。找不到或无法访问服务器。验证实例名是否正确,以及Server是否配置为允许远程连接。(提供程序:SQL网络接口,错误: 26 -指定的服务器/实例定位错误)
发布于 2015-03-03 16:47:01
这是一个字符串,它允许我连接到MDF文件
DataContext db = new DataContext(@"Data Source=(localdb)\v11.0;
Integrated Security=true;
AttachDbFileName=C:\DATA\NORTHWND.MDF");有关LocalDB的更多信息,请访问:http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx
发布于 2015-03-03 16:18:32
看起来您的计算机上没有,或者请求的网络接口协议没有激活。
对于.mdf文件,需要启动并运行Server
发布于 2015-03-03 16:27:01
连接字符串中不存在目录名称。
添加到connectionString中:
catalog=yourDbName注意:您不能直接连接到MDF文件。
如果数据库服务器中不存在数据库,则需要使用.mdf文件和.ldf文件。
然后使用连接字符串并添加我提到的目录名称。
https://stackoverflow.com/questions/28836591
复制相似问题