码迷,mamicode.com
首页 > 移动开发 > 详细

Dapper.net Insert mssql unicode 乱码问题

时间:2017-04-25 19:11:41      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:nal   this   value   images   style   mss   源码   src   dapper   

1、效果:

技术分享

2、处理方法:

 /// <summary>
        /// insert single sql
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public static int Insert<T>(string sql, dynamic param = null, string connectionString = null) where T : class, new()
        {
            using (IDbConnection conn = OpenConnection(connectionString))
            {
                return conn.Execute(sql, (object)param,null,null,null);
            }
        }
  public static readonly string InsertSql = @"Insert into SpreadApiErrorMsgs(Id,ControllerName,ActionName,Message,SoluWay,CreateDate)
                                                    values(@Id,@ControllerName,@ActionName,@Message,@SoluWay,@CreateDate)";
        /// <summary>
        /// insert single sql
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public static int Insert<T>(string sql, dynamic param = null, string connectionString = null) where T : class, new()
        {
            using (IDbConnection conn = OpenConnection(connectionString))
            {
                return conn.Execute(sql, (object)param,null,null,null);
            }
        }

 

PS:Dapper源码:

 /// <summary>
    /// This class represents a SQL string, it can be used if you need to denote your parameter is a Char vs VarChar vs nVarChar vs nChar
    /// </summary>
    sealed partial class DbString : Reasonable.Spread.Dapper.SqlMapper.ICustomQueryParameter
    {
        /// <summary>
        /// Create a new DbString
        /// </summary>
        public DbString() { Length = -1; }
        /// <summary>
        /// Ansi vs Unicode 
        /// </summary>
        public bool IsAnsi { get; set; }
        /// <summary>
        /// Fixed length 
        /// </summary>
        public bool IsFixedLength { get; set; }
        /// <summary>
        /// Length of the string -1 for max
        /// </summary>
        public int Length { get; set; }
        /// <summary>
        /// The value of the string
        /// </summary>
        public string Value { get; set; }
        /// <summary>
        /// Add the parameter to the command... internal use only
        /// </summary>
        /// <param name="command"></param>
        /// <param name="name"></param>
        public void AddParameter(IDbCommand command, string name)
        {
            if (IsFixedLength && Length == -1)
            {
                throw new InvalidOperationException("If specifying IsFixedLength,  a Length must also be specified");
            }
            var param = command.CreateParameter();
            param.ParameterName = name;
            param.Value = (object)Value ?? DBNull.Value;
            if (Length == -1 && Value != null && Value.Length <= 4000)
            {
                param.Size = 4000;
            }
            else
            {
                param.Size = Length;
            }
            param.DbType = IsAnsi ? (IsFixedLength ? DbType.AnsiStringFixedLength : DbType.AnsiString) : (IsFixedLength ? DbType.StringFixedLength : DbType.String);
            command.Parameters.Add(param);
        }
    }

 

Dapper.net Insert mssql unicode 乱码问题

标签:nal   this   value   images   style   mss   源码   src   dapper   

原文地址:http://www.cnblogs.com/zoro-zero/p/6763716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!