zoukankan      html  css  js  c++  java
  • 【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);
                }
            }
  • 相关阅读:
    01 变量、基本数据类型
    02 gitlab的基本使用
    kubernetes
    02 redis高可用集群
    Redis & ELK
    01 Redis安装、配置详解、数据备份与恢复
    Jenkins
    01 git gitlab jenkins的安装
    golang mysql 客户端
    接口类
  • 原文地址:https://www.cnblogs.com/Owen-ET/p/6016464.html
Copyright © 2011-2022 走看看