Developing SQL Server CE based Pocket PC Applications using Visual Studio.NET - Creating an SQL Server CE database programmatically
(Page 5 of 5 )
Now, it is time to create an SQL Server CE database programmatically (or dynamically).
To work with SQL Server CE, we need to import two namespaces (which are to be referred as shown in previous sections), namely "System.Data.Common" and "System.Data.SqlServerCe."
To create (or manage) an SQL Server CE database, we need to work with the class "System.Data.SqlServerCe.SqlCeEngine." We need to create an object out of it and call a method, "CreateDatabase," which simply creates a database based on the connection string we pass (along with an optional password). It is always a good idea to "dispose" of the "SqlCeEngine" once it completes its job.
Putting it all together, you can modify your code in the previous example in such a way that it looks like the following:
Imports System.Data.Common
Imports System.Data.SqlServerCe
Public Class Form1
Inherits System.Windows.Forms.Form
Friend WithEvents Button1 As System.Windows.Forms.Button
.
.
.
.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim connStr As String
connStr = "Data Source = emp.sdf; Password = jagchat"
Dim engine As SqlCeEngine
engine = New SqlCeEngine(connStr)
engine.CreateDatabase()
engine.Dispose()
MessageBox.Show("Successfully created")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
Execute the above code by pressing F5. Once you click on "Button1," you should be able to see a message box showing "Successfully created." You can check the file "emp.sdf" in your root directory (or in "My Device" as specified in the previous section) as shown in the following image (fig12).

You can try clicking on the file (emp.sdf) to open it using Query Analyzer. It will give the message "Invalid Password" and ask for the password. Once you provide the password (in this case it is "jagchat"), it will open the database (which is generally empty) and show something like the following (Fig13).

I developed the application using Microsoft Windows Server 2003 Standard Edition with Microsoft Visual Studio.NET 2003 Enterprise Architect and Pocket PC SDK 2003 (along with additional emulators downloaded). The entire solution for this article is freely available in the form of a zip downloadable.
In my upcoming articles, I shall concentrate further on developing advanced Pocket PC applications. So, keep close tabs on this website :)
Any comments, suggestions, feedback, bugs, errors, enhancements are highly appreciated at jag_chat@yahoo.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |