Insert Bulk Data through procedure

Introduction: Through Ado.net we can insert entire data table data in database follow below steps.

Step 1: Create Table

  1. [IR] is my schema name
  2. CREATE table [IR].[PRODUCT]
  3. (
  4. PRODUCTID INT IDENTITY(1,1) PRIMARY KEY,
  5. PRODUCTNAME VARCHAR(50)NOT NULL,
  6. PRICE INT NOT NULL,
  7. CREATED_DATE DATETIME DEFAULT GETDATE()
  8. );
Step2: Create Type
  1. CREATE TYPE [IR].[ProductType] AS TABLE(
  2. [ProductName] [varchar](50)Not null,
  3. [Price] int not null
  4. )
Step 3: Create Procedure
  1. CREATE PROCEDURE [IR].[InsertProduct]
  2. @ProductTable [IR].[ProductType] READONLY
  3. AS
  4. BEGIN
  5. SET NOCOUNT ON;
  6. ------INSETING DATA IN TABLE
  7. INSERT INTO [IR].[PRODUCT](PRODUCTNAME,PRICE)
  8. SELECT * FROM @ProductTable
  9. SELECT @@rowcount;
  10. END
Step 4: Source Code
  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. namespace Bulk
  6. {
  7. class BulkData
  8. {
  9. static bool InsertBulkData(DataTable productTable)
  10. {
  11. try
  12. {
  13. using(var oSqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
  14. {
  15. using(var oSqlCommand = new SqlCommand())
  16. {
  17. oSqlCommand.Connection = oSqlConnection;
  18. oSqlCommand.CommandTimeout = 0;
  19. oSqlCommand.CommandText = "IR.InsertProduct";
  20. oSqlCommand.CommandType = CommandType.StoredProcedure;
  21. oSqlCommand.Parameters.AddWithValue("@ProductTable", productTable);
  22. oSqlConnection.Open();
  23. int x = Convert.ToInt32(oSqlCommand.ExecuteScalar());
  24. oSqlCommand.Dispose();
  25. oSqlConnection.Close();
  26. oSqlConnection.Dispose();
  27. if (x > 0)
  28. return true;
  29. return false;
  30. }
  31. }
  32. } catch (Exception ex)
  33. {
  34. //log the error..
  35. return false;
  36. }
  37. }
  38. static void Main(string[] args)
  39. {
  40. DataTable productTable = new DataTable();
  41. productTable.Columns.Add("ProductName", typeof(string));
  42. productTable.Columns.Add("Price", typeof(int));
  43. var row = productTable.NewRow();
  44. row["ProductName"] = "Pen";
  45. row["Price"] = 10;
  46. productTable.Rows.Add(row);
  47. row = productTable.NewRow();
  48. row["ProductName"] = "NoteBook";
  49. row["Price"] = 100;
  50. productTable.Rows.Add(row);
  51. row = productTable.NewRow();
  52. row["ProductName"] = "Dairy";
  53. row["Price"] = 200;
  54. productTable.Rows.Add(row);
  55. var status = BulkData.InsertBulkData(productTable);
  56. if (status)
  57. Console.WriteLine("Product has been inserted successfully");
  58. else Console.WriteLine("Error occured during Bulk Insert");
  59. }
  60. }
  61. }
Source code contains two method
  1. Main Method is creating DataTable some Product record and calling to InsertBulkData method and displaying status.
  2. InsertBulkData Method is parameterized method which is taking one DataTable parameter and it will insert entire DataTable record in database if records is inserted then will get rows count if row count is greater than one it will return true else false.

Step 5: Add ConnectionString in AppConfig

  1. <connectionStrings>
  2. <add name="ConnectionString" connectionString="Data Source=;Initial Catalog=;Persist Security Info=True;uid=;pwd="/>
  3. </connectionStrings>
  4. Data Source=Add your Database Server Name
  5. Initial Catalog=Database Name
  6. Uid=Database Server User Name
  7. Password: Database Server password Name.