VB.NET connect to database!

vincentokw

Member
Joined
Sep 5, 2005
Messages
7
Programming Experience
1-3
hi thr!!
guys, i need help, currecntly i am doing project paper 1 and i wanna use VB.net, but just a beginner, how to connect VB.net to database, i mean, how? what sort of coding can i use to connect VB.net to Database

thx !
 

ayozzhero

Well-known member
Joined
Apr 6, 2005
Messages
186
Location
Malaysia
Programming Experience
1-3
It is always a good idea if you can be more specific, for example: type of database, what you've already known and such.

To connect to a database, first you need to connect to it. If you're using MySQL, then you can refer to this:
http://vbdotnetforums.com/showthread.php?t=3831

For other connection strings, you may refer to: http://www.connectionstrings.com

Please take note that it you're using Microsoft Access, it is better to connect using OLEDB instead of ODBC.

Hope this helps...
 

vincentokw

Member
Joined
Sep 5, 2005
Messages
7
Programming Experience
1-3
oh, thanks!

i wanna connect using Mirosoft access if possible, because MySQL it's kinda confusing.
so, connect using OLEDB?

i've got to refer some books regarding this =P

so, if you guys have some example coding for connecting VB.net to Mircosoft access, it would be great, thanks again!
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
It seems like haven't seen the link i left above ... however this an example hot to make connection against MSAccess DB
VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] myConn [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2] = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\myDatabase.mdb; Persist Security Info=False;"

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] oledbcon [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] OleDbConnection(myConn)



[/size][size=2][color=#0000ff][/color][/size] 
[size=2][color=#0000ff]Try
[/color][/size][size=2][/size][size=2][color=#0000ff]If[/color][/size][size=2] oledbcon.State = ConnectionState.Closed [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]oledbcon.Open()

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] strSQL [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2] = "SELECT * FROM Table1"

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] cmd [/size][size=2][color=#0000ff]As[/color][/size][size=2] OleDbCommand = [/size][size=2][color=#0000ff]New[/color][/size][size=2] OleDbCommand(strSQL, oledbcon)

[color=blue]Dim[/color] objreader as [size=2][color=#0000ff]As[/color][/size][size=2] OleDbDataReader = cmd.ExecuteReader[/size]
[size=2][/size] 
[size=2][color=blue]While[/color] objRead.Read[/size]
[size=2][/size] 
'do something with the data i.e. fill combo, listview or something else
[size=2][/size] 
[size=2][color=blue]End While[/color][/size]
[size=2]objread.Close()
[/size]

[/size][size=2][color=#0000ff]Catch[/color][/size][size=2] ex [/size][size=2][color=#0000ff]As[/color][/size][size=2] Exception

MessageBox.Show(ex.Message)

[/size][size=2][color=#0000ff]Finally[/color][/size]
[size=2][color=#0000ff][/color][/size] 
[size=2][color=#0000ff]If[size=2][color=#000000] oledbcon.State = ConnectionState.Open [/color][/size][size=2][color=#0000ff]Then[/color][/size][/color][/size]
[size=2][color=#0000ff] 
[/color][/size][size=2]oledbcon.Close()
 
[color=blue]End if[/color]
[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Try[/color][/size]

Regards ;)
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
another way to do it is to go to the Server Explorer (left side) and click the connect to database button click the Provider tab and select the Microsoft Jet 4.0 OLE DB Provider and click next then click the elipses (...) button next to the database name textbox and find your access DB on your HDD

then if there's a user name and password that needs to be supplied type them into the appropriate fields and click OK

now your access DB will be listed in server explorer, just browse it untill you get to the table with the fields you need, highlight those fields (and the field that's got the primary key if you havnt already) now drag and drop those fields onto your VB.net form

the VS editor will create the connection and the dataadapter for you, to further customize the data adapter, just right-click it and select customize (or something to that effect), when you're done customizing it, right-click on it again and select Make Dataset (or something to that effect too), once you have the dataset you can start binding textbox's and labels to the DB fields as well as manipulate the DB info in code as well

just my tip of the day if you will :)
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
yes, but i didnt feel like digging up one of those other threads that says what all i just said so i typed it all out, mostly out of boredom on my end

sry lol :D
 

vincentokw

Member
Joined
Sep 5, 2005
Messages
7
Programming Experience
1-3
err... guys, i have problem, i already connecnt the mircosoft acess database with my VB.net form but, how to by using VB.net code to modify the data in my database(made by access)? i have no idea how to edit using VB.net code to add, or delete data in my database, how?
 

vincentokw

Member
Joined
Sep 5, 2005
Messages
7
Programming Experience
1-3
i use the server explorer to connect Mcsft Acess Dbase with VB.net, now it says connected, but how do i edit and delete the data in the Dbase?
do i need some coding to do that?
 
Top Bottom