How to use the data adapter SqlDataAdapter to retrieve data from the database SQL Server and populate it with the dataset:
Methods to get data sets
Private DataSet GetDataSet ()
{
String Strcon = @ "Data source=./sqlexpress;initial catalog=school;uie=sa;pwd=123456";
SqlConnection con = new SqlConnection (Strcon);
String sql = "SELECT * from student";
DataSet ds = new DataSet ();
SqlDataAdapter objadpter = new SqlDataAdapter (sql, con);
Objadapter. Fill (ds, "Student");
return DS;
}
As you can see from the code, using an adapter to populate a dataset is divided into 4 steps:
Create a data connection (Connection) object.
Create an SQL statement to query the data in the database.
Third, create the DataAdapter object, the parameter is the SQL statement and the connection object.
Four, call the DataAdapter object's fill () method to fill the dataset
Note: When populating a DataSet with the Fill () method of the DataAdapter object, a datasheet is created if the dataset originally had no data tables. If you have a datasheet with the same name in the dataset, the data is added to the datasheet.