SaltyDawg
asked on
Return Multiple Columns on MySQL Queries
I have a MySQL Connection that works, but I can only 1 column per query. Here is my Connection Class:
class clsMySqlODBCConnection
{
public int QueryType = 0;
public System.Data.Odbc.OdbcConne ction OdbcCon;
public System.Data.Odbc.OdbcComma nd OdbcCom;
public System.Data.Odbc.OdbcDataR eader OdbcDR;
public string ConStr;
private string MySqlServer = "linux02", MySqlPort = "", MySqlDatabase = "EbtronSystems", MySqlUser = "ebtron", MySqlUserPass = "ebtron", MySqlOption = "3";
public clsMySqlODBCConnection()
{
ConStr = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=" + MySqlServer + ";PORT=" + MySqlPort + ";DATABASE=" + MySqlDatabase + ";UID=" + MySqlUser + ";PWD=" + MySqlUserPass + ";OPTION=" + MySqlOption + "";
OdbcCon = new System.Data.Odbc.OdbcConne ction(ConS tr);
try
{
//txtLog.AppendText("Openn ing connection...\r\n");
if (OdbcCon.State == ConnectionState.Closed)
{
OdbcCon.Open();
}
//txtLog.AppendText("Conne ction opened\r\n");
//MessageBox.Show("Connect ion opened\r\n");
}
catch (System.Data.Odbc.OdbcExce ption Ex)
{
//txtLog.AppendText(Ex.Mes sage + "\r\n");
//MessageBox.Show("Could not access the database.\r\nPlease make sure you completed the fields with the correct information and try again.\r\n\r\nMore details:\r\n" + Ex.Message, "Database connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public void MySqlDisconnection()
{
if (OdbcCon.State == ConnectionState.Open)
{
OdbcCon.Close();
//MessageBox.Show("Connect ed Closed\r\n");
}
}
public string[] MySQLQuery(string qry, int fldidx)
{
List<string> list = new List<string>();
string tStr = null; // the following is not needed: = Convert.ToString(null);
if (OdbcCon.State == ConnectionState.Open)
{
OdbcCom = new System.Data.Odbc.OdbcComma nd(qry, OdbcCon);
if (fldidx >= 0)
{
OdbcDR = OdbcCom.ExecuteReader();
while (OdbcDR.Read())
{
tStr = Convert.ToString(OdbcDR[fl didx]);
list.Add(tStr);
}
}
else
{
OdbcCom.ExecuteNonQuery();
QueryType = 1;
}
}
return list.ToArray();
}
public void MySQLNONQuery(string qry)
{
if (OdbcCon.State == ConnectionState.Open)
{
OdbcCom = new System.Data.Odbc.OdbcComma nd(qry, OdbcCon);
OdbcCom.ExecuteNonQuery();
QueryType = 1;
}
}
public object InitializeCnnObject()
{
// Create and initilize a recordset to represent the User Table
clsMySqlODBCConnection OpenConnection = new clsMySqlODBCConnection();
return OpenConnection;
}
}
////////////////////
I run a query like this:
string[] strQryResults = null;
strQryResults = OpenConnection.MySQLQuery( "SELECT * FROM AdvantageOrderForm WHERE ItemIndexNumber = '" + tmpItemIndexNumber + "'", 0);
I can return only one column I have to run another query in order to get another column:
strQryResults = OpenConnection.MySQLQuery( "SELECT * FROM AdvantageOrderForm WHERE ItemIndexNumber = '" + tmpItemIndexNumber + "'", 1);
I have a table with more than 50 columns, it takes nearly a minute to run all those queries. I need to be able to run 1 query and be able to get results for all the columns.
I know this is all because of my function:
public string[] MySQLQuery(string qry, int fldidx)
But I'm pretty new to DotNet and also to C#, so I'm not sure how to make it work.
Thanks for any help
class clsMySqlODBCConnection
{
public int QueryType = 0;
public System.Data.Odbc.OdbcConne
public System.Data.Odbc.OdbcComma
public System.Data.Odbc.OdbcDataR
public string ConStr;
private string MySqlServer = "linux02", MySqlPort = "", MySqlDatabase = "EbtronSystems", MySqlUser = "ebtron", MySqlUserPass = "ebtron", MySqlOption = "3";
public clsMySqlODBCConnection()
{
ConStr = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=" + MySqlServer + ";PORT=" + MySqlPort + ";DATABASE=" + MySqlDatabase + ";UID=" + MySqlUser + ";PWD=" + MySqlUserPass + ";OPTION=" + MySqlOption + "";
OdbcCon = new System.Data.Odbc.OdbcConne
try
{
//txtLog.AppendText("Openn
if (OdbcCon.State == ConnectionState.Closed)
{
OdbcCon.Open();
}
//txtLog.AppendText("Conne
//MessageBox.Show("Connect
}
catch (System.Data.Odbc.OdbcExce
{
//txtLog.AppendText(Ex.Mes
//MessageBox.Show("Could not access the database.\r\nPlease make sure you completed the fields with the correct information and try again.\r\n\r\nMore details:\r\n" + Ex.Message, "Database connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public void MySqlDisconnection()
{
if (OdbcCon.State == ConnectionState.Open)
{
OdbcCon.Close();
//MessageBox.Show("Connect
}
}
public string[] MySQLQuery(string qry, int fldidx)
{
List<string> list = new List<string>();
string tStr = null; // the following is not needed: = Convert.ToString(null);
if (OdbcCon.State == ConnectionState.Open)
{
OdbcCom = new System.Data.Odbc.OdbcComma
if (fldidx >= 0)
{
OdbcDR = OdbcCom.ExecuteReader();
while (OdbcDR.Read())
{
tStr = Convert.ToString(OdbcDR[fl
list.Add(tStr);
}
}
else
{
OdbcCom.ExecuteNonQuery();
QueryType = 1;
}
}
return list.ToArray();
}
public void MySQLNONQuery(string qry)
{
if (OdbcCon.State == ConnectionState.Open)
{
OdbcCom = new System.Data.Odbc.OdbcComma
OdbcCom.ExecuteNonQuery();
QueryType = 1;
}
}
public object InitializeCnnObject()
{
// Create and initilize a recordset to represent the User Table
clsMySqlODBCConnection OpenConnection = new clsMySqlODBCConnection();
return OpenConnection;
}
}
////////////////////
I run a query like this:
string[] strQryResults = null;
strQryResults = OpenConnection.MySQLQuery(
I can return only one column I have to run another query in order to get another column:
strQryResults = OpenConnection.MySQLQuery(
I have a table with more than 50 columns, it takes nearly a minute to run all those queries. I need to be able to run 1 query and be able to get results for all the columns.
I know this is all because of my function:
public string[] MySQLQuery(string qry, int fldidx)
But I'm pretty new to DotNet and also to C#, so I'm not sure how to make it work.
Thanks for any help
You can only get one column because the clsMySqlODBCConnection class _that you wrote_ (.Net style guildelines recommend against prefixes like 'cls', by the way) only returns one column. If you want more, make your data wrapper class return more. All the columns are there in the data reader.
ASKER
Wow, the prefix "cls" is not good. Never knew that. What would having the prefix do to hurt me?
How do I get the other columns from the data reader? Changes would I have to do to the wrapper
How do I get the other columns from the data reader? Changes would I have to do to the wrapper
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm trying to return the results using the function presented.
////////////////////////// ////////// ////////// ////////// ///////
clsMySqlODBCConnection MySQLConn = new clsMySqlODBCConnection();
IDataReader qryResults = null;
qryResults = MySQLConn.MySQLQuery("SELE CT * FROM `MainIndex` WHERE `SerialNumber` =110477");
qryResults["ItemIndexNumbe r"];
////////////////////////// ////////// ////////// ////////// ///////
Am I getting so far. Or what do I have wrong?
//////////////////////////
clsMySqlODBCConnection MySQLConn = new clsMySqlODBCConnection();
IDataReader qryResults = null;
qryResults = MySQLConn.MySQLQuery("SELE
qryResults["ItemIndexNumbe
//////////////////////////
Am I getting so far. Or what do I have wrong?
You have to read from your datareader before referencing any columns.
ASKER
I'm sorry I'm still not getting this right. By the way I was studying this and found SqlDataReader, whats the difference? Some say SqlDataReader runs faster...
ASKER
Am I anywhere close here:
clsMySqlODBCConnection MySQLConn = new clsMySqlODBCConnection();
MySQLConn.MySQLQuery("SELE CT * FROM `MainIndex` WHERE `SerialNumber` =110477");
MySQLConn.OdbcDR[0];
I'm sure MySQLConn.OdbcDR[0]; is wrong, just don't know where to go from there
clsMySqlODBCConnection MySQLConn = new clsMySqlODBCConnection();
MySQLConn.MySQLQuery("SELE
MySQLConn.OdbcDR[0];
I'm sure MySQLConn.OdbcDR[0]; is wrong, just don't know where to go from there
ASKER
Oh Think I have it now, this seems to work:
string tStr1,tStr2 = null;
string tResults = null;
MySQLConn.MySQLQuery("SELE CT * FROM `MainIndex` WHERE `SerialNumber` =110477");
while (MySQLConn.OdbcDR.Read())
{
tStr1 = Convert.ToString(MySQLConn .OdbcDR["I temIndexNu mber"]);
tStr2 = Convert.ToString(MySQLConn .OdbcDR["S erialNumbe r"]);
MessageBox.Show(tStr1 + " - " + tStr2);
}
string tStr1,tStr2 = null;
string tResults = null;
MySQLConn.MySQLQuery("SELE
while (MySQLConn.OdbcDR.Read())
{
tStr1 = Convert.ToString(MySQLConn
tStr2 = Convert.ToString(MySQLConn
MessageBox.Show(tStr1 + " - " + tStr2);
}
Odbc is generally slower than a native provider. However, SqlDataReader is for SQL Server only. For MySQL I would install the MySQL provider, Connector/Net:
http://www.mysql.com/products/connector/net/
http://www.mysql.com/products/connector/net/
ASKER
Thanks alot