.net core 连接 mssql数据库

跟在  .net 里面一样的连接方法,只是不能自动添加引用 dll


1、管理 nuget  ,添加以下两个引用(重要)


PM>  Install-Package System.Data.Common


PM>  Install-Package System.Data.SqlClient


2、测试代码


static void Main(string[] args)
        {
            using (SqlConnection con = new SqlConnection("Data Source=XFSERVER\\SQL2016;Initial Catalog=T2;Persist Security Info=True;User ID=sa;Password=xinfu978"))
            {
                con.Open();
                try
                {
                    using (SqlCommand command = new SqlCommand("SELECT TOP(10) * FROM T2", con))
                    {
                        SqlDataReader read=  command.ExecuteReader();
                        while (read.Read())
                        {
                            Console.WriteLine(read["fName"]  + " == " + read["fAddTime"]);
                        }
                    }
                }
                catch
                {
                    Console.WriteLine("Something went wrong");
                }
            }
            Console.ReadKey();
        }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值