Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Data Programming  »  ADO.NET  »  ADO.NET for Beginners Part Three  »  The Address Book Program
ADO.NET for Beginners Part Three
by David Jeavons | Published  01/12/2007 | ADO.NET | Rating:
The Address Book Program

If we cast our minds back to part one of this series, you will remember that we discussed the various objects and the namespaces where they reside in order to work with databases. In particular, we studied the System.Data.OleDb namespace and the objects contained within to connect to a Microsoft Access database and manage the data. Now that we are moving on to Microsoft SQL Server we will want to utilise a different set of objects that reside in the System.Data.SqlClient namespace.  It is feasible to continue to use the OleDb objects but they are not optimized for use with Microsoft SQL Server whereas the SqlClient objects are specifically designed to work with Microsoft SQL Server and therefore you should use these to gain optimum performance.

 

Fortunately, the objects that we will be using have the same methods as their OleDb counterparts we have already used and therefore do not require much tweaking to change them over to the SQL Server equivalents and have a working program. This is what we will do now.

 

The first change we will make is to replace the Imports statement that we have used in both the main and ManageData forms to the following:

 

    1         Imports System.Data.SqlClient

 

As soon as you have done this, you should see a number of errors appear in the Error List view (if you haven’t got this view available, select the View menu followed by Error List). The errors are caused because we have removed the OleDb namespace and therefore, the compiler cannot find the location of all of the OleDb objects we have used. This is quite handy for us however as we can now use this Error List to determine what and where we need to make code changes.

 

Let’s start by changing all OleDbConnection objects to SqlConnection objects. This can be done with a simple find and replace by using the Find and Replace tool in Visual Studio 2005.   To access this, select Quick Replace from the Edit Menu and in the “Find What” field enter OleDbConnection and in the “Replace With” field enter SqlConnection. Also ensure that the “Look In” drop down list states Current Project and then press the “Replace All” button. Now we will do the same for the following:

 

Find What Replace With
OleDbDataAdapter SqlDataAdapter
OleDbCommand SqlCommand
OleDbDataReader SqlDataReader

Unfortunately, we cannot run the project just yet as we still have to modify the connection string for the SqlConnection objects in order to point to the new database on your instance of Microsoft SQL Server.  Now might be a good time to note that we really should have used a configuration file to store the connection string instead of littering it throughout the program as it would have made this change very simple.   Hindsight is a great thing. So, we will just have to get our hands dirty and find each routine that currently specifies a connection string (actually, I will list the routines below):

Form Routine
Main RetrieveContacts
Main btnDelete_Click
ManageData btnSave_Click
ManageData RetrieveContactDetails

The type of connection string that we will use to connect to Microsoft SQL Server depends on how your server is setup. The most basic connection string simply states the name of the server to connect to and which database to use along with the credentials required to access the server. Note that the connection string can state whether to use Windows Authentication or SQL Server Authentication (an example of both follows). For simplicity, I am assuming that you are using an instance of SQL Server that is installed on the same machine as Visual Studio and therefore is known as the Local instance. If this is not the case then you will need to modify the Server value in the following connection strings to point to the name of the machine where the Microsoft SQL Server resides.

Specifying user credentials (SQL Server Authentication):

Data Source=(Local);Initial Catalog=AddressBook;User=username;Password=password

 

Windows Authentication:

Data Source=(Local);Initial Catalog=AddressBook;Integrated Security=SSPI

 

For more examples of connection strings for Microsoft SQL Server please refer too: Carl Prothman's connection strings 

 

Once you know which connection string you require, you will need to modify each of the routines listed above to use the new connection string. Once this is done, you should be able to run the program in its current state and all should work as before.

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 02/15/2007)
Rating
Thanks alot. It's easy for me a chinese to understand what u talk about.
 
Comment #2  (Posted by Resty on 06/21/2007)
Rating
I'm kinda stuck with this tutorial. I did everything & when I ran the program it would point to the line, "adapter.Fill(dt)" and the error would be: ...that the server might not allow remote connections. The fact is the SQL Server is installed locally. Please advise. Cheers!
 
Comment #3  (Posted by David Jeavons on 06/21/2007)
Rating
Hi Resty

What version of SQL Server are you using?

Also, can you post the connection string you are using.


Thanks
 
Comment #4  (Posted by Resty on 06/22/2007)
Rating
Thanks for your urgent reply. I'm actually using the SQL Server 2005 Express Edition & I'm doing my project in VB 2005 Express Edition. Below is a bit of my code which have the connection string:

Private Sub RetrieveContacts()
Dim conn As New SqlConnection("Data Source=(Local);Initial Catalog=AddressBook;Integrated Security=SSPI")
Dim adapter As New SqlDataAdapter("Contacts_AllContacts_Select", conn)
Dim dt As New DataTable("Contacts")
Try
adapter.Fill(dt)

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

dgContacts.DataSource = dt

End Sub

Hoping for your next reply. Cheers

Resty

 
Comment #5  (Posted by David Jeavons on 06/22/2007)
Rating
Hi Resty

I am not sure that the connection string you are using will work with SQL Server 2005 Express Edition. Instead, you might like to try one of the following:

If you are storing your database in the Data directory then:

Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;

Otherwise, if your database resides somewhere on your machine:

Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;

Hopefully one of these connection strings will resolve your issue.


HTH
 
Comment #6  (Posted by Resty on 06/22/2007)
Rating
Connection now works, but I have another problem. When adding some records & actually saving them, then closing the program & coming back to it again, the data are lost. That also goes with edit & delete function. When program closes, it returns to its original state. What is happening this time?
Please shed light on this. Cheers!


 
Comment #7  (Posted by David Jeavons on 06/22/2007)
Rating
Hi Resty

I think what is happening is that your database file has the property "Copy to Output Directory" set to "Copy Always". This means that each time you build (debug) your application the database file is copied to your output directory and you are then using this database during the lifetime of the application. The next time you run your program this database is overwritten therefore giving the impression that data is not being saved.

You can change this property value to "Do Not Copy" thus ensuring that you are always working with the same database file.


HTH
 
Comment #8  (Posted by Resty on 06/25/2007)
Rating
Thanks David! It's working perfectly now. Looking forward to more articles like this one from you. Really a great tutorial to learn programming!!!
 
Comment #9  (Posted by an unknown user on 07/03/2007)
Rating
Exactly what I was looking for. Most tutorials target design database manipulation techniques instead of programatic techniques.
Fantastic tutorial.
 
Comment #10  (Posted by an unknown user on 10/26/2007)
Rating
Outstanding. Just outstanding for me, a beginner. Outstanding. Thanks much.
 
Comment #11  (Posted by an unknown user on 11/14/2007)
Rating
Straight forward tutorial, exactly what the novice needs.
 
Comment #12  (Posted by an unknown user on 12/27/2007)
Rating
Very good job a explaining how to create a database project for access and SQLServer.
 
Comment #13  (Posted by Steve on 01/14/2008)
Rating
Thank you very much for taking the time and effort to write a superb set of tutorials - I have learned more in the last 3 nights than in the previous 3 months!
Question - in SQL databases, what is the difference between "Do not copy", "Copy if Newer", and "Copy Always".
Again thank you.
Steve
 
Comment #14  (Posted by David Jeavons on 01/15/2008)
Rating
Hi Steve

Thank you for your comments.

To answer your question, the "Do not copy" will ensure that when you compile your application, the database will not be copied to the output (build) directory. The "Copy if Newer" will only copy the file to the output directory if the file is newer. However, be careful with this option as when you compile your application the date of the file may change in which case the file will be copied even if the contents have not changed. Finally the "Copy Always" option will always copy the file to the output directory.

I personally find it safer to use the "Do not copy" option and ensure that I manually copy the database file. Thus ensuring that no changes are overwritten by mistake.


HTH
 
Comment #15  (Posted by an unknown user on 02/14/2008)
Rating
Great article!
 
Comment #16  (Posted by an unknown user on 02/23/2008)
Rating
Super!!!
 
Sponsored Links