Set up dynamically database

I have  asp.net project . What I need
1) Create Data Base dynamically from ASP.NET  application
2) generate connection string  for this database

how to do it  ?
YampolkaValeriya YampolskaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
One of the ways to do so is by using T-SQL. You need to connect to the server in the first place though. Connect to the server as an admin (use 'sa' account, or any other DB Admin account) and connect to the master database. Then, just use CREATE DATABASE command. Please note however, that you need to have a basic idea of the DB server Disk Drives and folders. Perhaps your DB admin has specified that the default data location is for example D:\Data, and the Transaction Log location is D:\Logs. The syntax may look complicated by basically all you need is this:
USE master;
GO
CREATE DATABASE Sales
ON 
( NAME = Sales_dat,
    FILENAME = 'D:\DATA\saledat.mdf',
    SIZE = 10,
    MAXSIZE = 50,
    FILEGROWTH = 5 )
LOG ON
( NAME = Sales_log,
    FILENAME = 'D:\Logs\salelog.ldf',
    SIZE = 5MB,
    MAXSIZE = 25MB,
    FILEGROWTH = 5MB ) ;
GO

Open in new window

In fact, according to the link above you can even create the database like this:
USE master;
GO
CREATE DATABASE mytest;
GO

Open in new window


The following example creates the database mytest and creates a corresponding primary and transaction log file. Because the statement has no <filespec> items, the primary database file is the size of the model database primary file. The transaction log is set to the larger of these values: 512KB or 25% the size of the primary data file. Because MAXSIZE is not specified, the files can grow to fill all available disk space.
When the database is created you will need to disconnect from the master database. You will have to reconnect to the newly created database. The connection string to this new database will be the same, except for the "Database=" parameter, i.e.:
Provider=SQLNCLI11;Server=myServerAddress;Database=myDataBase;Uid=myUsername;
Pwd=myPassword;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
YampolkaValeriya YampolskaAuthor Commented:
When I do it from SQL Managemet Studio - I am Fine
But when I do it from asp.net application ,
 I have  CREATE DATABASE permission denied in database 'master'.
0
YampolkaValeriya YampolskaAuthor Commented:
This could be resolved with  Server Authentication   and admin login
0
YampolkaValeriya YampolskaAuthor Commented:
Solution could be implemented if
1) DB install server with mixed mode
2) You know Admin Password
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.