Programmatically create MS SQL Compact database file on a desktop
Is it possible to create an SDF database file for MS SQL Server Compact on a desktop.?
I want to craete a desktop application which can create an SDF database file for copying it to a Windows Mobile device.
Is it possible and how?
thanks
Yes, this is possible. to quote from the MSDN page on SqlCeEngine.CreateDatabase:
if (File.Exists("Test.sdf"))
File.Delete("Test.sdf");
string connStr = "Data Source = Test.sdf; Password = <password>;";
SqlCeEngine engine = new SqlCeEngine(connStr);
engine.CreateDatabase();
engine.Dispose();
SqlCeConnection conn = null;
try
{
conn = new SqlCeConnection(connStr);
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "CREATE TABLE myTable (col1 int, col2 ntext)";
cmd.ExecuteNonQuery();
}
catch {}
finally
{
conn.Close();
}
After this, you can just copy Test.sdf onto the mobile device.
Hope this helps.
Create how?
From Visual Studio? Open the Server Explorer and add a new Data Connection. Select SQL Compact as the data source, give it a file name and click Create.
From Code? Use the SqlCeEngine class. Give it a connection string and call Create.
链接地址: http://www.djcxy.com/p/53648.html