Posts Tagged ‘.net’

Working with DBF file (dBase IV) from vb .net (create dbf, insert into, select from)

Friday, March 5th, 2010

I search a lot for this, it’s a little old, but if you needed, here it is the sample code:

Dim con As New OleDb.OleDbConnection
Dim com As New OleDb.OleDbCommand
con.ConnectionString = “Provider=vfpoledb.1;Data Source=dir.dbf;Collating Sequence=machine”
con.Open()
com.Connection = con
com.CommandText = “INSERT INTO dir (col1) VALUES (‘” & val1 & “‘)”
com.ExecuteNonQuery()

If you want a select, it is similar, replace the sql command.

And here is the code for creating a dbf file:

Dim dBaseConnection As New System.Data.OleDb.OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;” & _
“Data Source=” & path & “;” & _
“Extended Properties=dBase IV”)

dBaseConnection.Open()

‘New table
Dim SQLCreateCommand As String
SQLCreateCommand = “CREATE TABLE SUBDIR ” & _
“(subdir TEXT(50))”

Dim dBaseCommand As New System.Data.OleDb.OleDbCommand(SQLCreateCommand, dBaseConnection)

dBaseCommand.ExecuteNonQuery()
dBaseConnection.Close()

In the code above, path is the directory where you want to create the DBF file, it does not include the filename!

Update:
You can download Microsoft OLE DB Provider for Visual FoxPro 9.0 for the above code from
here.

Adding a function or procedure description in vb.net

Tuesday, February 16th, 2010

A searched a lot for this issue,  and although some sites suggested that it is a hard thing to do in VB.NET and that in C# is much easier, I finally found the code for adding a description to a function or to a procedure and it’s quite easy:

”’ <summary>
”’<para>Non-HTML files like Adobe Acrobat PDF files and Word
”’documents are stored with their original URLs partially
”’encoded in their filenames. This function will return the
”’original URL of the file.</para>
”’<para>The encoding done by the Index Server Companion removes
”’characters that cannot be present in Windows filenames
”’(these are: \/:*?”<>|). The decoding performed is:</para>
”’ <list type=”table”>
”’ <listheader><term>Find</term><description>Replace</description></listheader>
”’ <item><term>^f</term><description>\</description></item>
”’ <item><term>^b</term><description>/</description></item>
”’ <item><term>^c</term><description>:</description></item>
”’ <item><term>^s</term><description>*</description></item>
”’ <item><term>^q</term><description>?</description></item>
”’ <item><term>^d</term><description>\</description></item>
”’ <item><term>^l</term><description><</description></item>
”’ <item><term>^g</term><description>></description></item>
”’ <item><term>^p</term><description>|</description></item>
”’ </list>
”’ </summary>
”’ <param name=”FileName”>The document’s original filename.</param>
”’ <returns>Decoded filename</returns>
”’ <exception cref=”System.Exception”>Throws an exception when something goes wrong.</exception>
Private Function VBCreateURLFromFileName(ByVal FileName As String) As String

Try
‘Remove o_ prefix from URL
FileName = FileName.Substring(2, FileName.Length – 2)

‘Remove other encoded characters
FileName = FileName.Replace(“^f”, “\\”)
FileName = FileName.Replace(“^b”, “/”)
FileName = FileName.Replace(“^c”, “:”)
FileName = FileName.Replace(“^s”, “*”)
FileName = FileName.Replace(“^q”, “?”)
FileName = FileName.Replace(“^d”, “”"”)
FileName = FileName.Replace(“^l”, “<”)
FileName = FileName.Replace(“^g”, “>”)
FileName = FileName.Replace(“^p”, “|”)
FileName = FileName.Replace(“^f”, “\\”)
FileName = FileName.Replace(“^f”, “\\”)
FileName = FileName.Replace(“^f”, “\\”)
FileName = FileName.Replace(“^f”, “\\”)
Catch

End Try

Return FileName

End Function

The code was found here.

Replacing a text in a word document from VB.net

Monday, January 18th, 2010

This is the code I used:

Dim appWord As New Word.Application
Dim docWord As New Word.Document
docWord = appWord.Documents.Open(“C:\Infofarm\test.doc”)
Try
Dim myStoryRange As Microsoft.Office.Interop.Word.Range
For Each myStoryRange In docWord.StoryRanges
With myStoryRange.Find
.Text = “<-email->”
.Replacement.Text = “aaa@oooo.com”
.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
.Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
End With
Next myStoryRange
docWord.Save()
appWord.Quit()
docWord = Nothing
appWord = Nothing
Catch ex As Exception
MsgBox(ex.Message)
End Try

Hope it help. Good luck!

Get the name of the month in vb.net in any language

Friday, December 11th, 2009

If you want to get the name of a month in a specific language, you should try this code:

Dim myDTFI As Globalization.DateTimeFormatInfo = New Globalization.CultureInfo(“ro-RO”, True).DateTimeFormat
Console.Write(myDTFI.GetMonthName(2))

myDTFI.GetMonthName(2) will be “februarie”. Hope this helps you.