Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2016-06-07 15:59:23
Original
1052 people have browsed it

数据库的配置: 连接字符串: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;

数据库的配置:

\

连接字符串: 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();
        }
    }
}
Copy after login

连接结果:



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template