Autoincrement of Primary Key in SQL Server database
I have added a SQL Server database. I add PK (bignit
) as primary key. I want to set it to auto increase the no. by 1 as we can do in SSCE database. How i开发者_JAVA技巧s that possible.
Thanks Furqan
You'll need to make your PK an Identity.
in query like this.
CREATE TABLE dbo.example
(
EgId INT NOT NULL IDENTITY (1, 1)
) ON [PRIMARY]
also look for more.
http://blog.sqlauthority.com/2009/05/03/sql-server-add-or-remove-identity-property-on-column/
int MaxCode = 0;
DataTable DataTable = new DataTable();
string sql = "SELECT isNull(Max(ColumnName),0) AS MaxCode FROM Table
using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection))
{
sqlConnection.Open();
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand);
sqlAdapter.Fill(DataTable);
if (Convert.ToInt32(DataTable.Rows[0]["MaxCode"]) != 0)
MaxCode = Convert.ToInt32(DataTable.Rows[0]["MaxCode"]) + 1;
else
MaxCode = 1;
sqlAdapter.Dispose();
}
精彩评论