简体   繁体   English

如何连接到 MDF 数据库文件?

[英]How do I connect to an MDF database file?

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.我没有尝试通过代码连接到 MDF 文件。 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?有人能指出我正确的方向吗?

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";
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| 文件路径应该有| 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. 它实际上链接到“当前项目目录\\ App_Data \\”或“当前项目目录”并获取.mdf文件.....将.mdf放在这些地方中的任何一个并且应该在visual studio 2010中工作。当你使用它时生产系统上的独立应用程序,然后是可执行文件所在的当前路径,应该具有.mdf文件。

转到服务器资源管理器>您的数据库>右键单击>属性> ConnectionString并复制连接字符串并复制到connectiongstring代码:)

对于Visual Studio 2015,连接字符串为:

"Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True"
Server=.\SQLExpress;AttachDbFilename=c:\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
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");

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;")
Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\hasif\Documents\Visual Studio 2015\Projects\vgsoft\SqlserverRepo\data\Database1.mdf";Integrated Security=True;Connect Timeout=30

1 1个

在此处输入图像描述

2 2个

在此处输入图像描述

ConnectionString连接字符串

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM