سلام دوست عزیز
این اخرین رکورد رو برمیگردونه
select top 1 * from persons order by ID desc
برای جمع زدنش هم
اینطوری عمل کنید
int id=1;
id= (int)cmd1.ExecuteScalar();
id+=id;
یه مثال دیگه
SqlConnection cn = new SqlConnection("Data Source=Sourcename; initial catalog=LOGIN;Integrated security=true");
SqlCommand cmd = new SqlCommand("SELECT User_Name,User_Pass FROM User_LoginInfo where user_name='" + txtUserName.Text + "'", cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
Label1.Text = "Please User other UserName this name is already In System.";
dr.Close();
}
else
{
dr.Close();
string s = "insert into dbo.User_LoginInfo(User_Name,User_Pass) values(@uname,@upass)" +"Select Scope_Identity()";
int id;
cn.Close();
SqlConnection cn1 = new SqlConnection("Data Source=01-M4400-NPPATE; initial catalog=LOGIN;Integrated security=true");
SqlCommand cmd1 = new SqlCommand(s,cn1);
cmd1.Parameters.AddWithValue("@uname", UserName.Text);
cmd1.Parameters.AddWithValue("@upass", txtUserPass.Text);
cn1.Open();
id= (int)cmd1.ExecuteScalar();
cmd1.Dispose();
cn.Close();
cn1.Close();