How to perform a SELECT statement using ADO.NET : SqlCommand Select : ADO.Net C# Examples


C# Examples » ADO.Net » SqlCommand Select »

 

How to perform a SELECT statement using ADO.NET









    
using  System;
using  System.Data;
using  System.Data.SqlClient;

class  MainClass
{
    public  static  void  Main()
    {
        string  connectionString  ="server=localhost;database=Northwind;uid=sa;pwd=sa";

        SqlConnection  mySqlConnection  =  new  SqlConnection(connectionString);

        string  selectString  =  "SELECT  ID,  FirstName  FROM  Employee";

        SqlCommand  mySqlCommand  =  mySqlConnection.CreateCommand();
        mySqlCommand.CommandText  =  selectString;
        SqlDataAdapter  mySqlDataAdapter  =  new  SqlDataAdapter();

        mySqlDataAdapter.SelectCommand  =  mySqlCommand;

        DataSet  myDataSet  =  new  DataSet();

        mySqlConnection.Open();

        string  dataTableName  =  "Employee";
        mySqlDataAdapter.Fill(myDataSet,  dataTableName);

        DataTable  myDataTable  =  myDataSet.Tables[dataTableName];

        foreach  (DataRow  myDataRow  in  myDataTable.Rows)
        {
            Console.WriteLine("ID  =  "  +  myDataRow["ID"]);
            Console.WriteLine("FirstName  =  "  +  myDataRow["FirstName"]);
        }
        mySqlConnection.Close();
    }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo ADO.Net
» SqlCommand Select