Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> How To: Connect, Read, Write, Close A Database, VB.NET
bob3695
post Jun 25 2005, 02:53 AM
Post #1


Member [ Level 1 ]
Group Icon

Group: Members
Posts: 35
Joined: 10-June 05
Member No.: 6,038



Here is a tutoral or a novice programmer. This tutorial assumes you have a basic knoladge of databases and VB.NET. It also assumes that you have a MS access database named Testdb.mdb with a table called "Table1" that has at least one column called "Col1". You must start with a form with one label on it. If you need any help email me at bob3695@gmail.com.

The first step is connecting to the database.

The first thing you must do is add the ADODB reference. To do this in the "Project" menu click "Add Reference" then select ADODB from the list and click "Select".

The next thing you need is at least two variables. One for the connection and one for the record set. A record set is used to query the database. To make these variable add the next two lines at the top of the form.

CODE

Dim Conn as new ADODB.Connection
Dim Rec as new ADODB.Recordset


Now that we have our variables we need to set-up the connection. In order to set up the connection you need a connection string. The one I use works for me but in order to meet your needs you may need to make your own. To set-up the connection string in the "FormLoad" event add this code.

CODE

Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=Testdb.mdb"


Great...we are ready to connect to the database. Add this line under the connection string set-up line.

CODE

Conn.Open()


We now have a connection to the database. Now we need to find out how to get data from it. In order to do this you need to open a table in the database. To do this you must add these lines to your code.

CODE

Rec.Open("SELECT * FROM Table1", Conn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)


Now that we have a table open we need to actually get data from it. In order to do this add the following code. This code goes through all the rows in the table and does what you want with the data.

CODE

While not Rec.EOF
Label1.text = Label1.text & vbcrlf & Rec.Fields("Col1").value
Rec.MoveNext
End While


All this code does is take all the data from the column named "Col1" From "Table1" and put it all in Label1. Now that we have our data we need to edit the record and add a new one. To edit the current record use this code.

CODE

Rec.Fields("Col1").value = "SomeValue"
Rec.Update()


Now we need to add a new record. Use this code to do that.

CODE

Rec.AddNew()
Rec.Fields("Col1").Value = "SomeValue"
Rec.Update()


The code to add a record is very simialer except you need on more line of code. The Rec.AddNew(). All this does is add a new record to the database then selects it. Now that we are done playing around with the database we need to close it. Use this code.

CODE

Rec.Close
Conn.Close


Thats it! You opened a database, Got information from the database, Changed and added some records, and closed the database.

Thanks,
Rich
Go to the top of the page
 
+Quote Post
iGuest
post May 5 2008, 08:47 AM
Post #2


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



connecting database with vb.net web application and download it in different path?
How To: Connect, Read, Write, Close A Database

I want to connect the access database with website by vb.Net by useing your way but it fails how can I connect it and let the program recognize db location after I upload the web site in a differnt path?

-reply by Lamis
Go to the top of the page
 
+Quote Post
iGuest
post Jun 26 2008, 03:01 PM
Post #3


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



I have a access database, my program is reading data from DB into dataset-also deleting-updating-adding properly into dataset.

But fails to do any of these manipulation onto server DB. I have used oledconnection; instead of AODB is that where I am going wrong. This is my first project in VB2005- I am a foxpro programmer originally. My thanks in advance to whover helps me

-reply by Prashant. C
Go to the top of the page
 
+Quote Post
iGuest
post Jun 26 2008, 03:01 PM
Post #4


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



I have a access database, my program is reading data from DB into dataset-also deleting-updating-adding properly into dataset.

But fails to do any of these manipulation onto server DB. I have used oledconnection; instead of AODB is that where I am going wrong. This is my first project in VB2005- I am a foxpro programmer originally. My thanks in advance to whover helps me

-reply by Prashant. C
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Creating A Game In Rpg Maker 2000/2003(18)
  2. Want To Connect Computers..(12)
  3. Mirror My MySQL Database To Another Mysql Server(4)
  4. VB.NET: Rotating Label & Angled Text Control(8)
  5. Phishing In Myspace(8)
  6. How To Connect MySQL With Flash?(8)
  7. DVD-Rom Doesn't Read DVDs(7)
  8. How To Connect Dual/triple Monitor + Advantages(21)
  9. Need Help With A PHP - MySQL Registration Script(13)
  10. How To Connect Computers In A Wi-fi Network(10)
  11. How To Write A Virus ?(36)
  12. MySQL Output Database Question(18)
  13. Shmmni, Shmseg, Shmmax : How To Know The Current Value On Solaris ?(3)
  14. Firefox 2(3)
  15. [tutorial] Pc-pc Home Networking.(9)
  1. Wolfenstein: Enemy Territory(1)
  2. Sandisk Memory Card Write Protection(12)
  3. New Here, Read Some Other Posts...(1)
  4. Database(1)
  5. Integrate Access Database Onto Intranet Site(5)
  6. Bluetooth Software(3)
  7. Accessing Ms Access Database From A Centralized Location?(5)
  8. Mysql Database Management(1)
  9. Mysql Database Entry By Excel Sheets(2)
  10. Space Needed For Database(10)
  11. Database Access On Remote Server W/jsp(0)
  12. Some Useful Database Links.(7)
  13. Trap17/computing Host(1)


 



- Lo-Fi Version Time is now: 30th August 2008 - 12:26 PM