为变量赋空值DBNull.value

private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            if (trans != null)
                cmd.Transaction = trans;
            cmd.CommandType = CommandType.Text;//cmdType;
            if (cmdParms != null)
            {
                foreach (SqlParameter parm in cmdParms)
                {
                    if (parm.SqlDbType == SqlDbType.DateTime && parm.Value != DBNull.Value)
                    {
                        if ((DateTime)parm.Value == DateTime.MinValue)
                            parm.Value = System.DBNull.Value;
                    }
                    if (parm.SqlDbType == SqlDbType.Decimal && parm.Value != DBNull.Value)
                    {
                        try
                        {
                            decimal dd = (decimal)parm.Value;
                            if ((decimal)parm.Value == decimal.MinValue)
                                parm.Value = System.DBNull.Value;
                        }
                        catch (Exception ex)
                        {
                            string ss = "";
                            //parm.Value = System.DBNull.Value;
                        }
                    }
                    cmd.Parameters.Add(parm);
                }
            }
        }

阅读更多
个人分类: NET
上一篇eclipse/myeclipse注释模板的修改
下一篇ASP.NET获取远程网页下载到本地文件
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭