I've successfully imported a tab-delimited file into a dataset in asp.net (C#). I can bind the dataset to a gridview, and see my data (that part works fine).
Now I want to insert that dataset table (the only table in the dataset) into a sql table using an INSERT command - I don't know enough about datasets to figure it out.
Actually you won't use the INSERT command, to insert a new table you're going to need this query:
string query = "CREATE TABLE myTable (column1 INT, column2 NVARCHAR(10))";
Source: Creating Databases and tables using c#
Where 'column1' is your column's name and INT the type. You can see more about the types here: http://www.w3schools.com/sql/sql_datatypes_general.asp
and you can also see more SQL commands here: http://www.w3schools.com/sql/sql_syntax.asp
Related
I have a datatable DT with 25 columns. I want to insert that data from DT into a SQL Server table SQDT with the same columns. I'm using an INSERT query for now.
Is there a better way to approach this problem instead of using INSERT on each record? Can I bulk insert into the database using a stored procedure or some other efficient way? It takes a lot of time to insert as DT has 130000 tuples.
Yes, there is a better way. You need to create Type in SQL server with the same definition as your Datatable.
Here you can User-Defined TableType
CREATE TYPE [dbo].[MyTable] AS TABLE(
[Id] int NOT NULL,
)
Define Parameter in SP
CREATE PROCEDURE [dbo].[InsertTable]
#myTable MyTable readonly
AS
BEGIN
insert into [dbo].Records select * from #myTable
END
and send your DataTable as a parameter
You can use SqlBulkCopy in-build for C#.
SqlBulk Copy can be used for bulk inserts with batches which worked efficiently for me
https://programmingwithmosh.com/net/using-sqlbulkcopy-for-fast-inserts/
I need to send a datatable to sqlserver2012 and, insert datatable into a specific table. How can i do that?
( i don't want do this work in C# i want to send datatable to sqlserver and do this work in sqlserver). I saw similar question, but didn't found my answer.
Edit:
into sqlserver i want insert datatable rows into a table.
Your point is not clear enough for what is your purpose, you can use the Update() method which would perform the operation of updating newly added records to your table, this would implicitly send the table to the sql server and update data
i think you want to save bulk data from DataTable in sqlserver. please peroform following steps
1) Create custom table value data type in sqlserver 2008 onword
2) create stored procedure with custom table value data type parameter.
3) use following code for set parameter value from code
SqlParameter param = cmd.Parameters.AddWithValue("#RECORD_TBL", Datatable);
I am using C# to read a SQL stored procedure, put the results of the stored procedure into a C# data table and then reading the data table row by row to build up my "Insert into....values "etc. This creates my Excel spreadsheet with the correct data. However, instead of inserting row by row, is there a way of doing a bulk insert?
Failing that I was thinking of getting the stored procedure to write the results to a permanent table and therefore is there a way of doing an "Insert into ....select from ". When I have tried this in C# the code is unable to find the SQL table name specified "Microsoft database access engine cannot find the object ", what is the correct syntax and where do you specify where/how to access the SQL table?
Thanks
Hi, that link looks like it's using Microsoft.Office.Interop.Excel;
I'm using OLEDB (which i'm now beginning to regret!). So basically i have a C# class that is called from another component. This C# class reads a sql stored procedure, puts the results into a data table. I then set up the table definition using the OLEDBcommand "Insert into ( []) values ("?"). I then define the parameters e.g. cmd.Parameters.Add(columnHeading, OleDbType.VarChar, size) etc. Then for each row i find in the data table i set the cmd.Parameters[i].value = row[i], where parameters[i] is incremented for each column in that row. I then loop round for each data row and set cmd.Parameters[i].value appropriately. As I have to set the cmd.Parameters[i].Value for each row i find in my dataset and then cmd.ExecuteNonQuery();
, this is quite time consuming. So is there a way to bulk insert the data from the data table into the OLEDB command, if not, can i insert the data by referencing a SQL table directly and doing a "insert into..select from"?
You can separate your insert statements with a semicolon and run just 1 command. For instance ...
string sql = "insert into table (col1,col2) values ('row1','row1');"
sql += "insert into table (col1,col2) values ('row2','row2');"
sql += "insert into table (col1,col2) values ('row3','row3');"
SqlConnection conn = new SqlConnection("some connection string");
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
conn.ExecuteNonQuery();
conn.Dispose();
cmd.Dispose();
Or something similar. You are executing all 3 queries with 1 command. This might not work with databases other than SQL Server, but will definitely work with SQL Server.
is there any way to insert in bulk (i mean all gridview rows) to sql server database in single submit. its an ASP.Net app and I also want to validate data.
Have a look at using SqlBulkCopy Class
In oracle we have batch insert for example
your query returns two rows with column name as a,b,c
a:b:c
1:2:3
4:5:5
you can use
insert into tab_name(a,b,c)(qry)
commit;
Make it dynamic and it should work.
This is my first post.. I have 2 SQL Server databases located on different servers..
Let's say SDT for source data table from source database SDB to DDT (Destination data table) for Database DDB
I'm using C# for bulk copying from SDT to DDT..
My code is something like this:
sqlcommand = "Delete * from DDT where locID = #LocIDParam" // #LocIDParam is the parameter for a specific location //
then bulk copy "Select * from SDT where locID = #LocIDParam" // the steps are well known..
I just don't want to go for useless details..
However, my SDT has a huge data so that it causes high traffic for bulk copying the whole table
Is there anyway for bulk copying the only updated records from SDT to DDT as well as inserting the new ones???
Do you think using an SQL trigger for updated and newly inserted data is the best idea for this kind of scenarios? (trigger to insert the primary key value into a single column table for the new and update then deleting and inserting from/to DDT based on this )
PS. I don't want to use SQL replication for that since it has a lot of problems..
Thank you in advance
From the date I suppose you already fond your solution. In case not, here is how we deal with a somehow similar situation.
On the source table we have a column that shows if the data has to be send to the destination. We use a boolean but you can also have a datetime field that shows last update date.
Then our pull process does following :
Pull all the flagged data in a temporary table on the destination server
Update records that exists in both table
Insert all records from temporary table that don't exist in destination table
Drop the temporary table
If you use SQL 2008, there is a merge option that I don't know. Here a link that explains it :
SQL 208 MERGE command
Hope this will help you if you still need.