EDN Admin
Well-known member
Ive just created a database class which doesnt seem to be working as it should be. The reason for this class is so that it makes it easier for me to handle a database like I would in PHP.
Im connecting, making a query and then reading... Im receiving output, which is "A first chance exception of type System.NullReferenceException occurred in MySql.Data.dll"
Apart from this issue, there are a few things Id like to change. For example, in the Execute method I would like to return MySqlCommand upon success and false upon failure... but as far as Im aware, you can only return the specified type the method has
to return.
Any help will be appreciated.
This is how I connect, and use my database class.
<pre class="prettyprint Database^ db = gcnew Database();
db->Connect("Network Address=localhost;Persist Security Info=no;User Name=root;Password=mypassword");
MySqlCommand^ query = db->Execute("SHOW DATABASES");
MySqlDataReader^ row = db->Reader(query);
while(row->Read()) {
Debug::WriteLine(row[0]);
}[/code]
And the below, is my database class.
Database.h
<pre class="prettyprint #pragma once
using namespace System;
using namespace MySql:ata::MySqlClient;
using namespace System:iagnostics;
ref class Database
{
private:
bool connected;
MySqlConnection^ con;
public:
Database(void);
bool Connect(String^ dsn);
MySqlCommand^ Execute(String^ sql);
MySqlDataReader^ Reader(MySqlCommand^ query);
};[/code]
Database.cpp
<pre class="prettyprint #include "StdAfx.h"
#include "Database.h"
Database:atabase(void)
{
connected = false;
}
bool Database::Connect(String^ dsn)
{
MySqlConnection^ con = gcnew MySqlConnection(dsn);
try {
con->Open();
}
catch(MySqlException^ ex) {
Debug::WriteLine(ex);
return(false);
}
connected = true;
Debug::WriteLine("connected");
return(true);
}
MySqlCommand^ Database::Execute(String^ sql)
{
MySqlCommand^ cmdExecute;
if(connected == true)
{
try {
MySqlCommand^ cmdExecute = gcnew MySqlCommand(sql, con);
}
catch(MySqlException^ ex) {
Debug::WriteLine(ex);
}
}
return(cmdExecute);
}
MySqlDataReader^ Database::Reader(MySqlCommand^ query)
{
MySqlDataReader^ reader;
try {
reader = query->ExecuteReader();
}
catch(MySqlException^ ex)
{
Debug::WriteLine(ex);
}
return(reader);
}[/code]
View the full article
Im connecting, making a query and then reading... Im receiving output, which is "A first chance exception of type System.NullReferenceException occurred in MySql.Data.dll"
Apart from this issue, there are a few things Id like to change. For example, in the Execute method I would like to return MySqlCommand upon success and false upon failure... but as far as Im aware, you can only return the specified type the method has
to return.
Any help will be appreciated.
This is how I connect, and use my database class.
<pre class="prettyprint Database^ db = gcnew Database();
db->Connect("Network Address=localhost;Persist Security Info=no;User Name=root;Password=mypassword");
MySqlCommand^ query = db->Execute("SHOW DATABASES");
MySqlDataReader^ row = db->Reader(query);
while(row->Read()) {
Debug::WriteLine(row[0]);
}[/code]
And the below, is my database class.
Database.h
<pre class="prettyprint #pragma once
using namespace System;
using namespace MySql:ata::MySqlClient;
using namespace System:iagnostics;
ref class Database
{
private:
bool connected;
MySqlConnection^ con;
public:
Database(void);
bool Connect(String^ dsn);
MySqlCommand^ Execute(String^ sql);
MySqlDataReader^ Reader(MySqlCommand^ query);
};[/code]
Database.cpp
<pre class="prettyprint #include "StdAfx.h"
#include "Database.h"
Database:atabase(void)
{
connected = false;
}
bool Database::Connect(String^ dsn)
{
MySqlConnection^ con = gcnew MySqlConnection(dsn);
try {
con->Open();
}
catch(MySqlException^ ex) {
Debug::WriteLine(ex);
return(false);
}
connected = true;
Debug::WriteLine("connected");
return(true);
}
MySqlCommand^ Database::Execute(String^ sql)
{
MySqlCommand^ cmdExecute;
if(connected == true)
{
try {
MySqlCommand^ cmdExecute = gcnew MySqlCommand(sql, con);
}
catch(MySqlException^ ex) {
Debug::WriteLine(ex);
}
}
return(cmdExecute);
}
MySqlDataReader^ Database::Reader(MySqlCommand^ query)
{
MySqlDataReader^ reader;
try {
reader = query->ExecuteReader();
}
catch(MySqlException^ ex)
{
Debug::WriteLine(ex);
}
return(reader);
}[/code]
View the full article