Is there any way to quickly import data from a .csv or tab delineated .txt into a SQL Compact Edition 3.5 Table?
I have a large amount of data that is impractical for manual input.
I know I can use the BULK INSERT function if I wanted to import into a Server Based SQL Database, but this method does not work in SQL CE.
I use visual studio 2010 and I have SQL Server Management Studio installed.
Any help will be appreciated!
You can use my VS Add-in, which generates INSERT statements based on a CSV file: http://sqlcetoolbox.codeplex.com
Maybe simpler is better. Nothing easier than your own code which can be expanded very easily. You could even have it build the table dynamically if you wanted but probably not necessary.
var stuff = from l in File.ReadLines(filename)
let x = l.Split(new [] {',', ' '}, StringSplitOptions.RemoveEmptyEntries)
.Skip(1)
.Select(s => int.Parse(s))
select new
{
Sum = x.Sum(),
Average = x.Average()
};
see: Read Csv using LINQ
If you have Microsoft Access, you can import .csv or delimited .txt data as a new table then upsize to SQL database. Also you can create a linked table and copy-paste the data (say from Microsoft Excel) and it tends to be efficient and reliable (throwing errors into a separate table that you can review).
There is no straight way of doing it, you will have to read each line in the file and insert one by one to SQLCE. There's some posts on it before, folks using C# program to read the file to a DataTable. If you know C# its fairly simple to setup and run.
Bulk insert from DataTable to SQLCE DataSource
http://ruudvanderlinden.com/2010/10/13/bulk-insert-into-sql-ce-in-c/
Oops just noticed that c# is one of the tags in your question :D
My tool of choice when dealing with csv is powershell since it has an import-csv command built in. It's fantastic glue for this kind of stuff.
Here's a link where the developer imports a csv and converts it to an insert script.
http://allen-mack.blogspot.com/2008/02/powershell-convert-csv-to-sql-insert.html
To run the script from the link (note: powershell gives you tab completion so you can use it to help with file paths while you are typing):
Create a file named something like Import-File.ps1 and copy the contents of the script from the link into it.
Start powershell
Type 'set-executionpolicy remotesigned' (note: this is loosening security of your system just a tad; but the default settings won't let you run any scripts)
Navigate to the directory with both your script and import file
Type '.\import-file.ps1 .\importfile.csv'
Hit enter; voila, you should have an insert sql script in the same directoy (i.e., 'importfile.sql' in our case)
Finally, since you can instantiate .net objects from within powershell, you could alter the script and do a number of things like insert data directly into a database.
Related
While I use .NET Core update database in terminal, it creates the database, but it cannot be opened and I cannot enter any data. I am using sqlite extension and it says file not displayed in the editor because it is either binary or uses an unsupported text.
VisualStudio Code is unable to open a database in its entirety and output everything at once. Instead, you can use VisualStudio Code to connect to a database and then use Statments to extract data from the database.
Use SELECT * FROM tablename to output all table data.
However, you must execute this line with programming language that you are using to connect to database!
I am developing a software using a MSSQL database for holding the data. In the program I implemented a function for creating a backup with SMO.
Now I am trying to implement a restore function. It works without any problems when the user has to insert the path manually. But I want to implement a select file dialog like this one the SQL Server Management Studio (SSMS) is opening when selecting a custom medium (see on the screenshot).
I already found the Microsoft.SqlServer.Management.Smo.Server class with the method EnumDirectories, but it returns only directory names and no files. When I confirm the selection I need the path in format C:\Directory\FileName.bak.
Is it possible to meet my needs with using SMO?
Many months ago I found a solution. I just developed a custom SelectFileDialog which is able to connecto to SQL server and work with the following SQL functions:
For searching available drives:
exec xp_fixeddrives
For reading folders and files:
exec xp_dirtree 'PATH', 1, 1
The PATH variable is dynamicly replaced depends on which folder is expanded by the user. It works without problems.
In Microsoft Visual Express is there a way to export the SQLite database I've created through the Express interface into a code format so I can have it generated on first install by my customers?
I'd like to take the easiest way to do this without having to manually prepare all the code structure.
I was unable to find any sort of export feature. Any advice?
This resource will help me execute the code once I have it prepared, but I've 12 tables and some of them should come pre-populated, so being able to have the batch code will help.
If it's just an SQLite database you could just publish the file with the rest of your program since it's a normal file without any dependencies.
I have one store procedure in my database, it makes many things and print results when i run it on sqlserver management studio (ssms), for example:
exporting table abc...
exporting table def...
deleting table temp...
My program has to run the store procedure and show the output anywhere (maybe creating a log file), such as ssms does (it shows output in the messages tab). I have to show exactly the same ssms shows. How can I do this?? sqlcmd, ado.net?? I see this question
How to run sql from a .net application against sqlserver and get output like with SQL Management Studio?
but, answer is not clear... Help!!
If you want to capture warning and information messages, you will want to create a SqlInfoMessageEventHandler delegate to handle the SqlConnection.InfoMessage event. See references below for details.
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.infomessage.aspx
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlinfomessageeventhandler.aspx
I have an SSIS package that copies the data in a table from one SQL Server 2005 to another SQL Server 2005. I do this with a "Data Flow" task. In the package config file I expose the destination table name.
Problem is when I change the destination table name in the config file (via notepad) I get the following error "vs_needsnewmetadata". I think I understand the problem... the destination table column mapping is fixed when I first set up the package.
Question: what's the easiest way to do the above with an ssis package?
I've read online about setting up the metadata programmatically and all but I'd like to avoid this. Also I wrote a C# console app that does everything just fine... all tables etc are specified in the app.config ... but apparently this solution isn't good enough.
Have you set DelayValidation to False on the Data Source Destination properties? If not, try that.
Edit: Of course that should be DelayValidation to True, so it just goes ahead and tries rather than checking. Also, instead of altering your package in Notepad, why not put the table name in a variable, put the variable into an Expression on the destination, then expose the variable in a .DtsConfig configuration file? Then you can change that without danger.
Matching source destination column with case sensitive has done the work for me.
Like in my case SrNo_prod was column in dev and using it we developed the dtsx, while it is been created as SrNo_Prod in prod, after making case change from P to p, we got successful execution of package.
Check if the new destination table has the same columns as the old one.
I believe the error occurs if the columns are different, and the destination can no longer map its input columns to the table columns. If two tables have the same schema, this error should not occur.
If all you are doing is copying data from one SQL2005 server to another I would just create a Linked Server and use a stored proc to copy the data. An SSIS package is overkill.
How to Create linked server
Once the linked server is created you would just program something like...
INSERT INTO server1.dbo.database1.table1(id,name)
SELECT id, name FROM server2.dbo.database1.table1
As far the SSIS package I have always had to reopen and rebuild the package so that the meta data gets updated when modifying the tables column properties.