简体   繁体   English

执行随机SQL查询并将结果输入到datagridview

[英]Execute a random SQL query and get the result into datagridview

I am using C# and MySQL 我正在使用C#和MySQL

So far I've tried to execute SELECT, INSERT, DELETE commands and display result in datagridview using DataSet and MySqlDataAdapter successfully. 到目前为止,我已经尝试执行SELECT,INSERT,DELETE命令并成功使用DataSet和MySqlDataAdapter在datagridview中显示结果。

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** . 但是现在我想1.执行一个随机查询 (无论用户在文本框中键入什么,然后按Execute按钮),并在2. 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) (我尝试搜索,但仅将人们使用SELECT之类的特定命令返回的结果返回给我,我的情况有所不同,查询是由用户随机键入的)

Here is a sample code snippet of binding a query result into a gridview: 这是将查询结果绑定到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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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