1

I am creating a SQL Server Compact Edition database using C# code in a console application. Here is my code for creating database:

string con;

string fileName = "MiniProfilerData.sdf";
string password = "arcanecode";

if (File.Exists(fileName))
{
    File.Delete(fileName);
}

con = string.Format("DataSource=\"{0}\"; Password='{1}'", fileName, password);
SqlCeEngine en = new SqlCeEngine(con);
en.CreateDatabase();

This code generates the MiniProfilerData.sdf file

Now I want to create tables in that .sdf file. I have a table creation script which will generates tables, here is the script Click Here

How can I generate tables using that TableCreationScript in my MiniProfilerData.sdf file?

Any idea?

4
  • 1
    What's the issue you are having? Any errors? Or do you just need to know how to run that SQL? Commented Nov 28, 2014 at 5:41
  • I want to know, how to run that script using c# code. Commented Nov 28, 2014 at 5:49
  • dotnetperls.com/sqlclient Commented Nov 28, 2014 at 7:26
  • I know this. If I place this script in one file say "Script.sql". Then "How can I call/execute this sql file from my application?" Commented Nov 28, 2014 at 8:13

1 Answer 1

1

You need to split the script into individual CREATE statements, the execute each (pseudo-code):

var scripts = TableCreationScript
    .Trim()
    .Replace("create", "#create")
    .Split(new []{'#'}, StringSplitOptions.RemoveEmptyEntries);

for each (var script in scripts)
{
    SqlCeCommand cmd = new SqlCeCommand(script);
    cmd.Connection = connection;
    cmd.ExecuteNonQuery();
}
Sign up to request clarification or add additional context in comments.

4 Comments

Can you please tell me how to split script into individual CREATE statements?
Thank you! It works for me. One more question: It gives me an error on clustered/non clustered index syntax. Can you explain me why so? Also it gives me an error: Parsing error on nvarchar(max).
You have to remove the non clustered/clustered statements, SQL CE does not like them. and nvarchar(MAX) => ntext
Yes, I have already removed it from script. I just wanted to know. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.