Hi all,
How can we add more that 1 row data to a table in sql server at a time?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
shashi kiran Jillepally25 reputationPosted Feb 26, 2014, 1:58 AM EST
its inserts data(more than one Row at a time(In Single post back) data(All rows of the gridview) is inserted into sql type table(Tbl_LeaveType1) at once first and than to original(Tbl_Leave1)
create table Tbl_Leave1
(
LType varchar(5),
JAN varchar(20),
FEB varchar(20)
)
create type Tbl_LeaveType1 as table
(
TLType varchar(5),
TJAN varchar(20),
TFEB varchar(20)
)
create procedure Sp_Leave
(
@Leave Tbl_LeaveType1 readonly
)
as
insert into Tbl_Leave1(LType,JAN,FEB)select TLType,TJAN,TFEB from @Leave;
Source Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test1.aspx.cs" Inherits="Test1" %>
.Cs(C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Test1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TempTbl();
}
}
public void TempTbl()
{
//Here i have load a gridview with empty data dynamically with Temprary table in .cs file.
DataTable Dt = new DataTable();
Dt.Columns.Add(new DataColumn("Leave", typeof(String)));
Dt.Columns.Add(new DataColumn("JAN", typeof(String)));
Dt.Columns.Add(new DataColumn("FEB", typeof(String)));
Dt.Rows.Add("CL");
Dt.Rows.Add("EL");
Dt.Rows.Add("SL");
Dt.Rows.Add("ML");
Dt.Rows.Add("OL");
Gv1.DataSource = Dt;
Gv1.DataBind();
}
public DataTable GetGv()
{
DataTable Dt1 = new DataTable();
Dt1.Columns.Add(new DataColumn("Leave", typeof(String)));
Dt1.Columns.Add(new DataColumn("JAN", typeof(String)));
Dt1.Columns.Add(new DataColumn("FEB", typeof(String)));
DataRow Dr;
foreach (GridViewRow RW in Gv1.Rows)
{
Label Lv = (Label)RW.FindControl("L");
string Leave = Lv.Text;
TextBox J = (TextBox)RW.FindControl("t1");
string JAN = J.Text;
TextBox F = (TextBox)RW.FindControl("t2");
string FEB = F.Text;
Dr = Dt1.NewRow();
Dr[0] = Lv.Text;
Dr[1] = J.Text;
Dr[2] = F.Text;
Dt1.Rows.Add(Dr);
}
return Dt1;
}
public void SaveData()
{
DataTable Dt2 = new DataTable();
Dt2 = GetGv();
//using web.config file
string strcon = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
//or you can write query string//
using (SqlConnection con = new SqlConnection(strcon))
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Sp_Leave";
SqlParameter P1 = cmd.Parameters.AddWithValue("@Leave", Dt2);
con.Open();
cmd.ExecuteNonQuery();
}
}
protected void Btn1_Click(object sender, EventArgs e)
{
SaveData();
}
}
VulpesRank #55 · 29.2K reputationPosted Feb 25, 2014, 1:16 AM EST
Vithal WadjeRank #19 · 47.2K reputationPosted Feb 25, 2014, 7:45 AM EST
Jignesh TrivediRank #16 · 50.3K reputationPosted Feb 25, 2014, 6:27 PM EST
agree with both.
it is also depending on situation when you want add multiple record.
suppose you want to add records from table than you can use
Insert into table1 (a,b,c)
select a,b,c from tableb
hope this will help you.
Suma M161 reputationPosted Mar 9, 2014, 10:08 PM EDT
Thanks for replying.. I want to know how can i do this... If i want to add say 4- 5 rows to a table and in some cases how can i add 10 or more... In such scenario what to do? And is it possible? Then how? Have you`ll come across such scenarios?
VulpesRank #55 · 29.2K reputationPosted Mar 10, 2014, 2:58 AM EDT
In that situation, there will probably be some relationship between the records you're inserting and you could therefore insert the records one by one using a loop.
You could also use SqlBulkCopy as Vithal suggested though, for that to be worthwhile, I'd say you really need to be inserting 100 or more records at a time.
shashi kiran Jillepally25 reputationPosted Mar 10, 2014, 5:25 AM EDT
Suma M161 reputationPosted Mar 14, 2014, 11:00 PM EDT