Thursday, 27 June 2013

Executing a Parameterized Query with SqlCommand in ASP. NET

Executing a Parameterized Query with SqlCommand in ASP. NET


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    SqlConnection newConnection = new 
    SqlConnection(@"Data Source=R4R-3EFD13BB468\SQLEXPRESS; 
    initial catalog=newDatabase;integrated security=true;");
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlCommand sqlCommand = new 
        SqlCommand("select * from emp_tbl where emp_id > @id",newConnection);
        sqlCommand.Parameters.Add("@id", SqlDbType.Int).Value=123;
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCommand);
        DataTable sqlDt = new DataTable();
        sqlDa.Fill(sqlDt);
        foreach (DataRow row in sqlDt.Rows)
        {
            Response.Write(row["nme"]);
            Response.Write(row["emp_id"]);
            Response.Write(row["age"]);
        }
    }
}
Out Put:

No comments: