无法使char_length()函数在Access查询中起作用

时间:2016-09-11 03:36:13

标签: c# database ms-access

我有一个包含很多单词的数据库,我正在努力获得某个字符长度的最后一个字。我有一个名为getWord()的方法,它为我返回一个字符串。对于这个特定代码,我使用5作为我试图过滤的字符长度。我不是SQL的专家,但我尝试研究如何做到这一点,我遇到了char_length()函数。所以我尝试使用它。但是,当我运行我的程序时,它什么也没做。这是我的代码。顺便说一下,我正在使用C#。

我会非常感激任何帮助。感谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.OleDb;

namespace take2
{
    class MainGameControl
    {
        private OleDbConnection connection = new OleDbConnection();

        public MainGameControl()
        {
            connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\RJ\c#\take2  - updated\Take2.accdb;Persist Security Info=False;";
        }

        public string getWord()
        {
            string word = "";
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = "SELECT Words FROM Dictionary WHERE CHAR_LENGTH(Words) = 5 Order by Words DESC;";
            OleDbDataReader reads = command.ExecuteReader();    

            if (reads.Read())
            {
                word = reads.GetString(0);            
            }
            connection.Close();
            return word;
        }        
    }
}

0 个答案:

没有答案