Results 1 to 7 of 7

Thread: [RESOLVED] Creat Database Programatically

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Pakistan
    Posts
    436

    Resolved [RESOLVED] Creat Database Programatically

    Hi Friends

    I have to create an auto installer, which create database at installation time, i created sql database script file and add in vb project. Now i want to read and execute this script file, is it possible? then how?, thanks in advance.

  2. #2
    Addicted Member spyk3's Avatar
    Join Date
    Apr 2009
    Location
    Dothan, AL
    Posts
    218

    Re: Creat Database Programatically

    I think you first read it's content in a string and then execute CommandText command.

    But this will work as long you don't have GO statement in your sql script. From your post it looks like that SqlCommand is not usefull because scripts that include DDL command are completed with batch finalizer GO command.

    I think what you're looking for is somthing like this.
    If you are using SQL 05 or greater you can use the SMO Lib.
    I'm not 100% on this as i dont have a need to use it, but i'm pretty positive it should be somthing like this: (Doesnt use batch command and osql connection)

    New Sql Script Code:
    1. Imports System.Data.SqlClient
    2. Imports System.IO
    3. Imports Microsoft.SqlServer.Management.Common
    4. Imports Microsoft.SqlServer.Management.Smo
    5.  
    6.   Public Sub someSub()
    7.             Dim sqlConnectionString As String = "Data Source=(local);Initial Catalog=Somthing;Integrated Security=True"
    8.             Dim file As New FileInfo("C:\myscript.sql")
    9.             Dim script As String = file.OpenText().ReadToEnd()
    10.             Dim conn As New SqlConnection(sqlConnectionString)
    11.             Dim server As New Server(New ServerConnection(conn))
    12.  
    13.             server.ConnectionContext.ExecuteNonQuery(script)
    14.         End Sub

    However, if you're script is of older style and has a ddl command you'll find that won't work do to GO initializer. Course I don't think you should have that issue.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Pakistan
    Posts
    436

    Re: Creat Database Programatically

    Thanks for reply,

    Actually i found a solution which will create database from text file, which is located some where in hard disk, but i want to read text file which is added in project.

    Thanks

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Creat Database Programatically

    Quote Originally Posted by zubairkhan View Post
    Actually i found a solution which will create database from text file, which is located some where in hard disk, but i want to read text file which is added in project.
    So is your issue resolved?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Pakistan
    Posts
    436

    Re: Creat Database Programatically

    Half part of my issued is solved, but i do need to read text file which i added in my project.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Pakistan
    Posts
    436

    Creat Database Programatically Solved

    Finally i got solution, i created a text file and added in solution resources and read from there.

  7. #7

    Re: Creat Database Programatically

    Since this is resolved, please mark the topic as resolved from the Thread Tools menu at the top of the page.

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