关注how to select specific feid using a filter from access in C#
我有另一个问题:
我记得这个方法是SelectFeedbtn_Click
的点击事件,它有效:
public static void GetSelectedFeed(Form2 frm2)
{
string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings["FeedLibraryConnectionString"].ConnectionString;
OleDbConnection Connection = new OleDbConnection(StrCon);
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from FeedLibrary", Connection);
DataTable DTable = new DataTable();
DataA.Fill(DTable);
frm2.SelectedFeeddataGridView.DataSource = DTable;
}
但是当我想从FeedSelectListBox
获取DisplayMember
是Feed名称/描述且ValueMember
是ID时,它会显示一个例外:{"Data type mismatch in criteria expression."}
,
查询是:
public static void GetSelectedFeed(Form2 frm2)
{
string StrCon = System.Configuration.ConfigurationManager.ConnectionStrings["FeedLibraryConnectionString"].ConnectionString;
OleDbConnection Connection = new OleDbConnection(StrCon);
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from FeedLibrary where ID = 'frm2.FeedSelectListBox.SelectedValue'", Connection);
DataTable DTable = new DataTable();
DataA.Fill(DTable);
frm2.SelectedFeeddataGridView.DataSource = DTable;
}
我该怎么办?
答案 0 :(得分:2)
首先,ID是一个int,所以它不应该在引号''
中传递。您在查询中将frm2.FeedSelectListBox.SelectedValue
作为字符串传递,但您应该在查询中传递其值,以便对其进行评估并使您的查询变为可执行文件。
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from FeedLibrary where ID = "+frm2.FeedSelectListBox.SelectedValue, Connection);
OleDbDataAdapter DataA = new OleDbDataAdapter("Select * from FeedLibrary where ID = "+FeedSelectedID, Connection);