为变量赋空值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,exception,string,null,cmd)