473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to insert data from a data table into sqlserver table

9
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....would be really helpful. thanks in advance.
Jul 25 '10 #1

✓ answered by ThatThatGuy

@indona
http://www.jonasjohn.de/snippets/csh...on-example.htm

I think you should follow this tutorial

4 34430
ThatThatGuy
449 Expert 256MB
@indona
http://www.jonasjohn.de/snippets/csh...on-example.htm

I think you should follow this tutorial
Jul 26 '10 #2
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(conString);
// 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.Count;i++)
{
command = new SqlCommand("INSERT INTO tables (value,val) VALUES('"+dt.Rows[i][0].ToString()+"','"+dt.Rows[i][1].ToString()+"')",con);
command.ExecuteNonQuery();
}

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
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
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
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
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?...
4
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...
4
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? ...
4
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
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 ...
1
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...
10
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
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.