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

New Topic/Question
Reply



MultiQuote




|