Thursday, March 19, 2009

How to delete files and folder from network drive

Hi, In this sample I will show you how to delete files and folder from your hard drive using VB.NET code.

Let me first explain in words how this will work

DELETE FILES

1. I will get all see if direcoty exists
2. I will delete all files inside "AdnanKhan" directory

Dim sPathTok As String
sPathTok = My.Settings.FilePathName
If IO.Directory.Exists(sPathTok) Then
Dim dir As DirectoryInfo = New DirectoryInfo(sPathTok)
Dim files As FileInfo() = dir.GetFiles
For Each f As FileInfo In files
If f.Name.StartsWith("AdnanKhan") Then
f.Delete()
End If
Next

myLog.WriteLog("Processed: Deleted")

Else
myLog.WriteLog("Processed: Directory Does Not Exists")
End If


'Delete all Directories
Dim sPathDir As String
sPathDir = My.Settings.FilePathNameDir
If IO.Directory.Exists(sPathDir) Then
Dim dir As DirectoryInfo = New DirectoryInfo(sPathDir)
Dim dirs As DirectoryInfo() = dir.GetDirectories
For Each d As DirectoryInfo In dirs
If d.Name.StartsWith("AK") Then
d.Delete(True)
End If
Next
myLog.WriteLog("Process #1: Directories Deleted")
Else
myLog.WriteLog("Process #1: Directory Does Not Exists")
End If

Thank You

Tuesday, March 17, 2009

Wednesday, March 4, 2009

How to make Connection to Oracle from VB.NET

How to connect to Oracle Database using VB.NET

1. You can make a variable into your My Project > Settings
call this variable : sConnectionString
and in value connection string as shown below.

Source=(DESCRIPTION=(ADDRESS_
LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=servername.mycompany.com)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=dev.mycompany.com)));

Note: please make sure you put your servers information in : Host and Service_Name

Now go to the windows form or asp.net page:

Imports this into your page:

Imports Oracle.DataAccess.Client
Imports Oracle.DataAccess.Types

Note: if you can't find this in you project then you download this from oracel website :
Oracle Dll: Oracle.DataAccess.dll
version: 2.102.2.20

Declare Private Variable in your Form1.VB Page:

Private Shared sConnectionString As String = My.Settings.OracleConnectionString

Here is the function:
Public Function ProcessErrorFixDataCheck(ByVal itemNum As String) As Boolean
Dim sqlStr As String
Dim cmd As New Oracle.DataAccess.Client.
OracleCommand
Dim oraDa As OracleDataAdapter
Dim conn As OracleConnection = Nothing
Dim ds As Data.DataSet
Try

sqlStr = "select item_Num from itemTable where item_Num = '" & itemNum & "'"

' Initialize connection
oraDa = New OracleDataAdapter(sqlStr, sConnectionString)
conn = oraDa.SelectCommand.Connection
ds = New Data.DataSet()
oraDa.Fill(ds, "ItemCount")


If ds.Tables(0).Rows(0)(0) IsNot Nothing Then
Return True
Else
Return False
End If

Catch ex As Exception
Throw ex
Finally
cmd.Dispose()
conn.Close()
conn.Dispose()
GC.Collect()
End Try
End Function