这些查询之间的区别是什么?

时间:2016-04-06 06:37:28

标签: c# ms-access

关注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;
}

我该怎么办?

1 个答案:

答案 0 :(得分:2)

首先,ID是一个i​​nt,所以它不应该在引号''中传递。您在查询中将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);