C# : Tutorial of Asp.net with c# and example

Example and code about windows application, web application, SQL, C#, WPF etc which is helpful to developer and programer.

Search This Blog

How to create database using

♠ Posted by uvesh in at Tuesday, February 10, 2015

Create Database by programmatically


 protected void CreatDbBtn_Click(object sender, EventArgs e)
    {
        String str;
        SqlConnection myConn = new SqlConnection(@"Server=HOME\SQLEXPRESS;Integrated security=SSPI;database=master");
        str = "CREATE DATABASE MyDatabase ON PRIMARY " +
            "(NAME = MyDatabase_Data, " +
            "FILENAME = 'D:\\UveshMyDatabaseData.mdf', " +
            "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
            "LOG ON (NAME = MyDatabase_Log, " +
            "FILENAME = 'D:\\MyDatabaseLog.ldf', " +
            "SIZE = 1MB, " +
            "MAXSIZE = 5MB, " +
            "FILEGROWTH = 10%)";

        SqlCommand myCommand = new SqlCommand(str, myConn);
        try
        {
            myConn.Open();
            myCommand.ExecuteNonQuery();
            Response.Write("DataBase is Created Successfully");

            //MessageBox.Show("DataBase is Created Successfully", "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (System.Exception ex)
        {
            Response.Write(ex.ToString());
            //MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        finally
        {
            if (myConn.State == ConnectionState.Open)
            {
                myConn.Close();
            }
        }

    }

Improve Your Answer as comment below.

If our post is useful please share and comment on our post for more post and detail Visit :


http://aspdotnethelpmaster.blogspot.in/

0 comments:

Post a Comment