filmov
tv
Count Number Of Records from database Using ExecuteScalar In C#.Net Windows Application. (with code)
![preview_player](https://i.ytimg.com/vi/hx8NhZn9p8E/maxresdefault.jpg)
Показать описание
Learn to Count Number Of Records from database Using ExecuteScalar In C#.Net Windows Application. (with code)
Button Click Method :-
//connect to the database
string str = @"";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd;
//create a command
string query = "SELECT COUNT(brand_id) FROM brands";
try
{
con.Open();
label1.ForeColor = Color.Green;
label1.Text = "Database Sucessfully Connected!!";
cmd = new SqlCommand(query, con);
//read from db
Int32 rows_count = Convert.ToInt32(cmd.ExecuteScalar());
cmd.Dispose();
con.Close();
//Display data on the page
label2.Text = "No. of Brand = " +rows_count.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
Button Click Method :-
//connect to the database
string str = @"";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd;
//create a command
string query = "SELECT COUNT(brand_id) FROM brands";
try
{
con.Open();
label1.ForeColor = Color.Green;
label1.Text = "Database Sucessfully Connected!!";
cmd = new SqlCommand(query, con);
//read from db
Int32 rows_count = Convert.ToInt32(cmd.ExecuteScalar());
cmd.Dispose();
con.Close();
//Display data on the page
label2.Text = "No. of Brand = " +rows_count.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
Комментарии