Results 1 to 7 of 7

Thread: create database with sql script

  1. #1

    Thread Starter
    Lively Member Edilson's Avatar
    Join Date
    Aug 2000
    Location
    Orlando
    Posts
    81

    create database with sql script

    I'm new to VB.net !!
    My question is : How do I create a new database using sql script generated by Sql Server 2000 using vb.net code ?

  2. #2
    Lively Member
    Join Date
    Jan 2000
    Location
    treehouse
    Posts
    106
    Try this:

    'SET THE CONNECTION OBJECT
    Dim _connectionstring as string = "server=SERVERNAME;database=DATABASENAME;uid=USERID;pwd=PASSWORD"

    'SET THE SQL QUERY
    Dim Sql as string = "CREATE DATABASE DATABASENAME"

    'CREATE THE CONNECTION OBJECT
    Dim dbConn As New SqlClient.SqlConnection(_connectionstring)
    'CREATE THE COMMAND OBJECT
    Dim Command As New SqlClient.SqlCommand(Sql, dbConn)

    Command.CommandTimeout = 120
    Command.Connection.Open()
    Command.ExecuteNonQuery()

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    I find that an interesting approach since, there isn't a DB to connect to.....Which means you'd have to connect to the master db....for which the user had better have access to....
    That said.... I know you could also use SQLDMO (which is a COM component) to do it via code OR SQL Script.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    You can use the process class and run the script from the command line.

  5. #5

    Thread Starter
    Lively Member Edilson's Avatar
    Join Date
    Aug 2000
    Location
    Orlando
    Posts
    81
    what I'm looking for is something to create database and its tables,procedures,users...
    using the script generated by SQL Server.
    similar to the line command "osql".

  6. #6
    Lively Member
    Join Date
    Jan 2000
    Location
    treehouse
    Posts
    106
    Sorry about that. Try this code. It executes scripts through osql.




    Dim proc As New Process()
    Dim sCmdLine As String
    Dim sDatabase as String = "master"
    Dim _password as String = "put your password here"
    Dim _server as String = "Put Server Name here"
    Dim sFileName as String = "Put Name of .sql file here"
    Dim _path as String = "Put location of the file here"


    proc.StartInfo.FileName = "osql"
    sCmdLine += " -Usa "
    sCmdLine += "-d" & sDatabase & " "
    sCmdLine += "-P" & _password & " "
    sCmdLine += "-S" & _server & " "
    sCmdLine += "-i" & sFileName

    proc.StartInfo.WorkingDirectory = _path
    proc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
    proc.StartInfo.Arguments = sCmdLine

    proc.StartInfo.UseShellExecute = True

    proc.Start()

    proc.WaitForExit()

    If (Not proc.HasExited) Then
    proc.Kill()
    End If

  7. #7

    Thread Starter
    Lively Member Edilson's Avatar
    Join Date
    Aug 2000
    Location
    Orlando
    Posts
    81
    Thanks bontyboy !!!

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