Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem with Multiple Insert in sql server from c# code.

Problem with Multiple Insert in sql server from c# code.

Scheduled Pinned Locked Moved ASP.NET
tutorialcsharpdatabasesql-serversysadmin
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Johndas
    wrote on last edited by
    #1

    Hi all, can you please guide me, how to insert multiple insert using this code. SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "usp_Information"; for (Int64 acct = 1000000000000001; acct <= 1000000000024000; acct++) { String sAcct = Convert.ToString(acct); SqlParameter p1 = new SqlParameter(); p1.ParameterName = "@ShawNumber"; p1.SqlDbType = SqlDbType.VarChar; p1.Direction = ParameterDirection.Input; p1.Value = sAcct; SqlParameter p2 = new SqlParameter(); p2.ParameterName = "@UserId"; p2.SqlDbType = SqlDbType.Int; p2.Direction = ParameterDirection.Input; p2.Value = 88578539; SqlParameter p3 = new SqlParameter(); p3.ParameterName = "@ParentShawNumber"; p3.SqlDbType = SqlDbType.VarChar; p3.Direction = ParameterDirection.Input; p3.Value = "22"; cmd.Parameters.Add(p1); cmd.Parameters.Add(p2); cmd.Parameters.Add(p3); cmd.ExecuteNonQuery(); } Here cmd.ExecuteNonQuery(), it is use for single insert statment, i want to insert multiple records at a time, please guide me. Thanks in Advance.

    JohnDas

    S 1 Reply Last reply
    0
    • J Johndas

      Hi all, can you please guide me, how to insert multiple insert using this code. SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "usp_Information"; for (Int64 acct = 1000000000000001; acct <= 1000000000024000; acct++) { String sAcct = Convert.ToString(acct); SqlParameter p1 = new SqlParameter(); p1.ParameterName = "@ShawNumber"; p1.SqlDbType = SqlDbType.VarChar; p1.Direction = ParameterDirection.Input; p1.Value = sAcct; SqlParameter p2 = new SqlParameter(); p2.ParameterName = "@UserId"; p2.SqlDbType = SqlDbType.Int; p2.Direction = ParameterDirection.Input; p2.Value = 88578539; SqlParameter p3 = new SqlParameter(); p3.ParameterName = "@ParentShawNumber"; p3.SqlDbType = SqlDbType.VarChar; p3.Direction = ParameterDirection.Input; p3.Value = "22"; cmd.Parameters.Add(p1); cmd.Parameters.Add(p2); cmd.Parameters.Add(p3); cmd.ExecuteNonQuery(); } Here cmd.ExecuteNonQuery(), it is use for single insert statment, i want to insert multiple records at a time, please guide me. Thanks in Advance.

      JohnDas

      S Offline
      S Offline
      Sandeep Mewara
      wrote on last edited by
      #2

      What are you doing? The way you had put

      cmd.Parameters.Add(p1);
      cmd.Parameters.Add(p2);
      cmd.Parameters.Add(p3);

      in for loop, the same three parameters keep on adding again and again. Does this method works fine for you? I doubt 2nd execution would work!

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups