1.写一个联合查询的SQL语句,保证可以查询到两张表的数据
string SQL = "select Details.ID,ReferenceNumber,Plaintiff,Defendant,KTSJ,KTRQ,JSSJ,KTFT,CaseName from Details,EAJ_FTSY where ReferenceNumber=AH";
2.将查询数据填充到DataTable中
DataTable dt = DBHelper.ExecuteSqlGetDataTable(SQL); //这是我写的数据库查询方法 /// <summary> /// 执行SQL语句,并返回DataTable对象 /// </summary> /// <param name="safeSql">SQL语句</param> /// <returns>返回DataTable</returns> public static DataTable ExecuteSqlGetDataTable(string safeSql) { SqlConnection sqlConn = new SqlConnection(connectionString); sqlConn.Open(); DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(safeSql, sqlConn); cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); sqlConn.Close(); return ds.Tables[0]; }
3.绑定GridView
GridView1.DataSource = dt;
GridView1.DataBind();