首先通过ado.net技术从数据库获取数据,将获取到的数据绑定到前端显示:
例://创建基于Windwos身份登录验证的SQLServer的连接字符串
stringstr_conn="server=.;DataBase=数据库;Trusted_Connection=true";//integratedSecurity=true";
//1、用于从数据库中获取数据的查询字符串
stringstr_select="select需要的字段from表";
//2、开始建立建立并打开连接
myconn=new(str_conn);
myconn.Open();
//3、创建连接,从数据库中读取数据
sql_Adapter=new(str_select,myconn);
//4、创建DataSet连接
DataSetdataset1=newDataSet();
//5、使用.Fill(DataSet_Name,index_name)方法将读取的数据存入DataSet定义的名为任意名的Datatable中,其中任意名表用于数据的标识(索引)
sql_Adapter.Fill(dataset1,"任意名");
//6、将DataTable表中employee表的视图赋值给控件以便输出
this..DataSource=dataset1.Tables["任意名"].DefaultView;
//7、关闭数据库连接
myconn.Close();