Prepare code to programmatically create SQLite database - exporting template - c#

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.

Related

Use SMO to locate correct backup path

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.

How to add a column to a database table using Subsonic?

I am trying to add a column to a database table in an application that is using SubSonic 2.0.3.0 as the generator for the database layer.
I just cant get the hang of it. I tried to manually update the generated file of the table but it doesn't work. I have been trying to get the subsonic files regenerated but i cant find the sonic.exe tool that's needed? Can anyone help me to understand this?
You can download the latest 2.0 version of the project from https://github.com/subsonic/SubSonic-2.0, after you build it the sonic.exe will be in SubSonic\SubCommander\bin\Debug.
Then I've just got a little script file
del /q "DAL\*.*"
"..\..\ExternalResources\lib\sonic.exe" generate /out "DAL"
pause
Is that enough to get you on track?

Load and save .sdf at runtime

I'm building an C#-application that uses an SQL Server Compact 4.0 database (.sdf) with Entity Framework for data. I want to be able to load/save-files from within this application so that the user can load a different database or backup the database to an USB eg.
I know you can create an sdf in code, but how can i load it at runtime (The connectionstring)?
My question is that this must be a common thing to do, what is the best way to go about it? is there any guides out there to do this?
or do you reccomend another way to go about my problem?
//ObjectiveCoder
You should use a SqlCeConnectionStringBuilder to create a connection string containing your file path.

.CSV to SQL CE Table?

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.

Programmatically Repair SQLite Database

I have a need to try and repair a SQLite database from a .NET program if the database file gets corrupted. I have found several sites such as Fix SQLite and in the FAQ it describes that you can:
Depending how badly your database is corrupted, you may be able to recover some of the data by using the CLI to dump the schema and contents to a file and then recreate.
Does anyone know of a way to repair a SQLite database programmatically in .NET?
You might consider implementing your own strategy for database recovery. You could store backups of the SQLite file and then check that it is OK using:
PRAGMA integrity_check;
If errors are found then you can revert to a backup.
You are overlooking one important word: you can 'recover some data', this is not a repair!
If there is a sitatuation where a corrupted database could be repaired perfectly without user-interaction than it would not be corrupted in the first place and such an repair would have been a standard function of SQLite

Categories

Resources