【SQLServer2008】之改变主键当为null时也不会报错,可以入数据库。

在SqlServer红框中设置主键,右键会有添加主键选项,并且设置不能为null

 

当我们插入主键数据如果为null时,会插不进去,这时候我们需要修改一下,如下图:

“标识规范”中选择“是”,就可以了。当我们插入的数据为null时,不会报错。

下图我没设置插入主键。

/// <summary>
        /// 增加一条数据
        /// </summary>

        public int Add(string bugstyle, int submitterid, int projectid, string bugdescribe, DateTime submittime, string dealresult, byte[] image)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [Bug_test] (");
            strSql.Append("BugStyle,SubmitterID,ProjectID,BugDescribe,SubmitTime,DealResult,Image)");
            strSql.Append(" values (");
            strSql.Append(" @bugStyle,@submitterID,@projectID,@bugDescribe,@submitTime,@dealResult,@image) ");
            strSql.Append(";select @@IDENTITY");

            SqlParameter[] parameter = { 
                    new SqlParameter("@bugStyle",SqlDbType.VarChar,50),
                    new SqlParameter("@submitterID",SqlDbType.Int,10),
                    new SqlParameter("@projectID",SqlDbType.Int,10),
                    new SqlParameter("@bugDescribe",SqlDbType.VarChar,100),
                    new SqlParameter("@submitTime",SqlDbType.DateTime),
                    new SqlParameter("@dealResult",SqlDbType.VarChar,50),
                    new SqlParameter("@image",SqlDbType.Image)};
            parameter[0].Value = bugstyle;
            parameter[1].Value = submitterid;
            parameter[2].Value = projectid;
            parameter[3].Value = bugdescribe;
            parameter[4].Value = submittime;
            parameter[5].Value = dealresult;
            parameter[6].Value = image;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameter);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

 

转载于:https://www.cnblogs.com/Owen-ET/p/6016464.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值