C# 如何连接到 MDF 数据库文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8926512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I connect to an MDF database file?
提问by Ahmad
I'm experimenting in connecting a C# app to an MDF database for the first time, and I need a little help ..
我第一次尝试将 C# 应用程序连接到 MDF 数据库,我需要一些帮助..
I made a small MDF database file in Visual Studio 2010, then created another project and imported the file into the project itself.
我在 Visual Studio 2010 中创建了一个小的 MDF 数据库文件,然后创建了另一个项目并将该文件导入到项目本身中。
I am not trying to connect to the MDF file via code. Here the code I'm using:
我不想通过代码连接到 MDF 文件。这是我正在使用的代码:
namespace DBtestApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.Data.SqlClient.SqlConnection con;
private void Form1_Load(object sender, EventArgs e)
{
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = "DataSource=.\SQLEXPRESS; AttachDbFilename =SampleDatabase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
con.Open();
MessageBox.Show("Connection opened");
con.Close();
MessageBox.Show("Connection closed");
}
}
}
When I run the application, I get an exception at the line where I define the connection string, and the exception has this message at the top of the stack:
当我运行应用程序时,我在定义连接字符串的行处收到一个异常,并且该异常在堆栈顶部有以下消息:
System.ArgumentException: Keyword not supported: 'datasource'.
Can someone point me in the right direction ?
有人可以指出我正确的方向吗?
采纳答案by adatapost
Add space between Data Source
之间添加空格 Data Source
con.ConnectionString = @"Data Source=.\SQLEXPRESS;
AttachDbFilename=c:\folder\SampleDatabase.mdf;
Integrated Security=True;
Connect Timeout=30;
User Instance=True";
回答by John Woo
Server=.\SQLExpress;AttachDbFilename=c:\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
回答by user1943915
string sqlCon = @"Data Source=.\SQLEXPRESS;" +
@"AttachDbFilename=|DataDirectory|\SampleDB.mdf;
Integrated Security=True;
Connect Timeout=30;
User Instance=True";
SqlConnection Con = new SqlConnection(sqlCon);
The filepath should have |DataDirectory|which actually links to "current project directory\App_Data\" or "current project directory" and get the .mdf file.....Place the .mdf in either of these places and should work in visual studio 2010.And when you use the standalone application on production system, then the current path where the executable file is, should have the .mdf file.
文件路径应该有|DataDirectory| 它实际上链接到“当前项目目录\App_Data\”或“当前项目目录”并获取 .mdf 文件.....将 .mdf 放在这些位置中的任何一个,应该在 Visual Studio 2010 中工作。当你使用时生产系统上的独立应用程序,然后可执行文件所在的当前路径,应该有 .mdf 文件。
回答by Mike James
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Samples\MyApp\C#\bin\Debug\Login.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
this is working for me... Is there any way to short the path? like
这对我有用......有什么办法可以缩短路径吗?喜欢
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=\bin\Debug\Login.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
回答by Mahmmoud Qassem
Go to server explorer > Your Database > Right Click > properties > ConnectionString and copy the connection string and past the copied to connectiongstring code :)
转到服务器资源管理器 > 您的数据库 > 右键单击 > 属性 > ConnectionString 并复制连接字符串并粘贴复制到 connectiongstring 的代码:)
回答by July.Tech
For Visual Studio 2015 the connection string is:
对于 Visual Studio 2015,连接字符串是:
"Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True"
回答by f4d0
Alternative solution, where you can have the database in the folder you want inside the solution. That worked for me:
替代解决方案,您可以将数据库放在解决方案中所需的文件夹中。这对我有用:
.ConnectionString(@"Data Source=LocalDB)\MSSQLLocalDB;
AttachDbFilename="+AppDomain.CurrentDomain.BaseDirectory+"Folder1\Folder2\SampleDatabase.mdf" + ";
Integrated Security=True;")

