|
-
Jun 11th, 2003, 11:37 AM
#1
Thread Starter
Lively Member
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 ?
-
Jun 11th, 2003, 03:11 PM
#2
Lively Member
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()
-
Jun 11th, 2003, 03:35 PM
#3
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.
-
Jun 11th, 2003, 04:51 PM
#4
Frenzied Member
You can use the process class and run the script from the command line.
-
Jun 11th, 2003, 05:49 PM
#5
Thread Starter
Lively Member
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".
-
Jun 12th, 2003, 06:27 AM
#6
Lively Member
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
-
Jun 12th, 2003, 11:19 PM
#7
Thread Starter
Lively Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|