Link to home
Start Free TrialLog in
Avatar of ackid32
ackid32

asked on

How to create a database in a different location

How to create a database in a different location in SQL-Server.
Like doing
 createdatabase("c:\bla\abc\xyz.mdb")

i want my database is stored where i want in sql-server.
Avatar of Shivshankar
Shivshankar

SQL Server is not like an access database. You cannot specify a path for a database like this.

Yes, if you have sa rights, then you can create database. But, creating databases dynamically like this in an application is not generally recommended. You must understand that SQL Server has to build a host of tables (sys*) for any given database and statistics and so many stuff.

Best Regards.
Avatar of ackid32

ASKER

Any second opinion?
Avatar of Éric Moreau
You can use the FileName parameter. Check this sample:

USE master
GO
CREATE DATABASE Sales
ON
( NAME = Sales_dat,
   FILENAME = 'c:\program files\microsoft sql server\mssql\data\saledat.mdf',
   SIZE = 10,
   MAXSIZE = 50,
   FILEGROWTH = 5 )
LOG ON
( NAME = 'Sales_log',
   FILENAME = 'c:\program files\microsoft sql server\mssql\data\salelog.ldf',
   SIZE = 5MB,
   MAXSIZE = 25MB,
   FILEGROWTH = 5MB )
GO
Note that the previous solution (which looks correct) requires that you be already connected to the database server. The data and log information is stored in the indicated files, but it cannot work without the server, too. It's not like in Access where you can simply copy the files to another computer and have them work.

My book "Visual Basic .NET Database Programming" (http://www.vb-helper.com/vbdb.htm) shows how to build SQL Server databases, make a program that can execute this kind of script, and a lot of other stuff.

As an aside, also note that you can use MSDE instead of SQL Server. It has roughly the same features but is free. There's an intro at: http://www.informit.com/content/index.asp?product_id={D31B236D-00C1-4A5A-8832-BC9A68E9D5B8}
make a connection to that SQL server where you want to make your database & then using the connection statement execute the following statement

g_Connection.execute "Create database databasename"
Avatar of ackid32

ASKER

dear emor

im using VB

and for that pls explain the code (where i put your code?)

Create an ADO connection to the Master database. Then use the Execute method to run the CREATE DATABASE statement.
Avatar of ackid32

ASKER

give me the VB Code pls
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial