Tuesday, February 23, 2021

ASP.NET/C#: OleDb example, reading from DB


public void example()
{

    string sql = string.Concat("SELECT f1, f2 FROM table1",
                                               " WHERE id=?");


    using (OleDbConnection connection = new OleDbConnection(AppSettings.getConnectionString()))
    {
        // The insertSQL string contains a SQL statement that
        // inserts a new row in the source table.
        OleDbCommand command = new OleDbCommand(sql, connection);
 

        command.Parameters.AddWithValue("@id", 10);

        // Open the connection and execute the command.
        try
        {
            connection.Open();

            OleDbDataReader reader = command.ExecuteReader();

            while (reader.Read())

            {

                string str1 = reader.GetString(0);

                string str2 = reader.GetString(1); 

                break;

            }
 
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }
}

No comments:

 
Get This <