How to use ADO. NET and Visual C #. Net to create an SQL Server database programmatically

Source: Internet
Author: User
Tags microsoft website

This article describes how to use ADO. NET and Visual C #. Net to create a Microsoft SQL Server database step by step, because programmers usually need to create a database programmatically.

Go back to the top to create an SQL Server database

To create a database, follow these steps:

  1. Create a New Visual C #. Net windows application.
  2. PlaceButton. SetNameChange the property to btncreatedatabase, and thenTextThe property is changed to create database.
  3. PairSystemAndSystem. DataNamespace usageUsingIn this way, you do not need to limit the declarations in these namespaces in the subsequent code. Add the following code to the "general declarations" section of form1:
    using System;using System.Data.SqlClient;
  4. Switch to the form view, and double-clickCreate a databaseTo addClickEvent Handler. Add the following sample 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:\\MyDatabaseLog.ldf', " +        "SIZE = 1MB, " +        "MAXSIZE = 5MB, " +        "FILEGROWTH = 10%)";    SqlCommand myCommand = new SqlCommand(str, myConn);    try     {        myConn.Open();myCommand.ExecuteNonQuery();MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);    }    catch (System.Exception ex)    {MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);    }    finally    {if (myConn.State == ConnectionState.Open){    myConn.Close();}    }
  5. Change the connection string to the computer running SQL Server, and then make sure thatDatabaseThe parameter has been setMasterOr empty.
  6. Press F5 or Ctrl + F5 to run the project, and then clickCreate a database.
  7. Use the server resource manager to verify whether a database has been created.
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.
  • If you want to create a database similar to the SQL server model database and want the database to be in the default location, you should changeStrVariable, as shown in the following sample code:
    str = "CREATE DATABASE MyDatabase"
Back to Top refer to other information about creating database Transact-SQL commands, see SQL Server online books or msdn online libraries: Creating Database http://msdn.mic... Create a databaseFor more information about the transact-SQL command, see SQL Server online books or the msdn Online Library:

Create a database http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/tsqlref/ts_create_1up1.asp (http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/tsqlref/ts_create_1up1.asp)

For more information about ADO. Net objects and syntaxes, see the topics in the following Microsoft. NET Framework SDK documentation or visit the following Microsoft Website:

Use ADO. Net to access the data http://msdn2.microsoft.com/en-us/library/e80y5yhx (vs.71). aspx (http://msdn2.microsoft.com/en-us/library/e80y5yhx (vs.71). aspx) back to the top The information in this article applies:
  • Microsoft ADO. Net (supported with the. NET Framework)
  • Microsoft ADO. Network 1.1
  • Microsoft Visual C #. NET 2002 Standard Edition
  • Microsoft Visual C #. NET 2003 Standard Edition
Back to Top
Keywords: Kbhowtomaster kbsqlclient kbsystemdata kb307283
Back to the top of the page, Microsoft and/or its suppliers will not make any declaration of the applicability of the files and information contained in the images published on the server for any purpose. All such documents and related figures are provided "in accordance with the sample" without warranty of any nature. Microsoft and/or its suppliers hereby declare that they shall not be liable for all warranties and conditions relating to such information, such warranties and conditions include all implied warranties and conditions regarding merchantability, conformity with specific purposes, ownership and non-infringement. In all circumstances, in any lawsuit arising from or relating to the use or operation of information on the server, microsoft and/or its suppliers, in view of any special, indirect, consequential damage caused by the loss of use, data or profit, or any damage caused by the loss of use, data or profits are not liable for any consequences.
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.