<strong>具体代码如下:</strong>
using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace WindowsFormsApplication4{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connString = @"Data Source=YAYUN\SQLEXPRESS;Initial Catalog=dawn;Integrated Security=True"; SqlConnection connection=new SqlConnection(connString); connection.Open(); MessageBox.Show("打开数据库成功"); connection.Close(); MessageBox.Show("关闭数据库成功"); } }}
应该注意的问题有:
1.数据库服务启动
2.确保这句话的正确性
string connString = "Data Source=YAYUN\SQLEXPRESS;Initial Catalog=dawn;Integrated Security=True";
Data Source=服务器名;
Initial Catalog=服务器名;
做技术就是要有耐心,这个看似很简单的问题,我实际操作的时候也遇到不少麻烦,但是认真分析之后还是解决了。