SQL Server CE Programming with Pocket PC and Visual Studio.NET 2003: Databases and Tables - Creating SQL Server CE databases and tables in Pocket PC programmatically: source code
(Page 2 of 4 )
Make sure that you have completed all the steps in the previous section before proceeding further. Go through the following steps to write the code.
- Double click on the “Create Table” button to go to the code window.
- Include the following two lines at the top:
Imports System.Data.Common
Imports System.Data.SqlServerCe
- Just before the “btnCreateTable_click” event, declare a variable to hold the connection string (as follows):
Dim connStr As String = "Data Source = emp.sdf; Password = jagchat"
- You can modify the connection string according to your requirements. Within the button click event, copy the code in such a way that it looks like this:
Imports System.Data.Common
Imports System.Data.SqlServerCe
Public Class Form1
Inherits System.Windows.Forms.Form
.
.
.
.
Dim connStr As String = "Data Source = emp.sdf; Password = jagchat"
Private Sub btnCreateTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateTable.Click
Try
'creating database
Dim engine As SqlCeEngine
engine = New SqlCeEngine(connStr)
engine.CreateDatabase()
engine.Dispose()
'creating table
Dim cn As New SqlCeConnection(connStr)
Dim sql As String
sql = "CREATE TABLE emp "
sql &= "("
sql &= " empno int PRIMARY KEY,"
sql &= " ename nvarchar(20),"
sql &= " sal float,"
sql &= " deptno int"
sql &= ")"
Dim cmd As New SqlCeCommand(sql, cn)
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
cmd.Dispose()
cn.Dispose()
MessageBox.Show("Succesfully created")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
In the next section, we shall see how to execute the above application.
Next: Creating SQL Server CE databases and tables on Pocket PC programmatically: deployment >>
More MS SQL Server Articles
More By Jagadish Chaterjee