Create Database and execute Sql script file

muratsekerci

New member
Joined
Mar 18, 2006
Messages
3
Programming Experience
5-10
Hi, everybody. Anybody know How I can create Sql DataBase and execute an Sql Script code by VB.net codes ? Thanks a lot ... (sorry for my kite english)
 
I want to create SQL server database and execute an script file programmatically...(without using SQL Server Management Studio)
 
Ok this is very basic example ... how to create a new DB programatically.
After you declare SQLclient objects needed for this example
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myConnection [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SqlConnection
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myCommand [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SqlCommand
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SqlDataReader[/SIZE]
[SIZE=2]
[/SIZE]just add this lines:
VB.NET:
[SIZE=2]myConnection = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlConnection("server=SERVERNAME;Initial Catalog=;User ID=;Password=;")
[/SIZE][SIZE=2]myConnection.Open()
myCommand = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlCommand("CREATE DATABASE myNewDB", myConnection)
myCommand.ExecuteNonQuery()
myConnection.Close()
then you can create tables and other stuff ...

HTH
Regards ;)[/SIZE]
 
Just one more thing...

Ok thanks. I create Sql Server Database wit your help.
Now, I have a script file like ("script.sql"). This script file includes all the informaion to create tables which I generate with Sql Enterprise manager.

for example, script file includes these lines:
-----------------------------------------------
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[domain]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[domain]
GO

CREATE TABLE [dbo].[domain] (
[Domain_id] [int] IDENTITY (1, 1) NOT NULL ,
[Müşteri_id] [int] NOT NULL ,
[Alan_adı] [char] (30) COLLATE Turkish_CI_AS NULL ,
[Başlangıç] [smalldatetime] NULL ,
[Bitiş] [smalldatetime] NULL ,
[Fiyat] [real] NULL ,
[Fiyat_Cinsi] [char] (5) COLLATE Turkish_CI_AS NULL ,
[Register] [char] (30) COLLATE Turkish_CI_AS NULL ,
[Kull_adı] [char] (20) COLLATE Turkish_CI_AS NULL ,
[Şifre] [char] (20) COLLATE Turkish_CI_AS NULL ,
[E_mail] [char] (50) COLLATE Turkish_CI_AS NULL ,
[Hosting] [char] (30) COLLATE Turkish_CI_AS NULL
) ON [PRIMARY]
GO

.. .. ..
.. .. ..
.. .. ..



-----------------------------------------------

How can I execute this script file programmatically ? (to create tables in MyNewDB)
 
Back
Top