3/18/11

create a table at runtime using sql query vb.net

the 1 source by sagar in  
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


0 التعليقات:

Post a Comment

Related Posts Plugin for WordPress, Blogger...