1.创建连接字符串
string con = "Data Source=127.0.0.1;Initial Catalog=dingdan;Integrated Security=True";
2.通过连接字符串生成连接对象
SqlConnection conn = new SqlConnection(con);
3.打开连接对象
conn.Open();
4.通过连接对象生成SQLCommand对象
SqlCommand cmd = conn.CreateCommand();
5.设置SqlCommand的sql语句
cmd.CommandText = "select * from goods";
6.执行sql语句,并用SqlDataReader读取结果
SqlDataReader reader = cmd.ExecuteReader();
7.新建DataTable对象
DataTable dt=new DataTable();
8.加载结果到表对象
dt.Load(reader);
至此就将所需结果加载到DataTable里面了