DataBase Creation

Started by sajiv, Jan 20, 2009, 11:55 AM

Previous topic - Next topic

sajiv


In Windows Explorer, create a new Directory called C:\Temp
Create an empty Windows forms application. Name it Project1. Exit the IDE and a dialogue box will ask if you want to save or discard  the project.  Select save and specificy C:\temp as the directory.

Next we need a reference to the SQLServerCe namespace.

Select Main Menu | Project | Properties | references | Add | .Net Tab


Select System.Data.SqlServerCe. Depress OK.

In the Solution Explorer Select Form1. Select the second Icon from the right in the
row of icons immediately above the solution explorer. You will be seeing the source
of form1. Enter Control-A and then depress the delete key. Cut and paste the code below:

Imports System.Data               ' Import Namespaces

Imports System.Data.SqlServerCe

Public Class Form1

Private Sub Form1_Load(ByVal sender As Object, _

                           ByVal e As System.EventArgs) Handles Me.Load

   'Create a connection string constant

   Const ConString As String = "DATASOURCE=C:\ADatabase.sdf"

   'Instantiate a database engine using the connection string

   Dim Engine As New SqlCeEngine(ConString)

   'Create the database and dispose of the engine

   Try

       Engine.CreateDatabase()

   catch ex as exception     

        MessageBox.Show(ex.Message, "Failed to create new database.", _

                        MessageBoxButtons.OK, MessageBoxIcon.Error)

   Finally

       Engine.Dispose()

   End Try



   'Instantiate a connection

   Dim con As SqlCeConnection = New SqlCeConnection(ConString)

   'Create the T-SQL command string

   Dim PeopleCmd As String = _

   "Create Table People (RecordNo int PRIMARY KEY,FirstName nvarchar(20)," + _

   "Lastname nvarchar(20),Sex nvarchar(7),City nvarchar(20)," + _
   "State nvarchar(20));"

   'Instantiate an SQLCE command

    Dim Command As New SqlCeCommand

    'Fill in the command and the connection

    Command.CommandText = PeopleCmd : Command.Connection = con

    Try

       con.Open() ' Open the  connection

       Command.ExecuteNonQuery()    ' Execute the Create Table command

     Catch ex As Exception

        MessageBox.Show(ex.Message, "Failed to create new People Table.", _

                        MessageBoxButtons.OK, MessageBoxIcon.Error)

     Finally

            con.Close() : con.Dispose() ' Close the connection and dispose of

     End Try                            ' the connection

   End Sub

End Class

It's a little strange to do database creation first but we need a database to proceed and we aren't
going to become tangled up with the designer.

Caveats: The code creates a database. If there is one already,  an exception will occur.

Source:Msdn

dwarakesh

Hi sajiv

  Thanks for coding .. I tried the code .. its working...


   The code creates a database. If there is one already,  an exception is occuring... How to fix that error... can u explain..

kavee

thanks for the progrm this s the one i searchin