Store DataTable into SQL Server through SP

Step 1 :
 Create TYPE in sql server.
   Ex : Create TYPE CountryDet as Table(int CountryId,string Country)

Step 2 :
 Create Stored Procedure in SqlServer
  Ex : Create Procedure usp_CountryDetails
          @type varchar(8)
          @DataTable CountryDet ReadOnly
           as
          begin
          if(@type="select")
             select * from Mst_CountryDetails with(nolock)
          else if(@type="insert")
             insert into Mst_CountryDetails(CId,CName) select CountryIdCountry from @DataTable 
         else if(@type="update")
           update Mst_CountryDetails set CName=Countryfrom Mst_CountryDetails, @DataTable where Cid=CountryId
          else
           delete from Mst_CountryDetails where Cid in (select CountryId from @DataTable)
          end

Step 3 : 
  Call Stored Procedure from Front End(C#.Net).
Ex: public bool UpdateCountryInformation(DataTable dtCountry)
     {
      conOpen();
      SqlCommand cmd=new SqlCommand("usp_CountryDetails",con);
      cmd.Parameters.AddWithValue("@type","update");
      cmd.Parameters.AddWithValue("@ DataTable",dtCountry);
      cmd.ExecuteNonQuery();
      conClose();
      return true;
     }


Happy Coding...

No comments:

Post a Comment