In my project i want to create a database through my application, how can create new database using sql statement.( i.e create database...)

Dani AI

Generated

Short practical summary referencing the thread: SQL Server databases are created with the T-SQL CREATE DATABASE statement, but that statement must be executed against a running SQL Server instance (typically connected to the master database). The statement can either let the server place files in the instance default location or explicitly set data/log file paths with the FILENAME option. As and noted, an active connection to an existing server is required to run the CREATE DATABASE command. (learn.microsoft.com)

Example — create with explicit file paths (folder must already exist):

CREATE DATABASE MyAppDB
ON PRIMARY (
  NAME = MyAppDB_Data,
  FILENAME = 'C:\SQLData\MyAppDB.mdf',
  SIZE = 50MB,
  FILEGROWTH = 10MB
)
LOG ON (
  NAME = MyAppDB_Log,
  FILENAME = 'C:\SQLLogs\MyAppDB.ldf',
  SIZE = 20MB,
  FILEGROWTH = 10%
);

Specifying a path like the example above requires that the directory exists and that the SQL Server service account has write access; otherwise the CREATE will fail. If no filespec is supplied, SQL Server uses its default data/log locations (these can be viewed/changed in Server Properties → Database Settings or by advanced registry methods). (learn.microsoft.com)

Programmatic options mentioned in the thread: execute the CREATE DATABASE DDL from application code by opening a connection to the instance (set Database=master in the connection string) and running ExecuteNonQuery, or use SMO to build Database objects and call Create(). The Troubleshooting and SMO guides show working examples and important notes (for example: pre-create directories, handle exceptions, and avoid embedding highly privileged credentials). (learn.microsoft.com)

Permissions and safety reminders: creating a DB requires the proper server-level privilege (CREATE DATABASE / CREATE ANY DATABASE or membership in sysadmin/dbcreator roles), so ’s tip about using an sa-level login is functionally correct but insecure in production — prefer integrated security or a least-privileged login (or grant CREATE DATABASE explicitly). Example grant shown in the docs: USE master; GRANT CREATE DATABASE TO [Fay];. Also ensure the SQL Server service account (per-service SID such as NT SERVICE\MSSQLSERVER) has filesystem permissions for any custom folders. (learn.microsoft.com)

Recommended Answers

All 9 Replies

Thanks, but I have more. How to set the filepath for that database?

Can you be a bit clearer please

Do you want to know how create a DB using a language like C# or Java in Visual Studios rather than straight in SQL Server Management Studios, if so what language are you using?

Can you be a bit clearer please

Do you want to know how create a DB using a language like C# or Java in Visual Studios rather than straight in SQL Server Management Studios, if so what language are you using?

What database server are you using? Most of them don't let you change the path, it's established when the database server is installed. And after thinking about it I don't think you can create a new database via programming languages because the language has to have a connection to an existing database in order to run sql commands.

@Ancient Dragon, I think you're right about not being able to establish a DB through languages like C#, i tried it a while ago (I wanted to open and run an SQL file with a DB creation script in when a program is run for the first time) but there had to be a connection to a server already established before the script to create the DB could be run.

If you have an sa account you can login to master db and create a new one.

They can also be created using stored procedures, assuming you have the permissions to do that.

Hi Dear;
How to create database in sql server ;
Sql is the Structured querie Language ... & the queries command is......

create database databasename;

this is the database creation command ;
regards
prem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.