C#中往数据库插入空值报错解决方法

C#中的NUll于SQL中的null是不一样的, SQL中的null用C#表示出来就是DBNull.Value

 

class SqlHerper
    {
        private static readonly string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;

        //封装执行增删改 只有执行insert updata delete 语句的时候返回受影响的行数,select 永远返回的是-1
        public static int ExecuteNonQuery(string sql, CommandType cmdType, params SqlParameter[] parameters)
        {
            using(SqlConnection conn =new SqlConnection(connStr))
            {
                using (SqlCommand com = new SqlCommand(sql, conn))
                {
                    com.CommandType = cmdType;
                    if (parameters != null)
                    {
                        foreach (SqlParameter spr in parameters)
                        {
                            if (spr.SqlValue == null)
                            {
                                spr.SqlValue = DBNull.Value;  //C#中的NUll于SQL中的null是不一样的, SQL中的null用C#表示出来就是DBNull.Value
                            }
                            com.Parameters.Add(spr);
                        }
                        //com.Parameters.AddRange(parameters);
                    }
                    conn.Open();
                    return com.ExecuteNonQuery();
                }
            }
        }
        //封装返回单个值的方法 只返回第一行第一列
        public static object ExecuteScalar(string sql, CommandType cmdType, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand com = new SqlCommand(sql, conn))
                {
                    com.CommandType = cmdType;
                    if (parameters != null)
                    {
                        com.Parameters.AddRange(parameters);
                    }
                    conn.Open();
                    return com.ExecuteScalar();
                }
            }
        }
        //返回SqlDataReader对象的方法
        public static SqlDataReader ExecuteReader(string sql, CommandType cmdType, params SqlParameter[] parameters)
        {
            SqlConnection conn = new SqlConnection(connStr); //Connection 不能using 因为返回SqlDataReader对象时要保证conn连接时打开状态的。
            using (SqlCommand com = new SqlCommand(sql, conn))
            {
                com.CommandType = cmdType;
                if (parameters != null)
                {
                    com.Parameters.AddRange(parameters);
                }
                try
                {
                    conn.Open();
                    //CommandBehavior.CloseConnection 当关闭DataReader对象时,也自动关闭关联的Connection对象。
                    return com.ExecuteReader(CommandBehavior.CloseConnection);
                }
                catch (Exception)
                {
                    conn.Dispose();
                    throw;
                }
            }
        }
        //返回DataTable对象的方法
        public static DataTable ExecuteDataTable(string sql, CommandType cmdType, params SqlParameter[] parameters)
        {
            DataTable dt = new DataTable();
            using (SqlDataAdapter adapter = new SqlDataAdapter(sql, connStr))
            {
                adapter.SelectCommand.CommandType = cmdType;
                if (parameters != null)
                {
                    adapter.SelectCommand.Parameters.AddRange(parameters);
                }
                adapter.Fill(dt);
            }
            return dt;
        }
    }

C#中往数据库插入空值报错解决方法

上一篇:SQL_为表和列加注释


下一篇:sql经典面试题