ADO.NET
ExecuteScalar method is used to execute SQL Commands or storeprocedure, after executing return a single value from the database. It returns the first column of the first row in the result set from a database.
SqlDataAdadpter
try
{
string ConString = "data source=.; database=StudentDB; integrated security=SSPI";
using (SqlConnection connection = new SqlConnection(ConString))
{
SqlDataAdapter da = new SqlDataAdapter("select * from student", connection);
//Using Data Table
DataTable dt = new DataTable();
da.Fill(dt);
Console.WriteLine("Using Data Table");
foreach (DataRow row in dt.Rows)
{
Console.WriteLine(row["Name"] +", " + row["Email"] + ", " + row["Mobile"]);
}
Console.WriteLine("---------------");
//Using DataSet
DataSet ds = new DataSet();
da.Fill(ds, "student");
Console.WriteLine("Using Data Set");
foreach (DataRow row in ds.Tables["student"].Rows)
{
Console.WriteLine(row["Name"] + ", " + row["Email"] + ", " + row["Mobile"]);
}
}
}
catch (Exception e)
{
Console.WriteLine("OOPs, something went wrong.\n" + e);
}
Console.ReadKey();