我如何连接到MDF数据库文件? [英] How do I connect to an MDF database file?

查看:273
本文介绍了我如何连接到MDF数据库文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在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.

我并不是试图连接到通过code中的MDF文件。在这里,code我使用的是:

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 ?

推荐答案

数据源之间添加空格

 con.ConnectionString = @"Data Source=.\SQLEXPRESS;
                          AttachDbFilename=c:\folder\SampleDatabase.mdf;
                          Integrated Security=True;
                          Connect Timeout=30;
                          User Instance=True";

这篇关于我如何连接到MDF数据库文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆