简体   繁体   中英

c# - Random generating of data from ms access database

I am trying to make a C# program that will select a random data from my ms access database by clicking a button.. The error says that Data type mismatch in criteria expression. from the OleDbDataReader that i created..

This is what i currently achieved. Answers will be highly appreciated.

    {

        OleDbConnection connection = new OleDbConnection();

        connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\miguel\Documents\QuotesGenFunny.accdb;
        Persist Security Info=False;";

        connection.Open();

        OleDbCommand command = new OleDbCommand();

        command.Connection = connection;
        command.CommandText = "SELECT TOP 1 * FROM Funny ORDER BY Rnd(-10000000*TimeValue(Now())*[Author])";

        OleDbDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            listBox1.Items.Add(reader["Author"].ToString());
        }

        connection.Close();
    }

Your seem to be trying to order your records by a randome value, which I don't think makes any sense.

Maybe it would be easier to skip a random number of records (just make sure it is less than the total number of records), then take the next record after that.

If your records have an ID that is is a number that increases by 1, you could probably just select a random record by generating a random number between 0 and the total nr of posts using C#.

How to get random record from MS Access database

Check comments. It seems the correct syntax in your case is

ORDER BY Rnd(-(10000000*Funny)*Time())

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