Executing a Parameterized Query with OleDbCommand : OleDbCommand « ADO.Net « C# / CSharp Tutorial






using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;

    class Program
    {
        static void Main(string[] args)
        {
            string oledbConnectString = "Provider=SQLOLEDB; Data Source=(local);" +
                "Integrated security=SSPI;Initial Catalog=AdventureWorks;";

            string oledbSelect = "SELECT * FROM Sales.SalesOrderHeader WHERE TotalDue > ?";

            OleDbConnection oledbConnection = new OleDbConnection(oledbConnectString);
            OleDbCommand oledbCommand = new OleDbCommand(oledbSelect, oledbConnection);

            oledbCommand.Parameters.Add("@TotalDue", OleDbType.Currency);

            oledbCommand.Parameters["@TotalDue"].Value = 200000;

            OleDbDataAdapter oledbDa = new OleDbDataAdapter(oledbCommand);
            DataTable oledbDt = new DataTable( );
            oledbDa.Fill(oledbDt);

            foreach (DataRow row in oledbDt.Rows){
                Console.WriteLine(row["SalesOrderID"]);
                Console.WriteLine(row["OrderDate"]);
                Console.WriteLine(row["TotalDue"]);
            }
        }
    }








32.7.OleDbCommand
32.7.1.OleDbCommand: Execute Scalar
32.7.2.Connect Table to OleDbCommand using CommandType.TableDirect
32.7.3.Execute Non Query
32.7.4.Use OLE DB Data Provider
32.7.5.Executing a Parameterized Query with OleDbCommand