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

时间:2022-12-13 23:06:32
/// <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,),
new SqlParameter("@DocumentPath", SqlDbType.VarChar,),
new SqlParameter("@DocumentFormatType", SqlDbType.VarChar,),
new SqlParameter("@UploadBy", SqlDbType.VarChar,),
new SqlParameter("@UploadDate", SqlDbType.DateTime),
new SqlParameter("@UploaderOpenId", SqlDbType.VarChar,),
new SqlParameter("@BillNo", SqlDbType.VarChar,),
new SqlParameter("@BillAmount", SqlDbType.Float,),
new SqlParameter("@Reviewedby", SqlDbType.VarChar,),
new SqlParameter("@ReviewedDate", SqlDbType.DateTime),
new SqlParameter("@ReviewedResult", SqlDbType.Bit,),
new SqlParameter("@ReviewedComment", SqlDbType.VarChar,)};
parameters[].Value = model.DocumentName;
parameters[].Value = model.DocumentPath;
parameters[].Value = model.DocumentFormatType;
parameters[].Value = model.UploadBy??"";
parameters[].Value = model.UploadDate??Convert.ToDateTime("1970/01/01");
parameters[].Value = model.UploaderOpenId??"";
parameters[].Value = model.BillNo??"";
parameters[].Value = model.BillAmount??;
parameters[].Value = model.Reviewedby??"";
parameters[].Value = model.ReviewedDate ?? Convert.ToDateTime("1970/01/01");
parameters[].Value = model.ReviewedResult;
parameters[].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>