aldoblack
aldoblack

Reputation: 175

Get the biggest number in database column

I'm trying to get the biggest number in database like in the photo

enter image description here

For example the biggest number is 31, and the when I click a button it will increment with one. For example if a press button ADD in database it will be added number 32. I want to do it programmatically.

Thank You.

Upvotes: 0

Views: 70

Answers (2)

Developerzzz
Developerzzz

Reputation: 1126

ok dear this is the simple solution

SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = "SELECT (ISNULL(MAX(yourcolumn), 0) + 1) as BigNum FROM yourtable";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader = cmd.ExecuteReader();
    if (reader.Read())
{
    int bingnum =Convert.ToInt32( reader["BigNum"]);
}
sqlConnection1.Close();

Upvotes: 1

Developerzzz
Developerzzz

Reputation: 1126

dear friend in sql server simple you can do this

        DECLARE @MaxNum int;
        SELECT @MaxNum  = ISNULL(MAX(yourcolumn), 0) + 1 FROM yourtable

Upvotes: 1

Related Questions