我的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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 ADO.NET 执行 SQL 语句,你可以按照以下步骤进行操作: 1. 创建一个连接对象(SqlConnection),并传入数据库连接字符串作为参数。连接串包含了访问数据库所需的信息,如数据库服务器名称、认证方式等。 2. 打开连接(Open)。 3. 创建一个命令对象(SqlCommand),并传入 SQL 语句和连接对象作为参数。 4. 可选步骤:设置命令对象的参数,如果 SQL 语句有占位符需要填充。 5. 执行命令(ExecuteNonQuery、ExecuteScalar 或 ExecuteReader),根据需要选择适当的方法来执行 SQL 语句。 - ExecuteNonQuery 用于执行没有返回结果集的 SQL 语句,如插入、更新、删除操作。 - ExecuteScalar 用于执行返回单个值的 SQL 语句,如获取记录总数。 - ExecuteReader 用于执行返回多行结果的 SQL 语句,如查询操作。 6. 关闭连接(Close)。 下面是一个简单的示例代码,演示了如何使用 ADO.NET 执行 SQL 语句: ```csharp using System; using System.Data.SqlClient; class Program { static void Main() { string connectionString = "Data Source=(local);Initial Catalog=YourDatabase;Integrated Security=True"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string sql = "SELECT * FROM YourTable"; using (SqlCommand command = new SqlCommand(sql, connection)) { using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { // 处理每一行的数据 // 例如:Console.WriteLine(reader["ColumnName"]); } } } } } } ``` 这是一个基本示例,你可以根据实际需求进行扩展和调整。记得在使用完连接、命令和读取器后,要进行适当的释放和关闭,以确保资源得到正确释放。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值