简体   繁体   中英

OleDbDataReader returns empty value

I have a couple of days to come up with a solution to my problem and I can not. When running the following code:

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();

It returned an empty value, but if I run the query in Access it return a correct value:

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

Please can someone help me? Thanks

Sorry, the query would be:

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

As the * is the wildcard in Access, I thought that the query had to put a * but was %

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM