C# 连接到sql server 2005

来源:互联网 发布:上古卷轴5优化字体 编辑:程序博客网 时间:2024/04/28 14:26

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DBConnections
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
 
            try
            {

                //Data Source就是打开sql时出现的服务器名称

                string conStr = "Data Source=PZUM51D4ZOEY9QN;Initial Catalog=master;Persist Security Info=True;User ID=sa;pwd=sql";
                SqlConnection con = new SqlConnection(conStr);
                con.Open();
                if (con.State == ConnectionState.Open)
                {
                    MessageBox.Show("数据库连接成功");
                }
            }
            catch
            {
                MessageBox.Show("数据库连接失败");
            }
               

        }
    }
}