如何为sql表中一个自增字段添加记录
string time1 = DateTime.Now.ToString( "yyyy-MM-dd ");
string time2 = DateTime.Now.AddMonths(2).ToString( "yyyy-MM-dd ");
string cmdtext1 = "insert into lendrtnrec(id,userid,bookid,lendtime,bertntime)values( ' " + ?????? + " ' "+ readerid + " ', ' " + bookid + " ', ' " + time1 + " ', ' " + time2 + " ') ";
id字段我想让它成为自增的,可是不会弄,同时,增加记录的时候,?????处我不知道如何写,谁能教教我啊?
------解决方案--------------------重新来过
SqlCommand cmd = new SqlCommand( "select max(id) from lendrtnrec ", yourconnectionstring);
int i = Convert.ToInt32(cmd.ExecuteScalar().ToString())+1;
你的???的地方就用i代替
------解决方案--------------------string time1 = DateTime.Now.ToString( "yyyy-MM-dd ");
string time2 = DateTime.Now.AddMonths(2).ToString( "yyyy-MM-dd ");
string cmdtext1 = "declare @id as int ";
cmdtext1 += "select @id=max(id)+1 from lendrtnrec ";
cmdtext1 += "insert into lendrtnrec(id,userid,bookid,lendtime,bertntime)values(@id, "+ readerid + " ', ' " + bookid + " ', ' " + time1 + " ', ' " + time2 + " ') ";