我今天自己建了个留言板,在往数据库插入数据时出现以下提示:
无法将 NULL 值插入列 'uid',表 'test.dbo.liuyanban';该列不允许空值。INSERT 失败。
语句已终止。 我的表本有四个字段,但是我只向其中插入了三个字段,所以就报错了。我想让uid这个字段自动增加,该怎么解决啊?我的数据库代码为:
create table liuyanban
(uid int primary key not null,
uname nvarchar(20),
 title nvarchar(50),
 content nvarchar(200)
)
插入代码:
sqlcom = "insert into liuyanban(uname,title,content) values('" + username + "','" + usertitle + "','" + usercontent + "')";
 SqlCommand cmd = new SqlCommand(sqlcom,con);
   cmd.ExecuteNonQuery();

解决方案 »

  1.   

    ID可以变成自动增涨的嘛.
    那插入的时候,就可以不用insert ID了.
      

  2.   

    我想让uid这个字段自动增加,该怎么解决啊?数据哭里把他设为标识字段,设个你想要的自增量 
    SQL插入的时候就不用管他了哈
      

  3.   

    同楼上.   uid 要 int类型
      

  4.   

    create table liuyanban
    (uid int identity(1,1) not null,
    uname nvarchar(20),
     title nvarchar(50),
     content nvarchar(200)
    ) on primary
      

  5.   

    楼上正解,或者手动创建数据表时,uid数据类型int,其标识设为“是”即可
      

  6.   

    设计表的时候,把uid设为 自动编号 就行了!