Create an SQL Server database

Source: Internet
Author: User
Create an SQL Server database
1. Create a Visual C #. Net windows application.
2. Place a button on form1. Change the name attribute of the button to btncreatedatabase, and the text attribute to create database.
3. Use the using statement for the system and system. Data namespaces. In this way, you do not need to limit the declarations in these namespaces in the code. Add the following code to the "general declarations" section of form1:
4. Using system;
Using system. Data. sqlclient;
5. switch back to the form view, and double-click to create a database to add the click event handler. Add the following code to the handler:
6. String STR;
7. sqlconnection myconn = new sqlconnection ("Server = localhost; Integrated Security = sspi; database = Master ");
8.
9. Str = "create database mydatabase on primary" +
10. "(name = mydatabase_data," +
11. "filename = 'C: // mydatabasedata. MDF '," +
12. "size = 2 MB, maxsize = 10 MB, filegrowth = 10%)" +
13. "Log on (name = mydatabase_log," +
14. "filename = 'C: // mydatabaselog. ldf'," +
15. "size = 1 MB," +
16. "maxsize = 5 MB," +
17. "filegrowth = 10% )";
18.
19. sqlcommand mycommand = new sqlcommand (STR, myconn );
20. Try
21 .{
22. myconn. open ();
23. mycommand. executenonquery ();
24. MessageBox. Show ("database is created successfully", "myprogram", messageboxbuttons. OK, messageboxicon. information );
25 .}
26. Catch (system. Exception ex)
27 .{
28. MessageBox. Show (ex. tostring (), "myprogram", messageboxbuttons. OK, messageboxicon. information );
29 .}
30. Finally
31 .{
32. If (myconn. State = connectionstate. open)
33 .{
34. myconn. Close ();
35 .}
}
36. Change the connection string to point to your SQL Server computer and make sure that the database parameter is set to master or empty.
37. Press F5 or CTRL + F5 to run the project, and then click Create Database.
38. Use the server resource manager to verify database creation.
Back to Top
Note:
• This Code creates a custom database with specific attributes.
• Before running the code, the folder that stores the created. mdf and. ldf files must already exist. Otherwise, an exception is generated.
• To create a database similar to the SQL Server Model database and store it in the default location, change the str variable in the Code:
Str = "create database MyDatabase"
 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.