4 Replies - 1206 Views - Last Post: 11 February 2009 - 03:04 PM Rate Topic: -----

#1 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 178
  • Joined: 26-August 08

Connection to MySQL db

Post icon  Posted 11 February 2009 - 01:43 PM

OK, I'm gonna throw something soon and it will probably be my laptop!

I am trying to connect to my MYSQL database in Visual Studio 2008, I have created it and the tables are set up. I can connect to my database using Server Explorer.
I have downloaded and installed the ODBC connector (3.51)
I have also created my connection string which is this:

server=localhost;user id=root;persist security info=True;database=pok3r

This is the code I have so far;

I have my app.config with this;

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
	</configSections>
	<connectionStrings>
		<add name="Pok3rDb.Properties.Settings.pok3rConnectionString"
			connectionString="server=localhost;user id=root;Password=Password12;persist security info=True;database=pok3r"
			providerName="MySql.Data.MySqlClient" />
	</connectionStrings>
</configuration>




I start by calling my database class;

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Pok3rDb
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class mainWindow : Window
	{
		public mainWindow()
		{
			InitializeComponent();   
			database connect = new database();
			connect.ConnectToDatabase();
		}
	}
}



and here is my database class;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Odbc;
using System.Data;
using System.Windows;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Configuration.Assemblies;

namespace Pok3rDb
{
	class database
	{
		private OdbcConnection myConn;
		public void ConnectToDatabase()
		{
			try
			{
				myConn = new OdbcConnection();
				myConn.Open();
			}
			catch (Exception err)
			{
				MessageBox.Show(err.Message, "Problem with connection", MessageBoxButton.OK, MessageBoxImage.Error);
			}
		}
	}
}





But when I run it I get an error telling me my connection strings have not been initialised. I've tried moving things around and now I've changed my code so much I know I'm missing something. I have searched on google and found several different ways to do it but I just cannot seem to get this working.

I have also added the connection to my System DSN setting in Control Centre~Administrative Tools~Data Sources (ODBC)

What am I doing wrong? Can anyone help?

Premier2k

Is This A Good Question/Topic? 0
  • +

Replies To: Connection to MySQL db

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1632
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Connection to MySQL db

Posted 11 February 2009 - 01:51 PM

In your database class I don't see where you're providing the connection string for the connection, thus causing that error. Take a look at this snippet on connecting to mySQL with C#, it may help :)
Was This Post Helpful? 0
  • +
  • -

#3 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 178
  • Joined: 26-August 08

Re: Connection to MySQL db

Posted 11 February 2009 - 02:03 PM

View PostPsychoCoder, on 11 Feb, 2009 - 12:51 PM, said:

In your database class I don't see where you're providing the connection string for the connection, thus causing that error. Take a look at this snippet on connecting to mySQL with C#, it may help :)


Hmmm...ok, I'm going through this now. Do I need the:

	//create your mySQL reeader object
	MySqlDataReader reader;
	//set the command text (query) of the
	//mySQL command object
	cmdMySQL.CommandText = "select * from mycustomers";
	//open the mySQL connection
	cnMySQL.Open();


Because, I dont actually want to query the Db I just want to connect to it.


I'm also getting an error on this line;

MySqlCommand cmdMySQL = new cnMySQL.CreateCommand();


It's telling me: Error 1 The type or namespace name 'cnMySQL' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\paul.rowlands\My Documents\Pok3r DB\Pok3r v0.1\Pok3rDb\Pok3rDb\database.cs 27 45 Pok3rDb
I'm guessing I'm missing a using statement. But which one?

Premier2k
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1632
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Connection to MySQL db

Posted 11 February 2009 - 02:55 PM

Here's what I'm getting at. The snippet was just an example, you need to provide a connection string before you can connect. Simply adding it to the config file does nothing, you have to tell your application what to do. This code is what's causing the error

public void ConnectToDatabase()
 {
			try
			{
				myConn = new OdbcConnection();
				myConn.Open();
			}
			catch (Exception err)
			{
				MessageBox.Show(err.Message, "Problem with connection", MessageBoxButton.OK, MessageBoxImage.Error);
			}
 }



You're telling it to open, but not telling it what to open. Try this

public void ConnectToDatabase()
 {
			try
			{
				string connection = System.Configuration.ConfigurationManager.ConnectionStrings["Pok3rDb.Properties.Settings.pok3rConnectionString"].ConnectionString;
				myConn = new OdbcConnection(connection);
				myConn.Open();
			}
			catch (Exception err)
			{
				MessageBox.Show(err.Message, "Problem with connection", MessageBoxButton.OK, MessageBoxImage.Error);
			}
 }



Notice how I'm passing the OdBcConnection a connection string, that's what you were missing.

The snippet I showed is using the mySQL connector located here

Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#5 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 178
  • Joined: 26-August 08

Re: Connection to MySQL db

Posted 11 February 2009 - 03:04 PM

Yes, I was just reading through your snippet again and also another tutorial I found online and I have come up with this;

namespace Pok3rDb
{
	class database
	{
		public void ConnectToDatabase(string server)
		{
			try
			{
				string connString = "SERVER=" + server + ";" +
					"DATABASE=pok3r;" +
					"UID=root;" +
					"PASSWORD=Password12;";

				MySqlConnection cnMySQL = new MySqlConnection(connString);
				cnMySQL.Open();
			}
			catch (Exception err)
			{
				MessageBox.Show(err.Message, "Problem with connection", MessageBoxButton.OK, MessageBoxImage.Error);
			}
		}
	}
}



This produces no errors and in my other form I'm passing in "localhost" as the server. This seems to run ok, I'm just putting something together to test that it has actually connected. I may also try the other way you have suggested as well. I think (hope) I am getting somewhere with this!

I hate admitting defeat!

Premier2k
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1