none
How to create a Microsoft SQL Server database by using ADO.NET and Visual C# ?

    Question

  • I am able to get this code to compile with no errors but I have run time errors. Any idea what may be the problem? It is a Microsoft program. Vista Operating System and Visual Studio .Net 2010 Beta.

     

    This step-by-step article shows you how to create a Microsoft SQL Server database by using ADO.NET and Visual C# .NET because programmers often need to create Databases programmatically.

     

     

    Steps to Create a SQL Server Database

    To create the database, follow these steps:

    Create a new Visual C# .NET Windows application.

    Place a button on Form1. Change the button's Name property to btnCreateDatabase, and then change the Text property to Create Database.

    Use the using statement on the System and System.Data namespaces so that you do not have to qualify declarations in those namespaces later in your code. Add the following code to the General Declarations section of Form1:

    using System;

    using System.Data.SqlClient;

                                     

     

    Switch to Form view, and then double-click Create Database to add the click event 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();

           }

        }

                                     

     

    Change the connection string to point to your computer running SQL Server, and then verify that the Database argument is set to Master or is blank.

    Press F5 or CTRL+F5 to run the project, and then click Create Database.

    Use the Server Explorer to verify that the database is created.

    NOTES:

    This code creates a custom database with specific properties.

    The folder that is going to hold the created .mdf and .ldf files must already exist before you run the code or an exception will be generated.

    If you want to create a database that is similar to the SQL Server Model database, and you want the database in the default location, then change the str variable in the code, as in the following sample code:

    str = "CREATE DATABASE MyDatabase"

                                     

     

    Back to the top

    REFERENCESFor additional information about the Create Database Transact-SQL command, see t...For additional information about the Create Database Transact-SQL command, see the SQL Server Books Online or visit the MSDN Online Library:

    Create Database

    http://msdn2.microsoft.com/en-us/library/aa258257.aspx (http://msdn2.microsoft.com/en-us/library/aa258257.aspx)

    For more information about ADO.NET objects and syntax, see the following topic in the Microsoft .NET Framework SDK Documentation or visit the following Microsoft Web site:

    Accessing Data with ADO.NET

    http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx)

     

    HERE IS MY PROGRAM

     

    using System;

    using System.Data.SqlClient;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

     

    namespace DataBase

    {

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

                    private void button1_Click(object sender, EventArgs e)

            {

                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();

                    }

                }

            }

     

            private void Form1_Load(object sender, EventArgs e)

            {

     

            }

        }

    }

     

    RUN TIME ERRORS GENERATED

     

     

    • Moved by sudarshans1Moderator Tuesday, March 02, 2010 8:24 PM Moving from TechNet Online: Future Plans Forum, please move or answer as necessary (From:TechNet Online: Future Plans)
    Thursday, February 04, 2010 8:14 PM