ARTICLE

Creating new Database Table in run time

Posted by Manish Dwivedi Articles | VB.NET How do I April 19, 2007
This article explain you that how we can create table in the Page_Load or in a single click of button.
 
Reader Level:

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

 

Try

    If Not (conn.State = ConnectionState.Open) Then

        conn.Open()

    End If


    
Dim sql As String = "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. Anand', " + " 'Mayur

    Vihar, New Delhi', 2457896) "

    cmd = New OdbcCommand(sql, conn)

    cmd.ExecuteNonQuery()


    sql = "INSERT INTO mySchoolRecord (StudentId, Name,

    Address,Contact) " + "VALUES (3, 'Miss Mona', " + "

    '25,Church Road,Mumbai', 2784521) "

    cmd = New OdbcCommand(sql, conn)

    cmd.ExecuteNonQuery()

               

    If conn.State = ConnectionState.Open Then

        conn.Close()

    End If


Catch
 ex As OdbcException


    
MessageBox.Show(ex.Message.ToString)


End
Try

         

Happy Coding!

share this article :
post comment
 

hi!...does this only work for sql database or ms access as well..?

Posted by suhail mirza Jun 30, 2010

your welcome karthikayan!

Posted by Manish Dwivedi Nov 20, 2008

Hai Manish, This is karthik (Alagiri). your coding was helpful for my project.

Posted by Alagiri P Sep 13, 2008
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor