如何使用C#访问中的过滤器选择特定字段 [英] how to select specific field using a filter from access in C#
问题描述
在C#和Windows窗体中,
我有一个这样的数据库:
这就是我使用类将其数据放入datagridview
的方式:
class DBConnection
{
public static void GetList(Form2 frm2)
{
string DBPath = Application.StartupPath;
OleDbConnection Connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+DBPath+@"\DataBase\SampleFeeds.accdb");
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from FeedLibrary", Connection);
DataTable Dtable = new DataTable();
DataA.Fill(Dtable);
frm2.SelectedFeeddataGridView.DataSource = Dtable;
}
}
这是我的表单加载:
private void Form2_Load(object sender, EventArgs e)
{
DBConnection.GetList(this);
}
到目前为止,一切正常.
现在我有一个问题:
例如,我有一个列表框FeedSelectListBox
,
我想当用户单击按钮GrassLegumeForagebtn
我的FeedSelectListBox
时,只用草/豆类饲料类别中的所有饲料名称填充.
我应该怎么做?
在达米尔基的帮助下,我的问题解决了
//-----------------
但是现在我有另一个问题: 我想当用户从列表框中选择一个提要时,将其所有来自数据库的数据(例如名称,编号,提要类型和",,")放入数据网格视图中.
我在SelectFeedbtn
上使用了此代码,但是它不起作用:
private void SelectFeedbtn_Click(object sender, EventArgs e)
{
string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings["FeedLibraryConnectionString"].ConnectionString;
OleDbConnection Connection = new OleDbConnection(StrCon);
string FeedSelectedID = FeedSelectListBox.SelectedValue.ToString();
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from FeedLibrary where ID = 'FeedSelectedID'" , Connection);
DataTable DTable = new DataTable();
DataA.Fill(DTable);
SelectedFeeddataGridView.DataSource = DTable;
}
FeedSelectListBox
的ValueMember
属性是ID,但错误是:
System.Data.dll中发生了类型为'System.Data.OleDb.OleDbException'的未处理异常.
我什至使用此查询,但仍然无法正常工作:
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from FeedLibrary where ID =" FeedSelectListBox.SelectedValue , Connection);
首先,您最先设置FeedSelectListBox
的DisplayMember
和ValueMember
属性,并在点击事件上使用此代码.
您可以将此代码放在GrassLegumeForagebtn
click事件上.
string DBPath = Application.StartupPath;
OleDbConnection Connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+DBPath+@"\DataBase\SampleFeeds.accdb");
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from FeedLibrary where category='Grass / Legume'", Connection);
DataTable Dtable = new DataTable();
DataA.Fill(Dtable);
frm2.FeedSelectListBox .DataSource = Dtable;
in C# and windows Form,
I have a database like this:
and this is how I using a class put it's data into a datagridview
:
class DBConnection
{
public static void GetList(Form2 frm2)
{
string DBPath = Application.StartupPath;
OleDbConnection Connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+DBPath+@"\DataBase\SampleFeeds.accdb");
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from FeedLibrary", Connection);
DataTable Dtable = new DataTable();
DataA.Fill(Dtable);
frm2.SelectedFeeddataGridView.DataSource = Dtable;
}
}
and this is my form load:
private void Form2_Load(object sender, EventArgs e)
{
DBConnection.GetList(this);
}
So far everything is ok.
now I have a question:
for example I have a list box FeedSelectListBox
,
I want to when user click on a button GrassLegumeForagebtn
my FeedSelectListBox
fill with only all of Feed Names that are in the category of Grass / Legume Forage.
how should I do that ?
with help of Damirchi My problem solved
//-----------------
But now I have another question: I want to when user select a feed from list box all of it's data from data base (like name, number, feed type and ,,,) put in a data grid view.
I used this code on my SelectFeedbtn
but it doesn't work :
private void SelectFeedbtn_Click(object sender, EventArgs e)
{
string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings["FeedLibraryConnectionString"].ConnectionString;
OleDbConnection Connection = new OleDbConnection(StrCon);
string FeedSelectedID = FeedSelectListBox.SelectedValue.ToString();
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from FeedLibrary where ID = 'FeedSelectedID'" , Connection);
DataTable DTable = new DataTable();
DataA.Fill(DTable);
SelectedFeeddataGridView.DataSource = DTable;
}
And ValueMember
property of FeedSelectListBox
is ID but the error is :
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll.
I even use this query but it still doesn't work:
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from FeedLibrary where ID =" FeedSelectListBox.SelectedValue , Connection);
at first you most set the DisplayMember
and ValueMember
properties of FeedSelectListBox
and use this code on click event.
you can put this code on GrassLegumeForagebtn
click event.
string DBPath = Application.StartupPath;
OleDbConnection Connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+DBPath+@"\DataBase\SampleFeeds.accdb");
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from FeedLibrary where category='Grass / Legume'", Connection);
DataTable Dtable = new DataTable();
DataA.Fill(Dtable);
frm2.FeedSelectListBox .DataSource = Dtable;
这篇关于如何使用C#访问中的过滤器选择特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!