locked
How to test the return of OleDbCommand RRS feed

  • Question

  • User-1725652363 posted

    I have a bunch of excel that I should read them, but every file has a specific columns

    OleDbCommand cmd = new OleDbCommand("SELECT [cl1], [cl2] FROM [" + sheet+ "]", connexion);

    The prolem is cmd always has a value not null so I can't test with null and do something like this:

    if(cmd == null){
    cmd = new OleDbCommand("SELECT [l3], [l4] FROM [" + sheet+ "]", connexion);
    }

    Any other test to do?

    Thanks

    Thursday, June 28, 2018 10:30 AM

Answers

  • User1724605321 posted

    Hi kati Ais,

    You can call the ExecuteReader method of a C# Command Object to check the results :

                string connetionString = null;
                OleDbConnection cnn ;
                OleDbCommand cmd ;
                string sql = null;
                OleDbDataReader reader ;
    
                connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Your mdb filename;";
                sql = "Your SQL Statement Here like Select * from product"; 
    
                cnn = new OleDbConnection(connetionString);
                try
                {
                    cnn.Open();
                    cmd = new OleDbCommand(sql, cnn);
                    reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        MessageBox.Show(reader.GetValue(0) + " - " + reader.GetValue(1) + " - " + reader.GetValue(2));
                    }
                    reader.Close();
                    cmd.Dispose();
                    cnn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Can not open connection ! ");
                }

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 29, 2018 3:04 AM