Programming Creating SQL Server Database

xiaoxiao2021-03-06  50

Steps to create a SQL Server database

New Visual C # .NET Windows application. Place a button on Form1. Change the Name property of the button to BTNCREATEDATABASE to change the Text property to Create Database. Use the USING statement for System and System.Data namespace, so that you don't need to limit these namespaces in your code. Add the following code to the "General Declarations" section of Form1: use system;

Using system.data.sqlclient; switched back to Forms view, then double-click Create Database to add a Click event handler. Add the following code to the handler: String STR;

SqlConnection myconn = new SqlConnection ("Server = localhost; integrated security = sspi; database = master");

Str = "CREATE DATABASE MyDatabase on Primary"

"(Name = mydatabase_data,"

"Filename = 'c: //mydatabaseData.mdf',"

"Size = 2MB, MaxSize = 10MB, FileGrowth = 10%)"

"Log on (name = mydatabase_log,"

"Filename = 'c: //mydatabaseelog.ldf',"

"SIZE = 1MB,"

"Maxsize = 5MB,"

"FileGrowth = 10%)";

Sqlcommand mycommand = new sqlcommand (str, myconn);

Try

{

Myconn.open ();

mycommand.executenonquery ();

MessageBox.Show ("Database IS Created Success," MyProgram ", MessageBoxButtons.ok, MessageBoxicon.information;

}

Catch (System.exception EX)

{

MessageBox.show (ex. Tnowtring (), "MyProgram", MessageBoxButtons.ok, MessageBoxicon.information;

}

Finally

{

IF (MyConn.State == ConnectionState.Open)

{

MyConn.close ();

}

} Change the connection string to point to your SQL Server computer, and make sure the Database parameter is set to Master or empty. Press F5 or Ctrl F5 to run the project, and then click Create Database. Use the Server Explorer to verify the database creation.

Back to top

Remarks:

转载请注明原文地址:https://www.9cbs.com/read-116885.html

New Post(0)