如何使用变量从MySql Query获取数据集

时间:2016-04-07 15:23:27

标签: mysql vb.net mysql-helper

我有这样的查询:

SET @a = (SELECT GROUP_CONCAT(Id) FROM MyTable1 WHERE Id < 10);
SELECT * FROM MyTable2  WHERE find_in_set(IdLite, @a); 
SELECT * FROM MyTable3  WHERE find_in_set(IdLite, @a);
SELECT * FROM MyTable4  WHERE find_in_set(IdLite, @a); 

我已尝试使用此代码进行重新调整:

Using ds As DataSet = MySqlHelper.ExecuteDataset(CnStr, SqlStr)

但我收到错误:

  

命令执行期间遇到致命错误。

错误信息是:

  

必须定义参数'@a'。

我也尝试过:

SELECT * FROM MyTable2  WHERE find_in_set(IdLite, 
     @a := (SELECT GROUP_CONCAT(Id) FROM MyTable1 WHERE Id < 10)); 
SELECT * FROM MyTable3  WHERE find_in_set(IdLite, @a);
SELECT * FROM MyTable4  WHERE find_in_set(IdLite, @a); 

但是我得到了同样的错误 将结果导入DataSet的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

 DataSet mydataset =  new DataSet();
 MySqlConnection myConnection = new MySqlConnection();
 myConnection.ConnectionString = "************";
 myConnection.Open();
 string mySelectQuery = "SELECT * FROM table";
 MySqlCommand myCommand = new MySqlCommand(mySelectQuery,myConnection);
 MySqlDataAdapter adapter = new MySqlDataAdapter(myCommand);
 adapter.Fill(mydataset, "table");
 dataGridView1.DataSource = mydataset;
 dataGridView1.DataMember = "table";
 myConnection.Close();

您可以查看以下链接:

  

http://forums.codeguru.com/showthread.php?448008-How-do-i-load-mysql-data-into-a-dataset-then-into-a-datagrid

     

http://www.dotnetheaven.com/article/how-to-load-data-from-database-into-datagridview-in-vb.net

如果我的回答是正确的,请将masrk视为正确。谢谢

答案 1 :(得分:0)

错误在连接字符串中。

解决方案是将;Allow User Variables=True添加到数据库名称。

这样:

CnStr = "datasource=" + Server_Name + _
";username= " + UserDB + _
";password=" + Password +  _
";database=" + Database_Name + ";Allow User Variables=True"