Sponser Link

How to populate dataset in C sharp using SQL database

In this article, we will use SqlDataAdapter for fill the dataset from SQL data base.

The Code is given below for read the company data from company table in fill in dataset.

 public DataSet Read_Comp() {

  DataSet ds = new DataSet();

  try {

   string ConnString = 

     ConfigurationManager.ConnectionStrings["NAYDrive_Conn"].ConnectionString;

   SqlConnection sqlconn = new SqlConnection(ConnString);

   string qry = "";

   qry = "SELECT  Comp_Id, Comp_Name ";

   qry += " FROM  Car_Company ";

   qry += " Order By Comp_Name";

   SqlDataAdapter ad = new SqlDataAdapter(qry, sqlconn);

   ad.Fill(ds);

  } catch (Exception ex) {

   PushExceptionMsg(ex.Message, ex.HResult);

  }

  return ds;

 }