绑定的DataGridView访问SQL查询字符串的结果 [英] Bind DataGridView To Results of Access SQL Query String

查看:312
本文介绍了绑定的DataGridView访问SQL查询字符串的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绑定 DataGridView的来在运行时的文本生成的查询结果。



我如何发送一个查询文本到Microsoft Access和结果绑定到DataGridView?



当用户点击该按钮( ok_btn ),我想文本框的内容( query_txt.Text )发送到Microsoft Access,那么我想在我的DataGridView显示的查询结果( results_grid



简单的一列的查询都很好,现在:(选择一个; 选择A,b; 现在选择();



注意: C#所接受,VB.NET首选


解决方案

 使用System.Data.OleDb; 

OleDbConnection的康恩=新的OleDbConnection(@供应商= Microsoft.Jet.OLEDB.4.0;用户ID =;密码=;数据源=+文件名);
conn.Open();
OleDbDataAdapter的DataAdapter的=新OleDbDataAdapter的(query_txt.Text,康涅狄格州);
的DataSet DS =新的DataSet();
DataAdapter.Fill方法(DS);
dataGridView.DataSource = ds.Tables [0];
conn.Close();

请注意: query_txt.Text 是查询要运行。 文件名是路径到文件的存储位置。


I would like to bind a DataGridView to the results of a query generated as text at runtime.

How can I send a query as text to Microsoft Access and bind the results to the DataGridView?

When the user clicks the button (ok_btn), I want the contents of the textbox (query_txt.Text) sent to Microsoft Access, then I want the results of the query shown in my DataGridView (results_grid).

Simple one-row queries are fine for now: (SELECT "a";, SELECT "a", "b";, SELECT now();)

NOTE: C# accepted, VB.NET preferred

解决方案

using System.Data.OleDb;

OleDbConnection conn = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0;User Id=;Password=;Data Source=" + fileName);
conn.Open();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query_txt.Text, conn);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
dataGridView.DataSource = ds.Tables[0];
conn.Close();

Note: query_txt.Text is the Query that you want to run. fileName is the path to where your file is stored.

这篇关于绑定的DataGridView访问SQL查询字符串的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆