Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 1st, 2008, 07:08 PM
Authorized User
 
Join Date: Jul 2007
Location: Denver, CO, USA.
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default Creating a SQL Server DB through VB code

Hi,

I'm currently using VB2008 to access SQL Server 2005 instance on my machine. I'm trying to write code to create a new database and then create tables within the database.

I'm running into a strange problem right now, which is the fact that I was able to create a new database ok on either the regular SQL Server instance, or the SQLEXPRESS instance. However, when I attempted to create a table, I got an error message saying that there is already a table of the same name.

When I tried to connect to the new database via SQL Server Management Studio, I could see the database. However, there are no tables in it! So, theoretically, I'm creating a brand new blank DB, so there should not be anything in this DB, right? and I should be able to create any tables I want in it!

Here is the code that I have so far:

Module DBInitUtilities

    'Dim strConnect As String = "Data Source=D3J46DC1;Initial Catalog=DTEMDB;Integrated Security=True"
    'Dim strConnect As String = "Data Source=.\SQLEXPRESS;Initial Catalog=DTEMDB;Integrated Security=True"
    Dim strConnect As String = "Server=D3J46DC1;Integrated Security=True"
    Dim connSQL As SqlClient.SqlConnection = Nothing
    Dim cmdSQL As SqlClient.SqlCommand = Nothing

    Public Sub ExecuteSQLStmt(ByVal strSQL As String)
        '
        ' Execute a SQL statement
        '

        ' Open the connection
        If connSQL.State <> ConnectionState.Open Then
            connSQL.Open()
        End If

        'Define the SQL command
        cmdSQL = New SqlClient.SqlCommand(strSQL, connSQL)

        Try
            'Attempt to execute the SQL command
            cmdSQL.ExecuteNonQuery()

        Catch ae As SqlClient.SqlException
            MessageBox.Show(ae.Message.ToString())
        End Try

    End Sub 'ExecuteSQLStmt

    Public Sub CreateAllDBs()

        CreateDTDataDB()

    End Sub

    Public Sub CreateDTDataDB()

        ' Create a connection
        connSQL = New SqlClient.SqlConnection(strConnect)

        ' Open the connection
        If connSQL.State <> ConnectionState.Open Then
            connSQL.Open()
        End If

        Dim strSQL As String = "CREATE DATABASE DTData ON PRIMARY" + _
                               "(NAME = DTData, FILENAME = 'C:\dbs\DTData.mdf', SIZE = 2048KB," + "MAXSIZE = UNLIMITED, FILEGROWTH = 10%)LOG ON" + _
                               "(NAME = DTData_log, FILENAME = 'C:\dbs\DTData_log.ldf', SIZE = 1024KB," + "MAXSIZE = 4096KB, FILEGROWTH = 1%)"


        ExecuteSQLStmt(strSQL)

        CreateGroupsTable()
        CreateLocationHistoryTable()
        CreateLocationsTable()
        CreateSNDetailTable()

        MsgBox("New DB has been created!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Process Completed Successfully!")

    End Sub

    Public Sub CreateGroupsTable()

        Dim strSQL As String

        strSQL = "CREATE TABLE Groups" + _
                 "(GroupName NCHAR(64) CONSTRAINT PKeyGroupName PRIMARY KEY, " + _
                 "Parent NCHAR(64), " + _
                 "IcondIndex SMALLINT, " + _
                 "GroupImage IMAGE, " + _
                 "ImageStretchMode SMALLINT)"

        ExecuteSQLStmt(strSQL)

    End Sub

    Public Sub CreateLocationHistoryTable()

        Dim strSQL As String

        strSQL = "CREATE TABLE LocationHistory" + _
                 "(Location NCHAR(64), " + _
                 "Parent NCHAR(64), " + _
                 "CreationDate DATETIME, " + _
                 "LocParIDX BIGINT CONSTRAINT PKeyLocParIDX PRIMARY KEY, " + _
                 "SourceSN NCHAR(16))"

        ExecuteSQLStmt(strSQL)

    End Sub

    Public Sub CreateLocationsTable()

        Dim strSQL As String

        strSQL = "CREATE TABLE Locations" + _
                 "(Location NCHAR(64) CONSTRAINT PKeyLocation PRIMARY KEY, " + _
                 "Parent NCHAR(64), " + _
                 "DataType SMALLINT, " + _
                 "CurrentDataValue REAL, " + _
                 "SourceSN NCHAR(16), " + _
                 "AlarmStatus SMALLINT, " + _
                 "CurrentTimeStamp DATETIME, " + _
                 "CommStatus SMALLINT, " + _
                 "LocParIDX BIGINT, " + _
                 "Image_X FLOAT, " + _
                 "Image_Y FLOAT, " + _
                 "IconIndex SMALLINT, " + _
                 "BackgndColor BIGINT)"

        ExecuteSQLStmt(strSQL)

    End Sub

    Public Sub CreateSNDetailTable()

        Dim strSQL As String

        strSQL = "CREATE TABLE SNDetail" + _
                 "(SN NCHAR(16) CONSTRAINT PKeySN PRIMARY KEY, " + _
                 "TracerType SMALLINT, " + _
                 "CommStatus SMALLINT, " + _
                 "CurrentInterval SMALLINT, " + _
                 "Modulo SMALLINT, " + _
                 "InstallDate SMALLDATETIME, " + _
                 "LocPar1 BIGINT, " + _
                 "LocPar2 BIGINT, " + _
                 "LocPar3 BIGINT, " + _
                 "LocPar4 BIGINT)"

        ExecuteSQLStmt(strSQL)

    End Sub

End Module


I just have one simple form with a button on it. The click event on this button calls the CreateAllDBs() routine.

I'm not sure what I'm doing wrong here. I really appreciate any help on this. Thank you very much.

Khoi Nguyen
__________________
Khoi Nguyen
Reply With Quote
  #2 (permalink)  
Old July 1st, 2008, 08:13 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Never done this, but...

It seems to me that SQL Server would have NO IDEA which database to create those tables in!

You have created a database, yes (or at least I assume that part works), but then your connection is still to SQL Server, as a whole, an *NOT* to any specific database therein.

Perhaps you just need a
    USE [db name]
before doing the CREATE TABLEs??? Or perhaps you need to close the conneciton and reopen it specifically to the newly created DB???
Reply With Quote
  #3 (permalink)  
Old July 2nd, 2008, 11:28 AM
Authorized User
 
Join Date: Jul 2007
Location: Denver, CO, USA.
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Thank you for your help. I see it now... it was not apparent to me yesterday. I'll give it a try.

Khoi Nguyen
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
creating ssis packagte for sql server to sql serer Laxmikant_it ASP.NET 3.5 Professionals 0 November 26th, 2008 12:23 AM
Read SQL Server DB using .ASP-VB leafboy75 VB How-To 1 May 11th, 2007 12:08 AM
Accessing a SQL Server Express DB thru VB Express djelavic SQL Server 2005 2 February 21st, 2006 07:53 PM
access db to sql server db mikersantiago Classic ASP Basics 4 November 16th, 2004 03:33 AM
Unavailable download code for VB.Net & SQL Server bamboat_3 VB.NET 2 September 2nd, 2004 09:34 AM



All times are GMT -4. The time now is 12:44 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.