我的VS2013中,用Ado.net给SQLParameter赋值的时候,当赋值null的时候,生成的sql语句是default

/// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.WechatDocuments model)
        {
            StringBuilder strSql=new StringBuilder();
            strSql.Append("insert into WechatDocuments(");
            strSql.Append("DocumentName,DocumentPath,DocumentFormatType,UploadBy,UploadDate,UploaderOpenId,BillNo,BillAmount,Reviewedby,ReviewedDate,ReviewedResult,ReviewedComment)");
            strSql.Append(" values (");
            strSql.Append("@DocumentName,@DocumentPath,@DocumentFormatType,@UploadBy,@UploadDate,@UploaderOpenId,@BillNo,@BillAmount,@Reviewedby,@ReviewedDate,@ReviewedResult,@ReviewedComment)");
            SqlParameter[] parameters = {
                    new SqlParameter("@DocumentName", SqlDbType.VarChar,500),
                    new SqlParameter("@DocumentPath", SqlDbType.VarChar,500),
                    new SqlParameter("@DocumentFormatType", SqlDbType.VarChar,20),
                    new SqlParameter("@UploadBy", SqlDbType.VarChar,100),
                    new SqlParameter("@UploadDate", SqlDbType.DateTime),
                    new SqlParameter("@UploaderOpenId", SqlDbType.VarChar,100),
                    new SqlParameter("@BillNo", SqlDbType.VarChar,100),
                    new SqlParameter("@BillAmount", SqlDbType.Float,8),
                    new SqlParameter("@Reviewedby", SqlDbType.VarChar,100),
                    new SqlParameter("@ReviewedDate", SqlDbType.DateTime),
                    new SqlParameter("@ReviewedResult", SqlDbType.Bit,1),
                    new SqlParameter("@ReviewedComment", SqlDbType.VarChar,500)};
            parameters[0].Value = model.DocumentName;
            parameters[1].Value = model.DocumentPath;
            parameters[2].Value = model.DocumentFormatType;
            parameters[3].Value = model.UploadBy??"";
            parameters[4].Value = model.UploadDate??Convert.ToDateTime("1970/01/01");
            parameters[5].Value = model.UploaderOpenId??"";
            parameters[6].Value = model.BillNo??"";
            parameters[7].Value = model.BillAmount??0;
            parameters[8].Value = model.Reviewedby??"";
            parameters[9].Value = model.ReviewedDate ?? Convert.ToDateTime("1970/01/01");
            parameters[10].Value = model.ReviewedResult;
            parameters[11].Value = model.ReviewedComment??"";

            return SQLServerHelper.ExcuteNonQuery(strSql.ToString(), CommandType.Text, parameters);
        }

exec sp_executesql N'insert into WechatDocuments(DocumentName,DocumentPath,DocumentFormatType,UploadBy,UploadDate,UploaderOpenId,BillNo,BillAmount,Reviewedby,ReviewedDate,ReviewedResult,ReviewedComment) values (@DocumentName,@DocumentPath,@DocumentFormatType,@UploadBy,@UploadDate,@UploaderOpenId,@BillNo,@BillAmount,@Reviewedby,@ReviewedDate,@ReviewedResult,@ReviewedComment)',N'@DocumentName varchar(500),@DocumentPath varchar(500),@DocumentFormatType varchar(20),@UploadBy varchar(100),@UploadDate datetime,@UploaderOpenId varchar(100),@BillNo varchar(100),@BillAmount float,@Reviewedby varchar(100),@ReviewedDate datetime,@ReviewedResult bit,@ReviewedComment varchar(500)',@DocumentName='20151216072318_2511.jpg',@DocumentPath='/UploadFiles/Bill/20151216072318_2511.jpg',@DocumentFormatType=default,@UploadBy='',@UploadDate='2015-12-16 07:23:29.107',@UploaderOpenId='',@BillNo='',@BillAmount=0,@Reviewedby='',@ReviewedDate='1970-01-01 00:00:00',@ReviewedResult=0,@ReviewedComment=''

执行报错:

Msg 8178, Level 16, State 1, Line 0
The parameterized query '(@DocumentName varchar(500),@DocumentPath varchar(500),@Document' expects the parameter '@DocumentFormatType', which was not supplied.

把@DocumentFormatType=default改为@DocumentFormatType=null,就成功执行。在新添加一条数据的时候,某些字段是需要它为null的,不知道是我VS出问题了还是?

----------------------------------------------------------------------------------------------------

Error 19 The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>

错误19字符型必须是非空值类型来使用它作为参数T的泛型类型或方法的系统。空<T> 

原文地址:https://www.cnblogs.com/sen068/p/5050036.html