如何连接到 MDF 数据库文件? [英] How do I connect to an MDF database file?
问题描述
我第一次尝试将 C# 应用程序连接到 MDF 数据库,我需要一些帮助..
I'm experimenting in connecting a C# app to an MDF database for the first time, and I need a little help ..
我在 Visual Studio 2010 中创建了一个小的 MDF 数据库文件,然后创建了另一个项目并将该文件导入到项目本身中.
I made a small MDF database file in Visual Studio 2010, then created another project and imported the file into the project itself.
我不想通过代码连接到 MDF 文件.这是我正在使用的代码:
I am not trying to connect to the MDF file via code. Here the code I'm using:
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 ?
推荐答案
在Data Source
con.ConnectionString = @"Data Source=.SQLEXPRESS;
AttachDbFilename=c:folderSampleDatabase.mdf;
Integrated Security=True;
Connect Timeout=30;
User Instance=True";
这篇关于如何连接到 MDF 数据库文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!