Hi!
It doesn't help, because I want to query from C# code. Every time when I want add a row, I call this method to get the next identity value like:
private int GetNextRowID()
{
int NextRowID =0;
try
{
using (SqlConnection ConnectionSql = new SqlConnection(ConnectionString))
{
StringBuilder SqlQuery = new StringBuilder();
SqlQuery.Append("USE [ExampleDatabase]; ");
// Returns 1 so works for the first insert when the table is empty before any insert:
// Returns 1 again for the second insert, so it FAILS:
SqlQuery.Append("SELECT ISNULL(IDENT_CURRENT('ExampleTable'), 0) ;");
SqlCommand CommandSql = new SqlCommand(SqlQuery.ToString(), ConnectionSql);
ConnectionSql.Open();
SqlDataReader DataReaderSql = CommandSql.ExecuteReader();
while (DataReaderSql.Read())
{
NextRowID = int.Parse(DataReaderSql.GetValue(0).ToString());
}
ConnectionSql.Close();
MessageBox.Show("Add Row.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return NextRowID;
}
Thanks
Quote:
Originally Posted by ck9663
Identity auto-increment itself. Why do you need to add one? Anyway, you might want to do a
-
isnull(IDENT_CURRENT('ExampleTable'),0)
-
Good luck!!!
--- CK