public static int ExecuteCommand(string sql, params SqlParameter[] values)
        {
            SqlCommand cmd = new SqlCommand(sql, Connection);
            SqlTransaction trans = connection.BeginTransaction();
            try
            {
                cmd.Transaction = trans;
                cmd.Parameters.AddRange(values);
                int result = cmd.ExecuteNonQuery();
                trans.Commit();
                return result;
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                trans.Rollback();
                throw new Exception(e.Message);
            }        }
public static int InsertCategory(Category cate)
       {
           string sql = "insert into category values(@categoryName,@categoryDesc,@categoryImg,@CNcategoryName,@CNcategoryDesc,@DepartId)";
           SqlParameter[] para = new SqlParameter[] 
           {
                new SqlParameter("@categoryName",cate.CategoryName),
                new SqlParameter("@categoryDesc",cate.CategoryDescription),
                new SqlParameter("@categoryImg",cate.CategoryImage),
                new SqlParameter("@CNcategoryName",cate.CNcategoryName),
                new SqlParameter("@CNcategoryDesc",cate.CNcategoryDescription),
                new SqlParameter("@DepartId",cate.DepartId.DepartId)
           };
           int count = DBHelpers.ExecuteCommand(sql,para);
           return count;
       }
我有 @CNcategoryName指定值阿。给我一直报这个,而且 @CNcategoryName是可以为null的。

解决方案 »

  1.   

     new SqlParameter("@DepartId",cate.DepartId.DepartId)
    这个是不是写错了
      

  2.   

    可以为 "" ,但应该不能为 null你换 "" 再试试!
      

  3.   

    cate.CategoryName看下它是不是null勒
      

  4.   

    string sql = "insert into category(数据库中对应的6个字段)values(@categoryName,@categoryDesc,@categoryImg,@CNcategoryName,@CNcategoryDesc,@DepartId)";
      试试这样行不
      

  5.   

    页面后台调用方法时,给@CNcategoryName参数赋值了吗??
      

  6.   

    断点一下,看你的实体类的CNcategoryName是否为null
    再者看看你的存储过程有没有问题
      

  7.   

    你这个category表里面只有这6个字段么?
    插入语句最好这样写
    insert into category (categoryName,categoryDesc,categoryImg,CNcategoryName,CNcategoryDesc,DepartId) values(@categoryName,@categoryDesc,@categoryImg,@CNcategoryName,@CNcategoryDesc,@DepartId)
    要插入那些字段就写上那些字段,不要直接写个表名就完了,那样是插入表中所有字段,容易出错.