标签:
自己名一个名字,我选择的是test1.mdf
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace adoStep_one
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//连接字符串,通过打开mdf文件,然后查看属性获得连接字符串
//这一点是非常方便的
string connectionString="Data Source=(LocalDB)\\v11.0;AttachDbFilename=E:\\编程开发\\dotNET\\ADO.NET\\demo\\连接数据库\\adoStep_one\\adoStep_one\\test1.mdf;Integrated Security=True";
//访问数据库的类
SqlConnection conn = new SqlConnection(connectionString);
//与数据库建立连接
try
{
conn.Open();
MessageBox.Show("已建立与数据库的连接!");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
conn.Dispose();
MessageBox.Show("已断开与数据库的连接");
}
}
}
ADO.NET---VS2013连接自带的数据库---ShinePans
标签:
原文地址:http://blog.csdn.net/shinepan/article/details/46391901