Tuesday, 18 July 2017

VB - Deploying an Application using Oracle Data Access

Introduction

It's great when you get your application to run in Visual Studio, or even from the compiled files on your PC, but at some stage you're going to want to deploy or share it. This is where I ran into problems with Oracle drivers.

Oracle  Drivers

You set up your Oracle access in VB by selecting the DLL that came with your client software and adding it to the project resources. Here's what mine looked like in Visual Studio..

Assigning the Oracle library as a project resource.
Then add the following imports into your program module..

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


But, although you've set your resources correctly, the DLL location and version is likely to be different on other machines you deploy to. So you'll probably to get errors.

You can start to fix this by setting the Copy Local value to be True, and then the DLL file gets bundled into the release folder when you re-compile.

Set Copy Local
But while this might get your locally compiled version working, it's likely that other machines will still have problems with driver compatibility errors.

Typically you'll see something like this..

Oracle.DataAccess.Client.OracleConnection' threw an exception. ---> Oracle.DataAccess.Client.OracleException: The provider is not compatible with the version of Oracle client

Solution

Download the Instant Client Basic Lite files from Oracle and unzip it to your PC. Copy the following library DLL files and put it into the same deploy folder as your executable:-

oci.dll
ociw32.dll
Oracle.DataAccess.dll 
orannzsbb11.dll
oraocci11.dll
oraociicus11.dll
OraOps11w.dll

Hopefully that will get you going.

Friday, 7 July 2017

VB - Connecting to an Oracle Database without using a TNS Entry

Introduction

Connecting to an Oracle database isn't too much of a problem, there's examples everywhere on the web along the following lines..

Dim conn As New OracleConnection()
Dim connstr as String, dataSource as String, userId as String, password as String
dataSource = "dev10g"
userId = "jsmith"
password = "letmein"
connstr = "Data Source=" + dataSource + "; User Id=" + userId + "; Password=" + password + ";"
conn.ConnectionString = connstr
Try
  conn.Open()
Catch ex As Exception
  ' Database connection failed
  conn.Dispose() 'Dispose of the connection
  Exit Sub
End Try


This works ok, but it's not very portable unless everyone that wants to use it sets themselves a TNS entry called "dev10g" in their tnsnames.ora file.

What if we wanted to define the host and port number in the config, and then connect without using TNS?

Connecting to Oracle Directly

In practice all we need to do is alter the connection string to provide the information that the tnsnames.ora would have sent. So the code above doesnt change much.

Dim conn As New OracleConnection()
Dim connstr As String
Dim dbServer As String, dbPort As String, dbServiceName As String
Dim userId As String, password As String

dbServer = "lordv01"
dbPort = "1521"
dbServiceName = "dev10g"
userId = "jsmith"
password = "letmein"
dconnstr = "Data Source=" + dbServer + ":" + dbPort + "/" + dbServiceName + ";User ID=" + userId + ";Password=" + password
conn.ConnectionString = connstr
Try
  conn.Open()
  Catch ex As OracleException
  conn.Dispose() 'Dispose of the connection
  Exit Sub
End Try


It took me a bit of poking around to find the right syntax, so hopefully someone will find this useful.

Friday, 30 June 2017

VB - Reading a Text Resource File Line by Line

Introduction

I'm not a VB programmer, but recently I needed to build an interface between Oracle and an MDB file, so I had to start getting familiar with Visual Express Studio 2013. I've done a lot of LotusScript in the past so it wasn't a big problem, but some things have proved to be a struggle.

For today's problem, I wanted to store a text file (used as a template) and pragmatically create a new version of it with a new name. It felt like the best thing to do was store the file as a resource, but it wasn't immediately obvious how to do it.

Creating the Resource

You can add a resource to your project as a text file using the Resources section of your project Properties. It's quite simple to access it in your code, in the following way:-

myString = My.Resource.MyTextFile 

But, if you do this then you'll get the whole file as a long text string. I wanted to be able to read it in using StreamReader and write it out line by line. But text files are always treated in this special way, so my method won't work.

The answer is to trick VB into thinking it's opening a binary file (just give it a different extension).

I created my file called inprep.template and moved it into the projects resource folder.

I then dragged it into the Resources page to register it as a project resource.

My new file resource

The VB Code

Here's the code to read the file (one row at a time) and output it to a new file.

Dim template As New MemoryStream(My.Resources.inprep)
Dim file As System.IO.StreamWriter
Dim oRdr As StreamReader = New StreamReader(template)
Try
   file = My.Computer.FileSystem.OpenTextFileWriter("c:\test.txt", False)
   Do While oRdr.Peek() >= 0
      file.WriteLine(oRdr.ReadLine())
   Loop
   oRdr.Close()
   file.Close()
Catch ex As Exception
   Console.WriteLine(ex.Message)
End Try


It's simple enough once you realise that you need to handle it as a binary file.