Connecting Visual Basic to SQL Server 6.5

  • Bookmark and Share

In the company I am working in right now, they are still using a very old database server which is MS SQL Server 6.5. We have a program that query the database for customer information. The program was designed in-house 10 years ago by the person I am replacing. There are some issues with the design of the program and we can not make any changes to it as there are no source codes to be found. The only chance is to write a new program that connects to the existing database.

Visual Studio 2008 is the plateform I use to write my programs. However, it supports native connection to MS SQL Server 7.0 or newer only. To connect to SQL Server 6.5, I had to use a different method, similar to the MySQL connection I have talked about earlier.

Setting Up ODBC Connection

To connect to the database in question, I need to setup an ODBC connection in the operating system first. To do that in Windows XP, follow the steps below:

  1. Start > Control Panel > Administrative Tools > Data Sources (ODBC)
  2. Under the System DNS tab, click Add
    visualbasic_odbc01
  3. Select SQL Server and click Finish
    visualbasic_odbc02
  4. Enter a name for the connection and select the server that is hosting the SQL database, click Next
    visualbasic_odbc03
  5. Select the second option to use SQL Server Authentication and enter the user ID and password, click Next
    visualbasic_odbc04
  6. On the next page, leave the settings as-is and click Next
    visualbasic_odbc05
  7. Click Finish
    visualbasic_odbc06
  8. Click Test Data Source. If the settings are correct, it should tell you the test is successful.
  9. Click OK until all screens are closed.

Visual Basic to ODBC

After you have setup the ODBC connection in your operating system, you can use the following codes to establish the connection in Visual Basic. You need to replace the variables in the connection string and the SQL query in the odbcCommand.

Dim conn As New System.Data.Odbc.OdbcConnection
conn.ConnectionString = "Dsn=VB_ODBC;uid=user;pwd=password"
conn.Open()
Dim dbCommand As OdbcCommand
Dim dbAdapter As OdbcDataAdapter
Dim dbTable As Data.DataTable
dbCommand = New OdbcCommand("SELECT * FROM customer", conn)
dbAdapter = New OdbcDataAdapter(dbCommand)
dbTable = New Data.DataTable
dbAdapter.Fill(dbTable)
dbAdapter = Nothing
dbCommand = Nothing

Display Results in Datagridview

With the codes above, you have now loaded the results of the query “SELECT * FROM customer” into a data table in the memory. You can then display the data table containing the query results in your program such as a datagridview.

DataGridView1.DataSource = dbTable

At the end, remember to close the database connection with the following code.

conn.Close()

You should find that the codes above are very similar to the codes to setup with MySQL. You just need to replace the MySQL naming to ODBC.

Quick Tips: Clearing Your Printer Spool

  • Bookmark and Share

In our company, we have several Windows 98 systems connected to our domain. These systems are mainly there for some old programs that couldn’t be migrated to Windows XP. When it comes to printing, these Win98 machines are connected to printers that are shared from a server that is running Windows Server 2003. The printings have been working fine until yesterday when all Win98 machines receives the following error.

There was a problem printing to the printer due to an unknown system error. Restart Windows, and then try printing again.

The printing wouldn’t start and restarting Windows didn’t help it.

After doing some research on the internet about the error message, it came to an easy fix. All you need to do is to remove all the files inside the spool/printer folder on the server. If you are sharing your printers from a Win XP or Win 2003 Server, then you can find that folder in here:

C:\WINDOWS\system32\spool\PRINTERS

You should see a lot of .SPL files in there. You need to remove all of them to get your Windows 98 machines printing again.

ASUS Tops Laptop Reliability Research

  • Bookmark and Share

SquareTrade has recently published the results of a research about laptop reliability. The conclusion is that reliability of today’s laptop is very dependent on the manufacturer. The research results are very similar to my laptop experiences based on my own purchases and repair requests from my clients.

Laptop Malfunction Rates by Manufacturers

In the research, ASUS comes out to be the most reliable brand with only 15.6% malfunction rate over 3 years. This is why I always prefer to buy ASUS laptops. I have owned 3 ASUS laptops over the last 6 years with my newest purchase of the ASUS UL30A. The last 2 laptops are still working perfectly as of today.

On the other end, HP is the least reliable with the highest malfunction rate of 25.6% over 3 years. That is 1 out of 4 HP laptops would likely to break after 3 years lifetime. It is also consistent with my experiences where most of the laptops I fixed were from HP. And the most common problems I have seen are overheating issues and broken USB ports. I always have a feeling that there are some problems with the internal design of their laptop chassis.

A little surprise from this research is that Apple only ranks forth. Apple’s products always seem to be high quality products to me and I would expect it to rank in the top three. Nonetheless, 17.4% is still way better than HP’s malfunction rate.

If you are planning to get a new laptop during this holiday, you should really look into ASUS as they always provide high quality and sleek designs. You can read about the full report of laptop reliability here.

Linux Version of App Store?

  • Bookmark and Share

Today I read a blog post from the technology section of Canada.com. In the post, the author mentioned about Apple’s App Store in which it is really true that “there’s an App for that”. He was saying that there are over 100,000 applications available on App Store and you can find anything you want. As a Linux user myself, I have to say that Linux users have been enjoying this for a long time ever since the availability of package management tools. More to that, Linux users have been enjoying it for free. Although the number of applications available may not be as many as App Store, but you can find much more complex and functional programs in Linux.

No matter which Linux distribution you use, there is a package management tool implemented to let you find and install free applications in just a click. If you are Ubuntu user like me, then you would find the new Ubuntu Software Centre available. If you are using OpenSuse, then you would use the Software Management function from its YaST Control Center.

To be fair for both sides, you can not really compare between Linux’s package management tools and Apple’s App Store because one is for the desktop and laptop platforms while the other is specifically for iPhone. The users and demands between the two are very different. However, Linux is a very scalable operating system and it has very high potential in the mobile platform. One day we may have mobile versions of our favorite Linux apps and an App Store for Linux mobile with full integration between the mobile and desktop apps. And on top of that, everything is free!

Connecting Visual Basic to MySQL Database

  • Bookmark and Share

Over the past year, I have spent a lot of time in learning and doing programming with Visual Basic using Microsoft’s Visual Studio 2008. As a “junior programmer”, I have found myself spending a lot of time in searching for solutions to problems that I have encountered during the development of the program. Therefore, I would like to start documenting my solutions to the problems so that you do not need to spend as much time as I did.

Requirements

The programming language I chose to work with is Visual Basic, simply because someone told me it is easy to learn. Most programs today work with a certain database which stores all the data collected by the program. I have decided to use MySQL as my database because it is free and it provides flexibility in case I want to build a web interface on it in the future with PHP.

To allow your Visual Basic codes to talk to MySQL database, you need to install couple things.

  1. MySQL Community Server
  2. MySQL .Net Connector

You can find both items on MySQL’s website. The first one is the server providing the actual database and the second item is the .Net Connector that provides the connection machanism between a .Net program and the MySQL Server. Once you have installed both items, you need to create a database and a user on your MySQL Server, and the user should be given privileges to to the database.

Referencing MySQL Connector Library

To connect to your MySQL database from Visual Studio 2008, you need to reference the MySQL.Data library in your project.

  1. In Visual Studio 2008, click on “Project” from the menu. Then click on “Properties” – the last item in the drop down menu.
  2. On the properties screen, click on “References”.
  3. Click on “Add…”
  4. In the Add Reference dialog box, under the .Net tab, select the “MySQL.Data” component. Click “OK”
  5. In the list under “Imported namespaces”, make sure “MySQL.Data” is checked.
  6. Save your project.

Creating Connection String

The following Visual Basic codes show you how to establish the connection.

Dim DBConnect As MySqlClient.MySqlConnection
Dim connectionString As String = "Data Source=localhost; Database=DB_Name; " & _
"User ID=DB_User_Name; Password=DB_User_Pw"
DBConnect = New MySqlClient.MySqlConnection(connectionString)
DBConnect.Open()

The most important part is the construction of the connection string. It contains the information about where your database is, the name of the database, and the login credentials. These information should match with the configurations you have made when you were installing MySQL Server.

After you have successfully established the connection with the line DBConnect.Open(), you can begin creating queries as if you are connecting to a MS SQL Server, using the MySqlClient namespace instead of SqlClient namespace. When you are done querying the database, you need to close the connection with the following code.

DBConnect.close()

UTF-8 Encoding

If you plan to allow insert data that contains fonts from lanuages other than English, then you should add “CharSet=utf8″ to the end of your connection string to make sure everything is saved as UTF8 encoding.

Dim connectionString As String = "Data Source=localhost; Database=DB_Name; " & _
"User ID=DB_User_Name; Password=DB_User_Pw; CharSet=utf8"

This is extremely important if you are using different operating systems between the server (e.g. Linux) and the client (e.g. Windows).