OleDbDataReader返回空值

时间:2014-02-19 10:16:08

标签: c# sql

我有几天时间想出解决问题的办法,但事实并非如此。运行以下代码时:

OleDbConnection MyconnectionBDD = null;
MyconnectionBDD = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + bdd.Text);
//MyconnectionBDD = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + bdd.Text);

MyconnectionBDD.Open();
OleDbCommand cmdBDD = MyconnectionBDD.CreateCommand();
OleDbDataReader dbReaderBDD = null;
string queryBDD;

queryBDD = "SELECT MAX(Clientes.Codigo) AS CodEmp FROM Clientes WHERE Clientes.Codigo LIKE '" + int.Parse(dbReaderExcel.GetValue(0).ToString().Substring(3, 2)) + "*'";
//queryBDD = "SELECT MAX(Clientes.Codigo) FROM Clientes WHERE Codigo LIKE '17*'";
cmdBDD.CommandText = queryBDD;
cmdBDD.CommandType = CommandType.Text;

dbReaderBDD = cmdBDD.ExecuteReader();

if (dbReaderBDD.HasRows)
{

    dbReaderBDD.Read();
    //string codEmp = dbReaderBDD.GetString(0); //GetValue(0).ToString();
    MessageBox.Show(dbReaderBDD["CodEmp"].ToString());
    MessageBox.Show(dbReaderBDD.GetValue(0).ToString()); //GetInt64(0).ToString());
    //if (dbReaderBDD.GetValue(0).ToString() != "")
    if (!String.IsNullOrEmpty(dbReaderBDD.GetValue(0).ToString()))

        if (dbReaderBDD.GetValue(0).ToString().Length == 6)

            last_codigoCli = int.Parse(dbReaderBDD.GetValue(0).ToString().Substring(1, 5));

        else

            last_codigoCli = int.Parse(dbReaderBDD.GetValue(0).ToString().Substring(2, 5));

    last_codigoCli_Revisado = last_codigoCli;
}

dbReaderBDD.Close();

它返回一个空值,但如果我在Access中运行查询,则返回一个正确的值:

SELECT MAX(Clientes.Codigo) AS CodEmp FROM Clientes WHERE Clientes.Codigo LIKE '17*'

有人可以帮帮我吗?感谢

1 个答案:

答案 0 :(得分:0)

抱歉,查询将是:

queryBDD =“SELECT MAX(Clientes.Codigo)AS CodEmp FROM Clientes WHERE Clientes.Codigo LIKE'”+ int.Parse(dbReaderExcel.GetValue(0).ToString()。Substring(3,2))+“<强>%'“;

由于*是Access中的通配符,我认为查询必须放*但是%

由于