Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
hello friends,

I have used sessions to store my shopping cart into sessions at client side.

I am storing all the shopping details in a datatable format. Now i want to store the same datatable in my database but i don't know how to send it to a stored procedure on SQL server and how to insert the same into database.

I think its possible through hashtable but can i send a datatable to a stored procedure through hash table and if yes then which data type should i take for same in stored procedure.

can anybody help me for same.

Thanks in advance.
Shailesh J.
Posted

1 solution

as i understand ... the best solution is the BulkCopy
ex:
C#
public void BulkCopy()
    {
        dt = (DataTable)Session["lstCars"];
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand("TRUNCATE TABLE carsForSale", connection);//this procedure to delete the whole data in  database table 
            connection.Open();
            cmd.ExecuteNonQuery();//first you have to clear the data
            SqlBulkCopy bulkObj = new SqlBulkCopy(connection);//bulkCopy can insert into you dable in database all the values in the DataTable @ the same time
            bulkObj.DestinationTableName = "carsForSale";//table name 
            bulkObj.WriteToServer(dtUsers);//data you want to insert
            connection.Close();
        }
    }

best regards
 
Share this answer
 
Comments
saj_21 1-Feb-12 5:48am    
Thanks friend but, i have a doubt as, its a bulk copy so is there any chances of data loss??
youssef_123 1-Feb-12 5:54am    
if you mean you dont want to delete the data that are in the table before adding the new data, i think you can so plz comment these lines:

qlCommand cmd = new SqlCommand("TRUNCATE TABLE carsForSale", connection);//this procedure to delete the whole data in database table
connection.Open();
cmd.ExecuteNonQuery();//first you have to clear the data

if this is not your question plz ask again but make it clear little bit
i am online
youssef_123 1-Feb-12 5:57am    
i am waiting your reply
youssef_123 1-Feb-12 6:04am    
look and if you mean you afraid from loosing the data it also maybe done when you isert one record
and if you want to to be on the safe side you have to search about something called transaction i dont know how to use it but this to insure that there is no data loss try and search it ..reply

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900