Click here to Skip to main content
15,846,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Forum,

What I want is to get info into to a textbox from the mysql database when the user clicks a button.

So far I have it so it connects and then opens up. Then you can send querys from the textbox to the database.

I'm having trouble in fetching/retriving data and putting into a textbox or table.

What I have so far:

Using
C++
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace MySql::Data::MySqlClient;



The Query
C++
//Run The Query (Works like a charm)

private: System::Void RunQuery_Click(System::Object^  sender, System::EventArgs^  e) 
{
String^ SQLQuery = QueryBox->Text;
String^ connectionInfo = "datasource=localhost;port=3306;username=root;password=blacklegion;database=testing";
MySqlConnection^ conn = gcnew MySqlConnection(connectionInfo);
MySqlCommand^ connCMD = gcnew MySqlCommand(SQLQuery, conn);
MySqlDataReader^ dataReader;

try
{
	conn->Open();
	dataReader = connCMD->ExecuteReader();
}

catch(Exception^ex)
{
	MessageBox::Show(ex->Message);
}
//[End]
}


Reason why I'm trying to get it to work is that I have a Cpu Auth which i'm going to incorperate with the DB conneciton and fetch Keys within the db.

I need to some how fetch the keys and read it from the db.


This is the cpu auth
C++
void CpuKeyCheck() 
    {
	BYTE hvCpuFuse1[0x10];
	BYTE cpuKeyBytes[0x10];
//Each CPUKEY need too add 0x0D, 0x57 and so on til all digits are in the text below
	//CPUKEY D33DC71DA1289D6E1E534BB54438AO8B - Developer
	
BYTE CPUKey[0x10] = 
{
0x22, 0x66, 0x39, 0xDC, 0x2D, 0xE7, 0x25, 0x9F0, 0x91, 0xE6, 0x88, 0xDC, 0x1E, 0xDD, 0x78, 0x5A 
};

	HvPeekBytes(0x20, cpuKeyBytes, 0x10);
	QWORD f1p1 = HvGetFuseLine(3) | HvGetFuseLine(4);
	QWORD f1p2 = HvGetFuseLine(5) | HvGetFuseLine(6);
	memcpy(hvCpuFuse1, &f1p1, 8);
	memcpy(hvCpuFuse1 + 8, &f1p2, 8);
	if (memcmp(hvCpuFuse1, cpuKeyBytes, 0x10) != 0)
    HalReturnToFirmware(HalResetSMCRoutine);
    if (memcmp(hvCpuFuse1, CPUKey, 0x10) != 0) 
    HalReturnToFirmware(HalResetSMCRoutine);
}




Thanks in advanced for anyone to help me or guide me =],



Sir

What I have tried:

I've tried google but I don't know what I'm looking for , silly I know but it's why I need help.
Posted
Updated 4-Jun-16 5:00am

I'm not exactly sure what you're looking for. One 'complication' I see is that you're getting your SQL query from a ?textbox (QueryBox->Text) which may mean you dont know how many columns to expect back in the dataReader results (or what type they are)

The general approach would be

C++
while (dataReader->Read())
{
  // do something with this row
}


you would then use (within the while dataReader loop)

C++
dataReader->GetString(column#)
dataReader->GetInt32(column#)


To get the values from the columns.

So, going back to the original issue, the dataReader has a FieldCount property, so, that's the number of columns, you can for-loop through them.

Maybe dataReader->GetFieldType(#column) will be of use, or, read all the columns as strings and convert them to int etc only if you need to

A good ref is MySqlDataReader Class[^]

... is that the sort of help you need ? (I'm not sure what relevance your 'cpu auth' has if any, you havnt explained its connection/relevance terribly well)
 
Share this answer
 
Comments
Syngravez 4-Jun-16 10:02am    
Thank you for the help Garth , much appreciated!

I've posted my soultion still tryin' to get the textbox to get all of them though. Only grabs the last key.
My Solution:

C#
private: System::Void GetTable_Button_Click(System::Object^  sender, System::EventArgs^  e)
{
	
	String^ connectionInfo = "datasource=localhost;port=3306;username=root;password=blacklegion;database=testing";
	MySqlConnection^ conn = gcnew MySqlConnection(connectionInfo);
	MySqlCommand^ connCMD = gcnew MySqlCommand("SELECT * FROM testing.cpu_keys;", conn);
	MySqlDataReader^ dataReader;

	conn->Open();
	dataReader = connCMD->ExecuteReader();
	
	while(dataReader->Read())
	{
    String^ vName;
    vName= dataReader->GetString("cpu_keys");
	comboBox1->Items->Add(vName); // <- Grabs the keys!
	//ReadTableBox->Text=(vName); // <- Only gets 1 though.
	}
// [End]
}
 
Share this answer
 
Comments
Garth J Lancaster 4-Jun-16 20:34pm    
this should have been posted back as an update to the original question, not a solution (please take note for next time)

you say the dataReader only gets one key/the last key ? I was going to say 'make sure you're appending the details to the textbox' but I see you're using a comboBox now .... so I'm not sure what the issue is with that, it 'looks ok' (and I checked the MS docco)

you're so close, dont give up - of course, I take it you've checked the obvious using MySQL workbench or another MySQL tool, that testing.cpu_keys actually contains more than one entry ? .. I always find it handy to whip up a quick console project in times like this to read/dump to the console to check, if Im too lazy to fire up a bigger tool ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900