在调用存储过程时候,经常遇到所传的参数为空,应该怎么设置了?
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;
}