none
Linking to database to execute query written in a text box. RRS feed

  • Question

  • Hello all. I use Visual studio 2010. I have a query as follows:  While writing code (in VC++, Windows form) trying to make a small application similar to database engines for writing and executing queries, the statement used is:

    MySqlCommand cmdDataBase=gcnew MySqlCommand("CREATE TABLE Countries(CountryName char) ,conDataBase);

    or any query before 'conDataBase'.

    What I need is any code snippet which without passing the query in the code as above, allows writing query in the application's window and display result in it on execution. (I made a window with text box for writing query and execute query button; and an output box for displaying result).

    Tuesday, August 25, 2015 7:13 AM

Answers

  • Hi Humaira Syed,

    According to your error message, please check your connection string is right. Table a look below link to check your connection string.
    http://www.connectionstrings.com/mysql-connector-net-mysqlconnection/
    In addition, by review your code, you don't need to use both MySqlDataReader and MySqlDataAdapter. Try use below code.

    MySqlConnection^ conDataBase = gcnew MySqlConnection("your_connection_string");
    MySqlDataAdapter^ sda = gcnew MySqlDataAdapter(txtQuery->Text, conn);
    DataTable^ dbdataset = gcnew DataTable();
    sda->Fill(dbdataset);
    dataGridView1->DataSource = dbdataset;

    If the code can't work, please set a break point on your code, run your code step by step and told use which line of code cause the exception.

    Best Regards,
    Li Wang

    • Proposed as answer by Kristin Xie Friday, August 28, 2015 1:30 AM
    • Marked as answer by Humaira Syed Friday, September 4, 2015 6:25 AM
    Thursday, August 27, 2015 2:14 AM
    Moderator

All replies

  • If you're just trying to create a small 'query results' page then just put a textbox and a grid for select result or multiline textbox for text retrieved e.g. from SQLClient.SQLCommand and SQLClient.SQLConnection respectfully.

    Be careful though since you have no control over what the user types it's a huge security risk to implement this within your application.



    "If there's nothing wrong with me, maybe there's something wrong with the universe!"

    Tuesday, August 25, 2015 7:34 AM
  • Thank you. As I am new to VC++ programming, can you please provide any example code for that?

    I have connected to MySql database.

    Tuesday, August 25, 2015 8:02 AM
  • Hi Humaira Syed,

    I put two TextBoxes on form, one is named txtQuery and the other is named txtResult. When user enter sql in textQuery, we could use below code to run the sql.

    MySqlConnection^ conn;
    MySqlCommand^ cmd;
    conn = gcnew MySqlConnection("your_connection_string");
    cmd = gcnew MySqlCommand(txtQuery->Text, conn);
    conn->Open();
    //Run sql entered in TextBox and show result in txtResult
    txtResult->Text = cmd->ExecuteNonQuery().ToString();
    conn->Close();

    For more information about how to use Ado.net in C++, link below is for your reference.http://www.codeproject.com/Articles/2659/ADO-NET-Primer-using-Managed-C

    Best Regards,
    Li Wang

    Wednesday, August 26, 2015 7:51 AM
    Moderator
  • Thank you @LiWang. But I am unable to display data in result form. When trying to execute SELECT query, i got output as '-1'.

    I tried the below code to display output data in my result window.  

    MySqlDataReader^ myReader;	 
    
    		conDataBase->Open();
    
    		myReader=cmdDataBase->ExecuteReader();
    
    		MySqlDataAdapter^ sda = gcnew MySqlDataAdapter();
    		sda->SelectCommand = cmdDataBase;
    		DataTable^ dbdataset = gcnew DataTable();
    		sda->Fill(dbdataset);
    		BindingSource^ bsource = gcnew BindingSource();
    
    		bsource->DataSource = dbdataset;
    		dataGridView1->DataSource = bsource;
    		sda->Update(dbdataset);

    Prior to using this code, I din't get any errors. Now, I am getting error stating: 'Connection must valid and open'.

    How to get through this?

    Wednesday, August 26, 2015 1:13 PM
  • Hi Humaira Syed,

    According to your error message, please check your connection string is right. Table a look below link to check your connection string.
    http://www.connectionstrings.com/mysql-connector-net-mysqlconnection/
    In addition, by review your code, you don't need to use both MySqlDataReader and MySqlDataAdapter. Try use below code.

    MySqlConnection^ conDataBase = gcnew MySqlConnection("your_connection_string");
    MySqlDataAdapter^ sda = gcnew MySqlDataAdapter(txtQuery->Text, conn);
    DataTable^ dbdataset = gcnew DataTable();
    sda->Fill(dbdataset);
    dataGridView1->DataSource = dbdataset;

    If the code can't work, please set a break point on your code, run your code step by step and told use which line of code cause the exception.

    Best Regards,
    Li Wang

    • Proposed as answer by Kristin Xie Friday, August 28, 2015 1:30 AM
    • Marked as answer by Humaira Syed Friday, September 4, 2015 6:25 AM
    Thursday, August 27, 2015 2:14 AM
    Moderator
  • Thank you @LiWang. The code worked. I am able to display the data in the grid.

    Thanks.



    Thursday, August 27, 2015 5:53 AM