跟在 .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();
}