查询Access数据库

时间:2013-07-22 15:49:20

标签: c# ms-access oledb

我正在尝试从Access数据库的两个不同表中获取匹配值。我想知道是否有可能从单元格以“SN”开头的列中获取值,并将所有其余数据保留。这是我到目前为止所拥有的。

        String filePath = textBox1.Text;

        con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath);
        con3 = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath);



        if ((string)comboBox1.SelectedItem == "CGA0112")
        {
            try
            {
                string end = "ENDDATE";
                string qual = "CGA0112";
                string start = "START";

                ad.SelectCommand = new OleDbCommand("SELECT* FROM [AC_ECONOMIC] Where KEYWORD = '" + end + "'AND QUALIFIER = '" + qual + "' AND EXPRESSION = '", con); // ISSUE AFTER EXPRESSION= 


                ds.Clear();
                ad.Fill(ds);

                con.Open();
                ad.SelectCommand.ExecuteNonQuery();
                con.Close();

                ad3.SelectCommand = new OleDbCommand("SELECT* FROM [AC_ECONOMIC] Where KEYWORD = '" + start + "'AND QUALIFIER = '" + qual + "'", con3);
                ds3.Clear();
                ad3.Fill(ds3);

                ds.Merge(ds3); //merges the two datasets together


                win1.frm1 = this;
                win1.Show();

                con3.Open();
                ad3.SelectCommand.ExecuteNonQuery();
                con3.Close();

            }

我想我只是想在EXPRESSION区域之后添加一些代码,但我不太清楚从哪里开始。我只是想让它读起来像AND EXPRESSION = startswith(“SN”)。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

您应该使用like,如下所示:

"SELECT * FROM [AC_ECONOMIC] Where KEYWORD = '" + end + 
"' AND QUALIFIER = '" + qual + "' AND EXPRESSION LIKE 'SN*'"

然而,正如评论中提到的那样,我认为你一定要考虑一个框架,也许像Dapper,这非常棒。还有一些库扩展了Dapper,使其更容易上手,比如this one

修改:请参阅评论 - 'SN%'应与OleDb一起使用,而不是'SN*'