Mega Code Archive

 
Categories / C# Tutorial / ADO Net
 

Executing a Parameterized Query with SqlCommand

using System; using System.Data; using System.Data.SqlClient; using System.Data.OleDb;     class Program     {         static void Main(string[] args)         {             string sqlConnectString = "Data Source=(local);Integrated security=SSPI;Initial Catalog=AdventureWorks;";             string sqlSelect = "SELECT * FROM Sales.SalesOrderHeader WHERE TotalDue > @TotalDue";             SqlConnection sqlConnection = new SqlConnection(sqlConnectString);             SqlCommand sqlCommand = new SqlCommand(sqlSelect, sqlConnection);             sqlCommand.Parameters.Add("@TotalDue", SqlDbType.Money);             sqlCommand.Parameters["@TotalDue"].Value = 200000;             SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCommand);             DataTable sqlDt = new DataTable( );             sqlDa.Fill(sqlDt);             foreach (DataRow row in sqlDt.Rows){                 Console.WriteLine(row["SalesOrderID"]);                 Console.WriteLine(row["OrderDate"]);                 Console.WriteLine(row["TotalDue"]);             }         }     }