How to Save all the records of a DataTable with a single database call.

Posted by Ndebata under C# on 4/13/2011 | Points: 10 | Views : 1309 | Status : [Member] | Replies : 5
Hi Dear,

Is there any way of saving all the records of a DataTable to database , with a single call to database.

Thanks,
Debata




Responses

Posted by: Ndebata on: 4/13/2011 [Member] Starter | Points: 25

Up
0
Down
One way i can use SQL Bulk Copy, but its limited to save the records in a single database table.
What if data in DataTable required to be saved in different tables?

Is there any way of doing this?

Ndebata, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Walia_jagwinder on: 4/14/2011 [Member] Starter | Points: 25

Up
0
Down
use xml parameters

One person's data is another person's program.

Ndebata, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 4/14/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Walia,

Could please post a snippet/ link of your proposed implementation.

Thanks,
Debata

Ndebata, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ashua on: 12/18/2012 [Member] Starter | Points: 25

Up
0
Down
1.first create table for all record name and save .
2 add new item and select web from and add default.aspx page.
3.design the page for all database table entity.
4. select sqldatasources and check test query.
5 select connection path
5.coding default.aspx.cs page
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class SINUP : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"path");
//copy path & path paste
SqlCommand cmd;
SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string strinsert = "Insert intodetails()values()";
//details( type all entites in same database tabel) vales is create a vriable
example for insert into details(user,password name)values(@u,@p,@n);
cmd = new SqlCommand(strinsert, con);
con.Open();
cmd.Parameters.AddWithValue("@u", TextBox1.Text);
cmd.Parameters.AddWithValue("@p", TextBox1.Text);
cmd.Parameters.AddWithValue("@n", TextBox1.Text);
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("default.aspx");
Literal1.Text = "Record insert successfully";
}

Ndebata, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandeepmhatre on: 12/19/2012 [Member] Starter | Points: 25

Up
0
Down
Refer this code, will help you.

http://sandeepmhatre.blogspot.com/2012/12/sql-bulk-copy.html

Sandeep M,
Software Developer
Follow me on :
http://sandeepmhatre.blogspot.in

Ndebata, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response