码迷,mamicode.com
首页 > 数据库 > 详细

ADO.net之2-成功连接到数据库---ShinePans

时间:2015-08-25 14:06:27      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

配置数据库:

技术分享

连接字符串:
server=潘尚\\SQLEXPRESS;database=db_test;Trusted_Connection=true


连接代码:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SQLTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //连接数据库
            string connection =
                "server=潘尚\\SQLEXPRESS;database=db_test;Trusted_Connection=true";
            SqlConnection sc = new SqlConnection();
            sc.ConnectionString = connection;
            try
            {
                sc.Open();
                Console.WriteLine("已经打开数据库连接!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("打开数据库错误:{0}", ex.Message);
            }
            finally
            {
                sc.Close();
                Console.WriteLine("数据库连接已关闭!");
            }
            System.Console.ReadLine();
        }
    }
}

连接结果:

技术分享


版权声明:本文博客原创文章。博客,未经同意,不得转载。

ADO.net之2-成功连接到数据库---ShinePans

标签:

原文地址:http://www.cnblogs.com/gcczhongduan/p/4756890.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!