Creating new Database Table in run time in VB.NET

This article explain you that how we can create table in the Page_Load or in a single click of button.
  • 5363

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!

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.