ARTICLE

Drop command in ADO.NET

Posted by Rohatash Kumar Articles | ADO.NET in VB.NET February 14, 2011
Here we see how to use ADO.net to connect to a SQL Server database and drop the records and table structure from the database.
Download Files:
 
Reader Level:

Here we see how to use ADO.net to connect to a SQL Server database and drop the records and table structure from the database. To do that we create a table in SQL Server database which has the name emp2 and use drop command to removes the table from database.

Creating connection object

To create a connection we pass the connection string as a parameter in connection object.

Dim str As String = "Data Source=.;uid=sa;pwd=123;database=master"

Dim con As New SqlConnection(str)

 

Drop command

 

The drop command is used to removes the rows and structure of the table from the database. delete command is used to delete the only row's of the table not structure of the table but delete data can be rollback. Drop command does not allows rollback of removal data. this is a DDL statement.  which has the below command:

Dim com As New SqlCommand("drop table emp2", con)

 

Now we create a database table and insert some values in this table. Table looks like this.


tab1.gif

 

Table1.gif

 

For example

 

The above table has a name emp2 and also has data. The below example defines the drop command to drop the table from database.

 

Imports System.Data.SqlClient

Module Module1

    Sub Main()

        Dim str As String = "Data Source=.;uid=sa;pwd=123;database=master"

        Dim con As New SqlConnection(str)

        Try

            con.Open()

            Dim com As New SqlCommand("drop table emp2", con)

            com.ExecuteNonQuery()

            Console.WriteLine("Table has been droped")

        Catch ex As Exception

            Console.WriteLine("can not droped table")

        End Try

    End Sub

End Module

 

OUTPUT


tab2.gif 

Now open the database and use select statement to view the table test it.

 

 tab3.gif

Login to add your contents and source code to this article
share this article :
post comment
 
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.
    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