如何设置SQl存储过程调用参数为空

 在调用存储过程时候,经常遇到所传的参数为空,应该怎么设置了?

  private void GetData()
        {
            string ConnectionName="DefaultConnectionString";
          ConnectionStringSettings config=ConfigurationManager.ConnectionStrings[ConnectionName];
          DbProviderFactory factory = DbProviderFactories.GetFactory(config.ProviderName);
          DbConnection connection = factory.CreateConnection();
          connection.ConnectionString = config.ConnectionString;
          DbCommand cmd = factory.CreateCommand();
          cmd.Connection = connection;
          cmd.CommandText = "ckbx_ResponseTemplate_Get";
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.Add(CreateParameter("@ResponseTemplateID", DbType.Int32, null));
          cmd.Parameters.Add(CreateParameter("@AsscSurvID", DbType.Guid, null));
          cmd.Parameters.Add(CreateParameter("@EncryptKeyValue", DbType.String, ConfigurationManager.AppSettings["EncryptKey"].ToString()));
          cmd.Parameters.Add(CreateParameter("@EncryptSaltValue", DbType.String, ConfigurationManager.AppSettings["EncryptSalt"].ToString()));
          DbParameter parameter = cmd.CreateParameter();
          
          DbDataAdapter ap = factory.CreateDataAdapter();
          ap.SelectCommand = cmd;
          DataSet ds = new DataSet();
          ap.Fill(ds);
          dataGridView1.DataSource = ds.Tables[0];
 
        }
        private DbParameter CreateParameter(string name, DbType type, object value)
        {
            SqlParameter parameter = new  SqlParameter();
            parameter.DbType = type;
            parameter.ParameterName = name;
            parameter.Direction = ParameterDirection.Input;
            parameter.IsNullable = true;
            parameter.Value = (value == null) ? DBNull.Value : value;
            return parameter;
        }
      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值