Adding databese on sql server from code

A can create database on sql server from command line with:

osql.exe -i -e script.sql (I have script for my database)

How to do that from code?

[166 byte] By [Gosovic] at [2007-12-16]
# 1
Hello,

You can save your script as a stored procedure and execute it with a command, such as SqlCommand.
This topic shows how to do execute the stored procedure from code:

How to: Execute a Stored Procedure that Returns No Value
http://msdn2.microsoft.com/library/ms171921(en-us,vs.80).aspx
hope this helps...

SteveS_MS at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
No, it doesn't help.

How to send script to SQL server as Stored Procedure? How to send it at all?
A can use SqlCommand.CommandText property to send but there is in-line input when script is multi-page cod (file 30-40 kb). First I must create program that parse script to in-line command?! Bullshit.

I heard for object SQL-DMO. Anyone knows how to use it?

Gosovic at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

You can read script from file:

First, you must edit original script. Remove all 'GO' words. For that you can use Replace option of editor.

Then in VB code:

Dim sr As System.IO.StreamReader
Dim fs As System.IO.FileStream
fs = New System.IO.FileStream("c:\script.sql", IO.FileMode.Open, IO.FileAccess.Read)
sr = New System.IO.StreamReader(fs)

Dim strComm As String = sr.ReadToEnd()

SqlCommand1.Connection = SqlConnection1
SqlCommand1.CommandText = strComm
SqlConnection1.Open()
SqlCommand1.ExecuteNonQuery()
SqlConnection1.Close()


For that examle you must have script with your program. Next question is how to import text file in program project to be included in final exe file?


Gosovic at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4
Gosovic wrote:

A can create database on sql server from command line with:

osql.exe -i -e script.sql (I have script for my database)

How to do that from code?



Dim MyProcess As New System.Diagnostics.Process

With MyProcess.StartInfo

.Arguments = "-i -e script.sql"

.FileName = "osql.exe"

.Verb = "Open"

End With

MyProcess.Start()


This code can be used to execute any native process on your computer!!!
This is just one solution...I believe you may want to consider (as Stated ) acually storing your script in the DB as a stored procedure and then using a command object to execute the procedure....***The filename property should include the complete path!!!

DMan1 at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic Language...