using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 数据库连接 { class Program { static void Main(string[] args) { //连接数据库 SqlConnection conn = new SqlConnection("server=.;database=cangkudata;user=sa;pwd=1243672715"); SqlCommand cmd = conn.CreateCommand();//创建数据库操作对象 cmd.CommandText = "select * from cangku ";//指令 conn.Open();//打开数据库连接 SqlDataReader dr = cmd.ExecuteReader();//返回一个dr结果值 if (dr.HasRows)//判断是否含有数据,返回true or false { while (dr.Read())//指针下移 { Console.Write(dr[0].ToString() + " " + dr[1].ToString() + "\n"); } } conn.Close(); Console.ReadKey(); } } }