1 Replies - 3293 Views - Last Post: 27 November 2012 - 11:51 PM Rate Topic: -----

#1 AlexxanderX  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 13-January 12

Problem Tabel not created

Posted 27 November 2012 - 06:00 AM

Hello guys. I have a problem:
Dim con As New SqlCeConnection
        con.ConnectionString = "Data Source='|DataDirectory|\Database.sdf';Password='1997526alex'"
        If con.State = ConnectionState.Closed Then
        End If
            Dim cmd As SqlCeCommand = con.CreateCommand()
            Dim sql As String = "create table CoolPeople (" _
            + "LastName nvarchar (40) not null, " _
            + "FirstName nvarchar (40), " _
            + "URL nvarchar (256) )"

            cmd = New SqlCeCommand(sql, con)
            MsgBox("Table created.")
        Catch sqlexception As SqlCeException
            MessageBox.Show(sqlexception.Message, "Oh Crap." _
            , MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Oh Crap." _
            , MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

When I click it says "Table created" but when i verify nothing. If I repress it sayd was created. Why is not created?

Is This A Good Question/Topic? 0
  • +

Replies To: Problem Tabel not created

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: Problem Tabel not created

Posted 27 November 2012 - 11:51 PM

Are you positive your connection string is accurate? Next let's take a look at this

 Dim cmd As SqlCeCommand = con.CreateCommand()

Why say that is equal to con.createcommand if you, shortly after, set it's value to that of the new sqlcommand?

Furthermore let's look at that cmd string. You don't want to use the + operator on strings. You can't add a string together. You can concatenate strings together but you can't physically add them. So it's best you use the & operator instead. I see a space at
create table CoolPeople (

You'll want to be very clear with your statements. I've known SQL statements to not work due to extra spaces like that, not sure why, but it happens. So let's change that into a

create table CoolPeople(

Now let's examine the rest of your sql command statement.

Dim sql As String = "create table CoolPeople(" _
	            + "LastName nvarchar (40) not null, " _
	            + "FirstName nvarchar (40), " _
	            + "URL nvarchar (256) )"

There are three issues I see here. Those spaces after you've specified the type of the column.

LastName nvarchar (40) not null
FirstName nvarchar (40)
URL nvarchar (256) )

This should definitely be something more like this.

LastName nvarchar(40) NOT NULL
FirstName nvarchar(40)
URL nvarchar(256) )

Now the NOT NULL constraints on your column identifiers isn't required to be capitalized as SQL isn't like that, but it's very good practice, and will help you create understandable, readable sql statements that you can debug if anything goes wrong.

If there are any other issues it is because of your connection string, I assure you. That or there is a problem with your instance of the sql server. Let me know how this works out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1