473,811 Members | 2,717 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to insert data from a data table into sqlserver table

9 New Member
hi, i have to enter data from a delimited file into sqlserver database table.
i have been able to delimit the file and read the data into a data table, now i want enter the data table contents to sqlserver. some help in this matter...if possible code for moving data from data table to the database....wou ld be really helpful. thanks in advance.
Jul 25 '10 #1
4 34485
ThatThatGuy
449 Recognized Expert Contributor
@indona
http://www.jonasjohn.de/snippets/csh...on-example.htm

I think you should follow this tutorial
Jul 26 '10 #2
maddyromeo
22 New Member
if you already have data in your DataTable it just takes
4 steps to update into database

1)create a connection to database:

con = new SqlConnection(c onString);
// for conString see
http://www.csharp-station.com/Tutori.../Lesson02.aspx


2)Open connection :

con.Open();


3)Enter into DataBase with a loop like :

for(int i=0;i<dt.Rows.C ount;i++)
{
command = new SqlCommand("INS ERT INTO tables (value,val) VALUES('"+dt.Ro ws[i][0].ToString()+"', '"+dt.Rows[i][1].ToString()+"') ",con);
command.Execute NonQuery();
}

here i assume two columns so

dt.Rows[i][0].ToString() is the string from row i and column 0
dt.Rows[i][1].ToString() is the string from row i and column 1

u have to pass values to your own need

4)Close Connection :
con.Close();
Jul 26 '10 #3
indona
9 New Member
thanks a lot....the tutorial and the code you provided really helped..n wid that have been able to do my work..
thanks a lot all..
Jul 26 '10 #4
samundersingh19
1 New Member
Try this:
Expand|Select|Wrap|Line Numbers
  1. private void BulkUpload(DataTable dt)
  2. {
  3.     dt.TableName="YourDataTable";
  4.     string constr="your connection string";
  5.     using(SqlConnection connection=new SqlConnection(constr))
  6.     {
  7.       connection.Open();
  8.       //CreatingTranscationsothatitcanrollbackifgotanyerrorwhileuploading
  9.       SqlTransaction trans=connection.BeginTransaction();
  10.      //Start bulkCopy
  11.      using(SqlBulkCopy bulkCopy=new SqlBulkCopy(connection,
  12.      SqlBulkCopyOptions.TableLock|
  13.      SqlBulkCopyOptions.FireTriggers,
  14.      trans))
  15.      {
  16.        //Setting timeout to 0 means no time out for this command will not timeout until upload complete.
  17.      //Change as per you
  18.      bulkCopy.BulkCopyTimeout=0;
  19.      bulkCopy.DestinationTableName=dt.TableName;
  20.      //write the data in the "dataTable"
  21.      bulkCopy.WriteToServer(dt);
  22.     }
  23.    }
  24. }
  25.  
From:

Bulk insert or upload records into SQL Server with DataTable in c sharp using SqlBulkCopy
Feb 21 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
16074
by: ImraneA | last post by:
Hi there Application : Access v2K/SQL 2K Jest : Using sproc to append records into SQL table Jest sproc : 1.Can have more than 1 record - so using ';' to separate each line from each other.
2
52105
by: maltchev | last post by:
i need to insert data from an xml file into sql server table. the xml file contains only one record. how to insert the data? how to map the names of the fields in the xml file and the table? thank you in advance.
4
11838
by: authorking | last post by:
I use the following code to insert a data record in to a datatable of an access database.But every time I execute the command, there will rise an exception and the insert operation can't be completed. here's the code: System.Data.OleDb.OleDbConnection hist_cnn = new System.Data.OleDb.OleDbConnection(); System.Data.OleDb.OleDbCommand hist_command = new System.Data.OleDb.OleDbCommand();
4
2090
by: Renato Vieira | last post by:
Is there a way of insert or update images (*.gif or *.jpg) in a image field of a SQLServer table without using code? Some sort of small application can do that? by supplying the filename maybe? Thanks in advance... -- Renato /*Portugal*/ Vieira
4
1679
by: Bill Nguyen | last post by:
I wonder if I can write to an XML file using column structure (and column names) of an SQLserver table. for example: Table A: column1 int column2 char(30) column3 date XML output:
3
5710
by: Dave | last post by:
I'm writing a c# Windows program that needs to be able to insert records into a dbase table. I can read it using a dataset but can't insert records. I receive the following error, ERROR Unexpected error from external database driver (15877). What is wrong?
1
272
by: Frankz | last post by:
Hi, I am new to this group so forgive me if this question sounds too newbie. :-) I am writing a C# program. One of its functionalities is to use a .Net dataset to create and populate a table in the SQLServer database. I suppose I can 1) iterate through the column names of the .Net dataset to generate an SQL "CREATE TABLE" statement to create the table;
10
17956
by: pbd22 | last post by:
Hi. Like the title says - how do i do this? I was given the following example: INSERT INTO TABLE2 SELECT * FROM TABLE1 WHERE COL1 = 'A' The above statement threw the following error:
15
3689
by: Piero 'Giops' Giorgi | last post by:
Hi! I have a question: I already have a DB that uses partitions to divide data in US Counties, partitioned by state. Can I use TWO levels of partitioning? I mean... 3077 filegroups and 50 partition functions that address
3
2083
by: deneushasler | last post by:
Hello my name is Juan Jose. My problem is as follows. When I try to insert a record into a table (access) to control DetailsView Visual Web Developer 2005, when I run the page and insert a record pulse, whenever I get the following error: How could fix it?
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10398
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.