A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 宋兴征 中级黑马   /  2013-4-10 14:34  /  1982 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 宋兴征 于 2013-4-10 14:44 编辑
  1. <div class="blockcode"><blockquote> private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
  2.         {
  3.             if (conn.State != ConnectionState.Open)
  4.                 conn.Open();
  5.             cmd.Connection = conn;
  6.             cmd.CommandText = cmdText;
  7.             if (trans != null)
  8.                 cmd.Transaction = trans;
  9.             cmd.CommandType = CommandType.Text;//cmdType;
  10.             if (cmdParms != null)
  11.             {
  12.                 foreach (SqlParameter parameter in cmdParms)
  13.                 {
  14.                     if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
  15.                         (parameter.Value == null))
  16.                     {
  17.                         parameter.Value = DBNull.Value;
  18.                     }
  19.                     cmd.Parameters.Add(parameter);
  20.                 }
  21.             }
  22.         }
复制代码
ParameterDirection是一个枚举类型,值有:// 摘要:        //     参数是输入参数。        Input = 1,        //        // 摘要:        //     参数是输出参数。        Output = 2,        //        // 摘要:        //     参数既能输入,也能输出。        InputOutput = 3,        //        // 摘要:        //     参数表示诸如存储过程、内置函数或用户定义函数之类的操作的返回值。        ReturnValue = 6,

请问,
  1. if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
  2. (parameter.Value == null))
  3. {
  4. parameter.Value = DBNull.Value;
  5. }
复制代码
该怎么理解?是什么意思?  为什么要设参数为DBNull.Value?


评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

3 个回复

倒序浏览
仔细看过,我是这样理解的:目的是将没有值的参数设置为Null
foreach (SqlParameter parameter in cmdParms)//循环所有的参数
{
    if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&(parameter.Value == null))//参数没有值并且为空
    {
        parameter.Value = DBNull.Value;//将它设置为数据库的空值Null
    }
    cmd.Parameters.Add(parameter);
}

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

回复 使用道具 举报
Direction  获取或设置一个值,该值指示参数是只可输入、只可输出、双向还是存储过程返回值参数。


    ParameterDirection是一个枚举类型:
     Input        参数是输入参数。
     InputOutput        参数既能输入,也能输出。
     Output        参数是输出参数。

个人认为:
  if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&(parameter.Value == null))
//1.参数一定有(类型要么是即可输入也可输出,要么是输入型)2.参数的值为Null(c#中的null对应数据库中的DBNull)     1和2同时满足。



参考资料出处:http://msdn.microsoft.com/zh-cn/library/vstudio/system.data.sqlclient.sqlparameter_properties.aspx
http://msdn.microsoft.com/zh-cn/library/system.data.parameterdirection(VS.80).aspx
希望对您有所帮助!

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

回复 使用道具 举报
郑丹丹1990 发表于 2013-4-10 16:38
Direction  获取或设置一个值,该值指示参数是只可输入、只可输出、双向还是存储过程返回值参数。

ParameterDirection.InputOutput,参数是输入或输出参数,该怎么理解?参数传进去,还能再传出来吗?传出来怎么用?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马