SIGN UP MEMBER LOGIN:    
ARTICLE

Create table at run time in Database by using ASP.NET

Posted by Manish Dwivedi Articles | Learn .NET December 16, 2010
This article explains how to create a table in Database on Page_Load or on a click of button.
Reader Level:

Using the following code you can easily create a new table in a Database and fill records in that table. You can use this code on the Page_Load or in any Button click event.

try

{

    if (!(conn.State == ConnectionState.Open))

    {

        conn.Open();

    }

    string sql = "CREATE TABLE mySchoolRecord" + "
    (StudentId INTEGER CONSTRAINT PkeyMyId PRIMARY KEY,"
   
+ "Name CHAR(50), Address CHAR(255), Contact INTEGER)";

    cmd = new OdbcCommand(sql, conn);

    cmd.ExecuteNonQuery();

 

    sql = "INSERT INTO mySchoolRecord (StudentId, Name,

    Address,Contact) " + "VALUES (1, 'Mr. Manish', "

    + " 'Sector-12,Noida', 2447658 ) "

    cmd = new OdbcCommand(sql, conn);

    cmd.ExecuteNonQuery();

 

    sql = "INSERT INTO mySchoolRecord (StudentId, Name,

    Address,Contact) " + "VALUES (2, 'Mr. Ravi', "

    + " 'New Delhi', 2584076521 ) "

    cmd = new OdbcCommand(sql, conn);

    cmd.ExecuteNonQuery();

 

    sql = "INSERT INTO mySchoolRecord (StudentId, Name,

    Address,Contact) " + "VALUES (3, 'Mr. Peter', "

    + " 'United States', 25684124 ) "

    cmd = new OdbcCommand(sql, conn);

    cmd.ExecuteNonQuery();

 

 

    if (conn.State == ConnectionState.Open)

    {

        conn.Close();

    }

}

catch (OdbcException ex)

{

    MessageBox.Show(ex.Message.ToString);

}

Login to add your contents and source code to this article
share this article :
post comment
 

Hi Rajesh, For creating table, i have passed the static values, you can change it dynamically for that you have to create UI and ask to user for inserting table name and values. For ex. i have written like that string sql = "CREATE TABLE mySchoolRecord" + " (StudentId INTEGER CONSTRAINT PkeyMyId PRIMARY KEY," + "Name CHAR(50), Address CHAR(255), Contact INTEGER)"; you can change it like that string sql = "CREATE TABLE" + TextBoxTableName.Text + " (StudentId INTEGER CONSTRAINT PkeyMyId PRIMARY KEY," + "Name CHAR(50), Address CHAR(255), Contact INTEGER)"; and for passing the value dynamically. sql = "INSERT INTO mySchoolRecord (StudentId, Name ) " + "VALUES (1, '"+ TextBoxName.Text +"') " By this way you can dynamically pass values.

Posted by Manish Dwivedi Dec 20, 2010

how can we create tables at run time, and table name will be passed by user along with data. thaks rajesh kumar rajeshbcrec@gmail.com

Posted by Rajesh Kumar Dec 19, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor