Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Truncate a SQL server table

Status
Not open for further replies.

djj55

Programmer
Feb 6, 2006
1,761
US
Hello,
I read thread 796-1350501 and I realize that truncate is dangerous. That being said it also cleans out and resets the table.
I need to do a truncate a table, having used ADODB in the past, I would like to be able to do it with the SQL connection (System.Data.SqlClient).

The old code is:
Code:
Dim rs As New ADODB.Recordset
Dim DBCONN As New ADODB.Connection
Dim CONNSTRING As String = "DSN=MySQL;"
DBCONN.Open(CONNSTRING)
rs = DBCONN.Execute("TRUNCATE TABLE MyTable;")
…

The SQL connection:
Code:
Dim SQLConn As New SqlConnection("Persist Security Info=False;Integrated Security=SSPI;" & _
 "database=MySQLDB;server=MyServer;Connect Timeout=30")
Dim daSQL As New SqlDataAdapter("SELECT * FROM MyTable", SQLConn)

I know how to do the delete but I want to do a truncate. Is it possible? If so how?

Thank you,
djj
 
Something like this???

Code:
Dim connSQL As New SqlConnection("server=server;database=database;uid=sa;pwd=something;")
Dim strSQL As String = "TRUNCATE TABLE tablename;"
Dim commSQL As New SqlCommand(strSQL, connSQL)
commSQL.CommandType = CommandType.Text
connSQL.Open()
commSQL.ExecuteNonQuery()
connSQL.Close()

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Thank you, I had to switch projects and just now got to try the code.

I thought there was a ExecuteNonQuery command for doing this but I forgot how to use it after not working in VB.NET for a few months.

Thanks again,
djj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top