代码如下:
string connStr = "server=LENOVO-PC;database=PMS_db;Trusted_Connection=SSPI";//创建连接字符串
SqlConnection conn = new SqlConnection(connStr);//创建连接对象
if (conn.State == ConnectionState.Closed)//判断数据库连接的状态
conn.Open();DataSet ds = new DataSet();//实例化
SqlDataAdapter adapter = new SqlDataAdapter();创建数据适配器
string sqlStr = "SELECT Type AS 类型,Name AS 姓名,Squad AS 组别,Leader AS 组长 FROM Main_tb ORDER BY Squad,TypeNumber";//sql字符串
SqlCommand comm = new SqlCommand(sqlStr, conn);
adapter.SelectCommand = comm;
adapter.Fill(ds, "Main_tb");//将按照条件查出来的Teachers表中信息填充到ds中
this.dataGridView1.DataSource = ds.Tables["Main_tb"];