Fill datatable through DataAdapter using C# and Stored Procedure

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 222
The below code will do so

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace TestNS
{
class Program
{
static void Main(string[] args)
{


try
{

string ConnectionPath = System.Configuration.ConfigurationManager.ConnectionStrings["DBString"].ToString();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();


using (var sqlCon = new SqlConnection(ConnectionPath))
{
using (SqlCommand cmd = new SqlCommand("SP_Name", sqlCon))
{
sqlCon.Open();
da.SelectCommand = cmd;
da.Fill(dt);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}


First we are creating a connection string which we are passing to a SQL Command Object along with the SP_Name. Then open connection , fetch the record and fill the data table via data adapter

Comments or Responses

Login to post response