SQL Server Express
编程接口 > ADO.Net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Connection
{
    class Program
    {
        static void Main(string[] args)
        {
            //构造连接字符串
            SqlConnectionStringBuilder connStr = new SqlConnectionStringBuilder();
            connStr.DataSource = @".\SQLEXPRESS";
            connStr.InitialCatalog = "master";
            connStr.IntegratedSecurity = true;

            SqlConnection conn = new SqlConnection();//创建连接对象
            conn.ConnectionString = connStr.ConnectionString;//设置连接字符串

            conn.Open();//打开连接

            if(conn.State == ConnectionState.Open)
            {
                Console.WriteLine("Database is linked.");
                Console.WriteLine("\nDataSource:{0}",conn.DataSource);
                Console.WriteLine("Database:{0}",conn.Database);
                Console.WriteLine("ConnectionTimeOut:{0}",conn.ConnectionTimeout);
            }

            conn.Close();//关闭连接
            conn.Dispose();//释放资源

            if(conn.State == ConnectionState.Closed)
            {
                Console.WriteLine("\nDatabase is closed.");
            }

            Console.Read();
        }
    }
}
对于较高版本，如SQL Server Express大于版本2010的，可以通过在Visual Studio，依次点击“视图”(菜单栏)->“服务器资源管理器（Server Explorer）”，进入“服务器资源管理器”窗口。右键点击“数据连接（Data Connection）”，在弹出的上下文菜单中选择“添加连接（Add Connection）”，在弹出的窗口中将Data Source选择为Microsoft SQL Server，“服务器名”设置为(LocalDb)\MSSQLLocalDB，附加服务器文件为具体的mdf文件并点击确定。此时，通过Microsoft SQL Server（SQL Client）方式访问指定的mdf文件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection();
            connection.ConnectionString =
                @"Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=D:\apps\Documents\NORTHWND.mdf;Integrated Security = True;Connect Timeout = 30;";
              //@"Data Source=(localdb)\MyInstance;InitialCatalog =blat;Integrated Security = True;Connect Timeout = 30;";
            connection.Open();
            if (connection.State == ConnectionState.Open)
            {
                Console.WriteLine("Database is linked.");
                Console.WriteLine("DataSource:{0}", connection.DataSource);
                Console.WriteLine("Database:{0}", connection.Database);
                Console.WriteLine("ConnectionTimeOut:{0}", connection.ConnectionTimeout);
            }
            SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Employees", connection);
            DataTable table = new DataTable();
            adapter.Fill(table);
            var result = (from row
                          in table.Rows.Cast<DataRow>()
                          select
                          new { ID = (int)row[0], LastName = (string)row[1], FirstName = (string)row[2] });
            result.ToList().ForEach(x => Console.WriteLine($"{x.ID},{x.LastName},{x.FirstName}"));
        }
    }
}
对于较低版本，如SQL Server Express 2005的mdf文件，可以直接用如下connection string打开访问（可能需要administrator权限）：
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection();
            connection.ConnectionString =
                @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\apps\Documents\NORTHWND.mdf;Integrated Security = True;Connect Timeout = 30;";                
            connection.Open();
            if (connection.State == ConnectionState.Open)
            {
                Console.WriteLine("Database is linked.");
                Console.WriteLine("DataSource:{0}", connection.DataSource);
                Console.WriteLine("Database:{0}", connection.Database);
                Console.WriteLine("ConnectionTimeOut:{0}", connection.ConnectionTimeout);
            }
        }
    }
}