Problem with Multiple Insert in sql server from c# code.
-
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
-
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
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!