Results 1 to 2 of 2

Thread: Change database during excute SQLCe

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2012
    Posts
    47

    Change database during excute SQLCe

    Hi
    I 'm creating a little Address Book on my PPC and i would like to do a button that I allow to change database so Address book for example
    I use the Database A and I want to change with B and work on It
    I created a form where I can create a new DB with a table (Name,LastName,Phone,Mail) and Load this new Database or If i have already created a database load it
    But when it did the instruction DataAdpater.Fill(Dataset,"NameTable") it gave me this Error --> " The table already exist(NameTable)" why?
    This is all the code

    Code:
    Imports System.IO
    Imports System.Data.SqlServerCe
    Imports System.Data
    Public Class Form2
    
        Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
            If TxtAgenda.Text = "" Then
                MsgBox("Inserire nome Agenda", MsgBoxStyle.OkOnly)
            Else
                If Not File.Exists("\My Documents\Agenda\" & TxtAgenda.Text & ".sdf") Then
                    Dim SQLEngine As New SqlCeEngine("Data source=\My Documents\Agenda\" & TxtAgenda.Text & ".sdf")
                    SQLEngine.CreateDatabase()
                    ' NomeDb = TextBox1.Text
    
                    ' mi connetto al db
                    stringc = "Data source=\My Documents\Agenda\" & TxtAgenda.Text & ".sdf"
                    Conn = New SqlCeConnection(stringc)
                    Conn.Open()
                    'Creazione di un tabella
    
                    sqL = "CREATE TABLE MiaTabbela (ID int IDENTITY Primary Key " + "NOT NULL,Nome nvarchar(50) NOT NULL" + ",Cognome nvarchar(50) NOT NULL" + ",Telefono nvarchar(10) NOT NULL" + ",Mail nvarchar(30) NOT NULL)"
                    Cmd = New SqlCeCommand(sqL, Conn)
                    Cmd.CommandType = CommandType.Text
                    Cmd.ExecuteNonQuery()
                    sqL = ""
                    MenuItem2.Enabled = True
                    MenuItem1.Enabled = False
                    MsgBox("ok")
    
                Else
    
                    MsgBox("Agenda giĆ  esiste eliminare?", MsgBoxStyle.YesNo, "Attenzione")
                    If vbYes Then
                        '  NomeDB = TextBox1.Text
                        MsgBox("\My Documents\Agenda\" & TxtAgenda.Text & ".sdf")
                        System.IO.File.Delete("\My Documents\Agenda\" & TxtAgenda.Text & ".sdf")
                        Dim SQLEngine As New SqlCeEngine("Data source=\My Documents\Agenda\" & TxtAgenda.Text & ".sdf")
                        SQLEngine.CreateDatabase()
                        ' NomeDb = TextBox1.Text
    
                        ' mi connetto al db
                        stringc = "Data source=\My Documents\Agenda\" & TxtAgenda.Text & ".sdf"
                        Conn = New SqlCeConnection(stringc)
                        Conn.Open()
                        'Creazione di un tabella all'interno del nuovo DB
    
                        sqL = "CREATE TABLE MiaTabella (ID int IDENTITY Primary Key " + "NOT NULL,Nome nvarchar(50) NOT NULL" + ",Cognome nvarchar(50) NOT NULL" + ",Telefono nvarchar(10) NOT NULL" + ",Mail nvarchar(30) NOT NULL)"
                        Cmd = New SqlCeCommand(sqL, Conn)
                        Cmd.CommandType = CommandType.Text
                        Cmd.ExecuteNonQuery()
                        sqL = ""
                        MenuItem2.Enabled = True
                        MenuItem1.Enabled = False
                        MsgBox("ok")
                    Else
                        MsgBox("Creazione annullata!!!!", MsgBoxStyle.OkOnly)
    
                    End If
    
                    ' ModifcaToF = True
    
                End If
            End If
    
        End Sub
    
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
            If Not DaCreato Then
                If Not File.Exists("\My Documents\Agenda\" & TxtAgenda.Text & ".sdf") Then
                    MsgBox("agenda non presente ")
                    MenuItem1.Enabled = True
                End If
            End If
    
            If Not Conn.State = ConnectionState.Open Then Conn.Open()
    
            Da = New SqlCeDataAdapter(Cmd)
            Cmb = New SqlCeCommandBuilder(Da)
            Ds = New Data.DataSet
            Da.Fill(Ds)
            Bs = New BindingSource(Ds)
            Try
                Principale.DatiDB()
            Catch Ex As Exception
                Principale.Enabled = False
            End Try
            Principale.Annulla.Enabled = False
            Principale.Salva.Enabled = False
            Principale.Show()
    
        End Sub
    End Class
    The variable Ds,Cmd,Cmb(command Builder),Conn,Bs,Da are all in common beacuse there are in Module
    If it could help when I load the program I do this
    Code:
    Public Sub LoadDB()
            Conn = New SqlCeConnection(stringc)
            Cmd = New SqlCeCommand("SELECT * FROM MiaTabella", Conn)
    
            If DebugON Then
                MsgBox("SELECT * FROM MiaTabella", MsgBoxStyle.OkOnly, "Caricamento DB")
    
            End If
    
            Da = New SqlCeDataAdapter(Cmd)
            Cmb = New SqlCeCommandBuilder(Da)
            Ds = New DataSet
            Panel2.Enabled = False
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            LoadDB()
            Da.Fill(Ds, "MiaTabella")
            Bs = New BindingSource(Ds, "MiaTabella")
            Try
                DatiDB()
            Catch Ex As Exception
                Panel1.Enabled = False
            End Try
            Annulla.Enabled = False
            Salva.Enabled = False
    
        End Sub
    When I load the first time the program the connection string is always the same "Data Source=\My Documents\Agenda\Prova1.sdf"
    I change it when I create a Db or when I try to load a db
    Thanks

  2. #2

    Thread Starter
    Member
    Join Date
    Mar 2012
    Posts
    47

    Re: Change database during excute SQLCe

    OK I don't resolve the problem but I know why I wll do another questions in another thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width