简体   繁体   中英

Execute a random SQL query and get the result into datagridview

I am using C# and MySQL

So far I've tried to execute SELECT, INSERT, DELETE commands and display result in datagridview using DataSet and MySqlDataAdapter successfully.

But now I want to 1.execute a random query (whatever user type into a Textbox and press Execute button) and also 2.display result in a datagridview** .

What are these steps needed to archive that task?

(I tried to search but only return me those results in what people are using specific command like SELECT, my case is different, the query is randomly typed by user)

Here is a sample code snippet of binding a query result into a gridview:

DataSet objDataSet = new DataSet();
SqlConnection objConn = new SqlConnection();

SqlCommand objComm = new SqlCommand("SELECT COUNT(*) FROM table", objConn);

SqlDataAdapter objDataAdapter = new SqlDataAdapter(objComm);

if (objConn.State == ConnectionState.Closed)
{
    objConn.Open();
}

objDataAdapter.Fill(objDataSet, strTable);

dataGridView1.DataSource = objDataSet;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM