Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 1st, 2008, 07:08 PM
Authorized User
 
Join Date: Jul 2007
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
 
Old July 1st, 2008, 08:13 PM
Friend of Wrox
 
Join Date: Jun 2008
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???
 
Old July 2nd, 2008, 11:28 AM
Authorized User
 
Join Date: Jul 2007
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





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





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