Re: Programatically creating a SQL database if it doesn't exist.
From: Matt (no_at_no.com)
Date: 03/22/04
- Next message: NM: "Re: Thread and TabControl"
- Previous message: CJ Taylor: "Re: Array: "Cannot Convert to interface IList" - Why?"
- In reply to: Jay B. Harlow [MVP - Outlook]: "Re: Programatically creating a SQL database if it doesn't exist."
- Next in thread: Francisco: "Re: Programatically creating a SQL database if it doesn't exist."
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 22 Mar 2004 10:49:05 -0500
That's a great idea, I don't know why I didn't think of that. :)
I'll take this idea and look at Peter's code and give it go. Sure
it'll work now.
Thanks much to everyone that offered some help (I'll also look up the
adonet group, didn't see when I looked last time)
M
On Mon, 22 Mar 2004 09:15:41 -0600, "Jay B. Harlow [MVP - Outlook]"
<Jay_Harlow_MVP@msn.com> wrote:
>Matt,
>In addition to the other comments.
>
>I would not expect the connect to work as you do not have the database yet
>(chicken & egg problem).
>
>I would try to simply connect to a known database first "master","model",
>"tempdb" come to mind.
>
>Then execute your statement.
>
>After executing your statement you can then change to your database.
>
>Something like:
>
> Dim connectionString As String = "UID=sa;PWD=pass;" & _
> "Initial Catalog=master;Data Source=localhost;"
>
> Dim sql As String = "CREATE DATABASE mydb ON PRIMARY" & _
> "(Name=test_data, filename = 'C:\mydb_data.mdf', size=3," & _
> "maxsize=5, filegrowth=10%)log on" & _
> "(name=mydbb_log, filename='C:\mydb_log.ldf',size=3," & _
> "maxsize=20,filegrowth=1)"
>
> Dim connection As New SqlClient.SqlConnection(connectionString)
> connection.Open()
> Dim cmd As New SqlClient.SqlCommand(sql, connection)
> cmd.ExecuteNonQuery()
> connection.ChangeDatabase("mydb")
>
> connection.Close()
>
>Of course I would probably have two connection strings or modify a single
>connection string, one to create the database and a second to actually
>connect & use it.
>
>Note: the SqlConnection.ChangeDatabase does a "USE mydb", so the connection
>above is left using the new database after it is created.
>
>Hope this helps
>Jay
>
>"Matt" <no@no.com> wrote in message
>news:snus50p6rkehl1p0issddf6ujh26k0vd85@4ax.com...
>> I need to be able to test an already started MSDE/SQL server for a
>> specific database. If that database doesn't exist, I want to create
>> it.
>>
>> I have the following statement someone provided on site, but if I
>> can't connect to the database, how can I execute the sql command?
>>
>>
>> Dim sql As String = "CREATE DATABASE mydb ON PRIMARY" +
>> "(Name=test_data, filename = 'C:\mysql\mydb_data.mdf', size=3," +
>> "maxsize=5, filegrowth=10%)log on" + "(name=mydbb_log,
>> filename='C:\mysql\mydb_log.ldf',size=3," + "maxsize=20,filegrowth=1)"
>>
>> If I try to connect with the following string, it fails and I can't
>> execute the sql string above, of course.
>>
>>
>> ConnectionString = "UID=sa;PWD=pass;" + "Initial Catalog=mydb;" +
>> "Data Source=localhost;"
>>
>>
>>
>> Thanks much for any help you can offer.
>
- Next message: NM: "Re: Thread and TabControl"
- Previous message: CJ Taylor: "Re: Array: "Cannot Convert to interface IList" - Why?"
- In reply to: Jay B. Harlow [MVP - Outlook]: "Re: Programatically creating a SQL database if it doesn't exist."
- Next in thread: Francisco: "Re: Programatically creating a SQL database if it doesn't exist."
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|