.net sql sever 当 IDENTITY_INSERT 设置为 OFF 时,不能为表 'Archives' 中的标识列插入显式值。 60
publicintAdd(bsslzyjianyu.Model.Archivesmodel){StringBuilderstrSql=newStringBuilder()...
public int Add(bsslzyjianyu.Model.Archives model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("insert into Archives(");
strSql.Append("IDCard,Name,Password,Gender,Age,Education,Height,DateOfBirth,Native,Degree,Major,Title,Post)");
strSql.Append(" values (");
strSql.Append("@IDCard,@Name,@Password,@Gender,@Age,@Education,@Height,@DateOfBirth,@Native,@Degree,@Major,@Title,@Post)");
strSql.Append(";select @@IDENTITY");
SqlParameter[] parameters = {
new SqlParameter("@IDCard", SqlDbType.VarChar,50),
new SqlParameter("@Name", SqlDbType.VarChar,50),
new SqlParameter("@Password", SqlDbType.Int,4),
new SqlParameter("@Gender", SqlDbType.VarChar,2),
new SqlParameter("@Age", SqlDbType.Int,4),
new SqlParameter("@Education", SqlDbType.VarChar,50),
new SqlParameter("@Height", SqlDbType.Int,4),
new SqlParameter("@DateOfBirth", SqlDbType.DateTime),
new SqlParameter("@Native", SqlDbType.VarChar,50),
new SqlParameter("@Degree", SqlDbType.VarChar,50),
new SqlParameter("@Major", SqlDbType.VarChar,50),
new SqlParameter("@Title", SqlDbType.VarChar,50),
new SqlParameter("@Post", SqlDbType.VarChar,50)};
parameters[0].Value = model.IDCard;
parameters[1].Value = model.Name;
parameters[2].Value = model.Password;
parameters[3].Value = model.Gender;
parameters[4].Value = model.Age;
parameters[5].Value = model.Education;
parameters[6].Value = model.Height;
parameters[7].Value = model.DateOfBirth;
parameters[8].Value = model.Native;
parameters[9].Value = model.Degree;
parameters[10].Value = model.Major;
parameters[11].Value = model.Title;
parameters[12].Value = model.Post;
object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
if (obj == null)
{
return 0;
}
else
{
return Convert.ToInt32(obj);
}
}在数据库中有一个字段 datapartyid 是自增的 ,但是我的增加语句中没有给datapartyid 赋值, 还是出现错误!
我在数据库里直接添加是可以成功的 在。net里就不行 展开
{
StringBuilder strSql=new StringBuilder();
strSql.Append("insert into Archives(");
strSql.Append("IDCard,Name,Password,Gender,Age,Education,Height,DateOfBirth,Native,Degree,Major,Title,Post)");
strSql.Append(" values (");
strSql.Append("@IDCard,@Name,@Password,@Gender,@Age,@Education,@Height,@DateOfBirth,@Native,@Degree,@Major,@Title,@Post)");
strSql.Append(";select @@IDENTITY");
SqlParameter[] parameters = {
new SqlParameter("@IDCard", SqlDbType.VarChar,50),
new SqlParameter("@Name", SqlDbType.VarChar,50),
new SqlParameter("@Password", SqlDbType.Int,4),
new SqlParameter("@Gender", SqlDbType.VarChar,2),
new SqlParameter("@Age", SqlDbType.Int,4),
new SqlParameter("@Education", SqlDbType.VarChar,50),
new SqlParameter("@Height", SqlDbType.Int,4),
new SqlParameter("@DateOfBirth", SqlDbType.DateTime),
new SqlParameter("@Native", SqlDbType.VarChar,50),
new SqlParameter("@Degree", SqlDbType.VarChar,50),
new SqlParameter("@Major", SqlDbType.VarChar,50),
new SqlParameter("@Title", SqlDbType.VarChar,50),
new SqlParameter("@Post", SqlDbType.VarChar,50)};
parameters[0].Value = model.IDCard;
parameters[1].Value = model.Name;
parameters[2].Value = model.Password;
parameters[3].Value = model.Gender;
parameters[4].Value = model.Age;
parameters[5].Value = model.Education;
parameters[6].Value = model.Height;
parameters[7].Value = model.DateOfBirth;
parameters[8].Value = model.Native;
parameters[9].Value = model.Degree;
parameters[10].Value = model.Major;
parameters[11].Value = model.Title;
parameters[12].Value = model.Post;
object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
if (obj == null)
{
return 0;
}
else
{
return Convert.ToInt32(obj);
}
}在数据库中有一个字段 datapartyid 是自增的 ,但是我的增加语句中没有给datapartyid 赋值, 还是出现错误!
我在数据库里直接添加是可以成功的 在。net里就不行 展开
3个回答
展开全部
太简单了。把表的IDCard 为自动增长给去掉,或者把语句改为:
strSql.Append("insert into Archives(");
strSql.Append("Name,Password,Gender,Age,Education,Height,DateOfBirth,Native,Degree,Major,Title,Post)");
strSql.Append(" values (");
strSql.Append("@Name,@Password,@Gender,@Age,@Education,@Height,@DateOfBirth,@Native,@Degree,@Major,@Title,@Post)");
OK。搞定
strSql.Append("insert into Archives(");
strSql.Append("Name,Password,Gender,Age,Education,Height,DateOfBirth,Native,Degree,Major,Title,Post)");
strSql.Append(" values (");
strSql.Append("@Name,@Password,@Gender,@Age,@Education,@Height,@DateOfBirth,@Native,@Degree,@Major,@Title,@Post)");
OK。搞定
追问
idcard不是自增 自增的datapartyid已经去掉了
展开全部
set IDENTITY_INSERT ArchivesON
IDCard 为自动增长 不需要插入 去掉这一列 即可!!!!
IDCard 为自动增长 不需要插入 去掉这一列 即可!!!!
更多追问追答
追问
idcard不是自增 自增的datapartyid已经去掉了
追答
DbHelperSQL.GetSingle 里面是用 ExecuteScalar 吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询