1.建立鏈接字符串sql
string con = "Data Source=127.0.0.1;Initial Catalog=dingdan;Integrated Security=True";
2.經過鏈接字符串生成鏈接對象spa
SqlConnection conn = new SqlConnection(con);
3.打開鏈接對象code
conn.Open();
4.經過鏈接對象生成SQLCommand對象對象
SqlCommand cmd = conn.CreateCommand();
5.設置SqlCommand的sql語句blog
cmd.CommandText = "select * from goods";
6.執行sql語句,並用SqlDataReader讀取結果字符串
SqlDataReader reader = cmd.ExecuteReader();
7.新建DataTable對象cmd
DataTable dt=new DataTable();
8.加載結果到表對象string
dt.Load(reader);
至此就將所需結果加載到DataTable裏面了it