Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
Connecting To Mysql Databse
Posted 15 April 2009 - 05:01 PM
[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)
'open the connection
Catch ex As Exception
'the program has crashed probably due to mis typing the database name
'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)
'populate the datatable via the data adapter
Catch ex As Exception
'the program has crashed probably due to mis typing the table name
'take a copy of the record structure
mARow = mTableData.NewRow
'close the connection
Posted 17 April 2009 - 10:48 AM
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;" & _
Some resources that may help:
- Using MySQL With .NET/C#/Visual Basic/Powershell
- Define and Store MySQL ADO Connection String in VB.NET 2005.
- Beginning MYSQL 5 with Visual Studio.NET 2005 (pdf)
- VBMySQL.com website.
Posted 25 April 2009 - 05:01 AM
- 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;" & _
Reply to this topic
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users