August 8, 2010

Vb dot Net Code to check table exist or not

Sometimes we require to write code to check whether table exist in database or not. This is simple Vb dot Net Code to check table exist or not in sql or any other database.

 Public Function CheckTempUserTbl(ByVal strSpNm As String) As String  
 Dim cmd As SqlCommand  
 Dim strCd As String  
 cmd = New SqlCommand(strSpNm, objCn.OpenDB())  
 cmd.CommandType = CommandType.StoredProcedure  
 cmd.Parameters.Add(New SqlParameter("@TBLNM", SqlDbType.VarChar, 30, ParameterDirection.Input, True, 0, 0, "", DataRowVersion.Proposed, _Table))  
 If Not IsDBNull(cmd.ExecuteScalar()) Then  
 strCd = cmd.ExecuteScalar()  
 End If  
 Return strCd  
 Catch ex As Exception  
 MsgBox("clsCommon::CreateTempUserTbl::Error occured.", MsgBoxStyle.Critical, "TEST")  
 End Try  
 End Function  

By this stored procedure you can pass any table name at runtime.