Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 

Create database in sql server using asp.net with C# (VS2005)

By , 5 Oct 2006
 
SqlDatabase.zip
SqlDatabase
App_Data
Bin
Interop.SQLDMO.dll
Interop.SQLDMO.dll.refresh
Introduction.doc
Picture
MySamples_1x1.jpg
MySamples_2x1.jpg
MySamples_3x1.jpg
SqlDatabase.suo
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    //private System.ComponentModel.Container components = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        SQLDMO.Application oSQLServerDMOApp = new SQLDMO.Application();
        SQLDMO.NameList oNameList;
        oNameList = oSQLServerDMOApp.ListAvailableSQLServers();
        for (int intIndex = 0; intIndex <= oNameList.Count - 1; intIndex++)
        {
            if (oNameList.Item(intIndex as object) != null)
            {
                cboServers.Items.Add(oNameList.Item(intIndex).ToString());
            }
            else
            {
                cboServers.Items.Add("(Local)");

            }
        }
         cboServers.SelectedIndex = 0;

    }
    protected void btnCreateDB_Click(object sender, EventArgs e)
    {
        string strDatabaseName = "";
        SQLDMO.SQLServer gSQLServerDMO = new SQLDMO.SQLServer();
        SQLDMO.Database nDatabase = new SQLDMO.Database();
        SQLDMO.DBFile nDBFileData = new SQLDMO.DBFile();
        SQLDMO.LogFile nLogFile = new SQLDMO.LogFile();

        strDatabaseName = txt_Database.Text.ToString();


        try
        {
            gSQLServerDMO.Connect(this.cboServers.SelectedItem.ToString(), this.txtUser.Text, this.txtPassword.Text);

            nDatabase.Name = strDatabaseName;
            nDBFileData.Name = strDatabaseName;
            nDBFileData.PhysicalName = gSQLServerDMO.Registry.SQLDataRoot + "\\DATA\\" + strDatabaseName + "_Data.mdf";
            nDBFileData.PrimaryFile = true;
            nDBFileData.Size = 2;

            nDBFileData.FileGrowthType = SQLDMO.SQLDMO_GROWTH_TYPE.SQLDMOGrowth_MB;
            nDBFileData.FileGrowth = 1;

            //Add the DBFile object
            nDatabase.FileGroups.Item("PRIMARY").DBFiles.Add(nDBFileData);

            //
            nLogFile.Name = strDatabaseName + "Log";
            nLogFile.PhysicalName = gSQLServerDMO.Registry.SQLDataRoot + "\\DATA\\" + strDatabaseName + "_Log.ldf";
            nLogFile.Size = 2;
            nDatabase.TransactionLog.LogFiles.Add(nLogFile);

            gSQLServerDMO.Databases.Add(nDatabase);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('Database Sucessfully Created');</script>");
            gSQLServerDMO.DisConnect();
           

        }
        catch (Exception SQLDBException)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script type=\"text/javascript\">alert('" + SQLDBException.Message + "');</script>");

            return;
        }
        finally
        {
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of use and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Joshy_geo
Web Developer
India India
Member
I'm Joshy George.I finished my Post Graduation in Computer Application,i'm from the great 'Gods Own Country', Kerala, India. Now I'm working in Infopark Kochin, as a Sr.software Engineer.my specialised areas are C# ,Asp.Net ,Vb6,smart card application,Tapii,Comm ctrls.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 5 Oct 2006
Article Copyright 2006 by Joshy_geo
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid