Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Toggle shoutbox Shoutbox Open the Shoutbox in a popup

@  yordan : (14 April 2014 - 05:28 PM) By The Way, This Could Be An Interesting Subject For A Topic, What About Posting This Question? Let's See If Other People Have The Same Feeling Concerning Bootlists!
@  yordan : (13 April 2014 - 09:36 AM) Boot Order : Cd, [Usb,] Hard Drive :D
@  yordan : (11 April 2014 - 07:23 PM) I Simply Let The Bios Do That
@  Ritesh : (11 April 2014 - 10:23 AM) Is It Possible To Launch Fedora Live Cd Or Installation Disk From Hard Drive On Windows Platform Using Grub Mbr File.
@  Ritesh : (11 April 2014 - 10:21 AM) No U Are Not.. Btw.. I Have Question For You.
@  yordan : (10 April 2014 - 08:02 AM) You Are Partially Right.
I Was Not.
Nevertheless, I Am Again :)
@  Ritesh : (09 April 2014 - 07:33 PM) :P
@  Ritesh : (09 April 2014 - 07:33 PM) I Think U R Not..
@  yordan : (09 April 2014 - 09:28 AM) I'm The Master Of The Shoutbox!
@  yordan : (05 April 2014 - 10:32 PM) He-He
@  Ritesh : (04 April 2014 - 06:59 PM) Ha Ha Ha ....
@  yordan : (04 April 2014 - 11:15 AM) Welcome Back, Starscream!
@  yordan : (03 April 2014 - 02:31 PM) And I Hope That He Will Come Back Soon :)
@  yordan : (01 April 2014 - 02:53 PM) Nice, Ritesh Came, I'm Not Home Alone Today.
@  Ritesh : (01 April 2014 - 08:51 AM) Oh!!! Poor Dear Yordan..
@  yordan : (31 March 2014 - 10:02 AM) I'm A Poor Lonesome Cow-Boy
@  yordan : (27 March 2014 - 02:22 PM) He Is Unpatient Due To His Patients!
@  Ritesh : (27 March 2014 - 10:46 AM) :(
@  Ritesh : (27 March 2014 - 10:46 AM) He Is Busy With His Patients.
@  yordan : (26 March 2014 - 08:12 PM) Ahsani, Where Are You?

Photo
- - - - -

Connecting To Mysql Databse


3 replies to this topic

#1 dipesh

dipesh

    Newbie [ Level 2 ]

  • Members
  • 10 posts
  • Gender:Male
  • Location:uk

Posted 15 April 2009 - 05:01 PM

hi, i need help with connecting to a mysql database, i am developing my application in microsoft visual studio with vb.net and require help with the code for the connection string, as i am not using ado.net to connect to the database, i am using a vb class file to connect to the database.

[codebox]Private Sub OpenDatabase(ByVal DatabaseName As String, ByVal TableName As String)
'declare a variable to store our SQL
Dim SQL As String
'declare a new command builder
Dim CB As OleDbCommandBuilder
'declare a variable to store the application path
Dim DbPath As String = System.AppDomain.CurrentDomain.BaseDirectory & "App_Data\"
'this is the connection string to an Access 2003 file uncomment as reguired
Dim ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DbPath & DatabaseName & ";Persist Security Info=False"
'this is the connection string for Office 2007
'Dim ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DbPath & DatabaseName & ";Persist Security Info=False"
'opens the connection object based on the connection string
mNewConnection = New OleDbConnection(ConnectionString)
Try
'open the connection
mNewConnection.Open()
Catch ex As Exception
Stop
'DatabaseTable error!!!!!!!!!!!!!!
'the program has crashed probably due to mis typing the database name
'DatabaseTable error!!!!!!!!!!!!!!
End Try
'build up our SQL
SQL = "select * from " & TableName
'connect the data adapter to the connection object using the specified sql
mDataAdapter = New OleDb.OleDbDataAdapter(SQL, mNewConnection)
'initialise the select command of the data adapter
mDataAdapter.SelectCommand = New OleDbCommand(SQL, mNewConnection)
'initialise the command builder to initialis the other commands of the data adapter
CB = New OleDbCommandBuilder(mDataAdapter)
Try
'populate the datatable via the data adapter
mDataAdapter.Fill(mTableData)
Catch ex As Exception
Stop
'DatabaseTable error!!!!!!!!!!!!!!
'the program has crashed probably due to mis typing the table name
'DatabaseTable error!!!!!!!!!!!!!!
End Try
'take a copy of the record structure
mARow = mTableData.NewRow
'close the connection
mNewConnection.Close()
End Sub[/codebox]

#2 TavoxPeru

TavoxPeru

    Super Member

  • [HOSTED]
  • 876 posts
  • Gender:Male
  • Location:Lima - Peru
  • Interests:Web and Software development, Internet, Computers, Electronic music, music, soccer.
  • myCENTs:13.21

Posted 17 April 2009 - 10:48 AM

Well, i don't use VB for a long time, but tell me the reason because you don't use ado.net? i guess that it could help you to do this.

I think that another way will be by creating a DSN and then use it on your connection string, to do this you need to download the odbc driver for MySql or the MySql Connector/NET.

BTW, looking at your code i see that you are using the strings for Access or Office, try this one instead:

MySQLConnectionString = "Server=myServer;" & _
"Database=myDatabase;" & _
"Uid=myUserID;" & _
"Pwd=myPassword;" & _
"Connect Timeout=30;"

Some resources that may help:
Check out the articles section of the last one, there are lots of tutorials and articles.

Best regards,

#3 dipesh

dipesh

    Newbie [ Level 2 ]

  • Members
  • 10 posts
  • Gender:Male
  • Location:uk

Posted 19 April 2009 - 07:22 AM

hi, thank you for your replay, the reason i not using ado.net is because my uni course has not covered it.

#4 TavoxPeru

TavoxPeru

    Super Member

  • [HOSTED]
  • 876 posts
  • Gender:Male
  • Location:Lima - Peru
  • Interests:Web and Software development, Internet, Computers, Electronic music, music, soccer.
  • myCENTs:13.21

Posted 25 April 2009 - 05:01 AM

Ok and you are welcome, now relating to your problem, do you find the solution to it??? if not, you can do the following:
  • Download and install the MySql Connector/NET:
    MySQL Connector/NET can be downloaded from MySql Connector/NET. Download to your hard disk the zip file which includes the installer and unzip it. Then to start the installation process simply double click that file. Finally make a complete installation on the default folder or to any folder that you wish.
  • Start Visual Basic and create a new project and a new form, you can use the default settings or make any change that you want.
  • Add References:
    Before you begin using Connector/NET with Visual Basic, we need to add a reference to our project. By adding a reference, we are telling to Visual Basic where to find MySQL Connector/NET to access the objects and methods of Connector/NET.

    To add a reference to your project, choose Add Reference... (Add Reference...) from the Project menu (Project). Choose the Browse tab (Browse) and locate the folder where your installation of Connector / NET is located. Finally select the file MySql.Data.dll and the Connector / NET will be added to your project.
  • Save your form and project, and then complete your form as you wish. To use a Connection object, first you need to define it as MySql.Data.MySqlClient.MySqlConnection or you can use the Imports sentence. If you add Imports MySql.Data.MySqlClient at the top of the source code, you can be able to refer the Connector/NET object as MySqlConnection. After this add and instantiate a MySqlConnection object as you do with any other object in Visual Basic, and replace the Connection string posted above with the following:

    ConnectionString = "server=localhost;" & _
    "user id=UserName;" & _
    "password=Password;" & _
    "database=DatabaseName"
If you have any problem please let me know.

Best regards,



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users