在.Net4.0里用了iBatis.Net,出错了,开始还以为是我的配置问题,仔细检查后实在找不出问题.

 

 把项目改成3.5之后,正常运行...

 

 先记录下来,日后再做研究. 

  

 代码

 

posted @ 2010-07-21 22:59 Mr_Sheng 阅读(186) 评论(2) 编辑

 

CodeSmith生成实体类模板:

 

Model
<%-- 
Name    :     TearsEntity.cst
Author  :    Sheng
Description: Create Model Template
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" ResponseEncoding="UTF-8" Description="Build A Model" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="TableName" %>
<%@ Property Name="NameSpace" Type="String" Default="Model" Optional="False" Category="" Description="NameSpace" %>
<%@ Property Name="Author" Type="System.String" Default="Tears" Optional="False" Category="" Description="Author" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
//======================================================================
//
//        http://www.*****.com.cn 
//
//        Copyright (C) 2009-2010  ********
//        All Rights Reserved
//
//        FileName :    <%= SourceTable.Name %>
//        Description :  
//
//        Created By : Sheng   <%= DateTime.Now.ToString() %>
//        
//======================================================================
using System;
using System.Data;
using System.Configuration;

namespace <%=NameSpace%>
{
    
/// <summary>
    
/// <%= SourceTable.Name %> Model
    
/// </summary>
    [Serializable()] 
    
public class <%= SourceTable.Name %>
    {
        
        
#region 私有字段
        
<% for (int i = 0; i < SourceTable.Columns.Count; i++){ %>
        
private <%= SourceTable.Columns[i].SystemType %> <%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %>;
        
<% } %>
        
#endregion
        
        
#region 构造函数
        
/// <summary>
        
/// 无参构造函数
        
/// </summary>
        public <%= SourceTable.Name %> () { }

        
        
/// <summary>
        
/// 有参构造函数
        
/// </summary>
        <% for (int i = 1; i < SourceTable.Columns.Count; i++) { %>
        
/// <param name="<%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %>"><%= SourceTable.Columns[i].Description %></param>
        <% } %>
        
public <%= SourceTable.Name %> (<% for (int i = 1; i < SourceTable.Columns.Count-1; i++){ %><%= SourceTable.Columns[i].SystemType %> <%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %>,<% } %><% for (int i = SourceTable.Columns.Count-1; i < SourceTable.Columns.Count; i++) { %><%= SourceTable.Columns[i].SystemType %> <%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %> <% } %>)
        {
        
<% for (int i = 1; i < SourceTable.Columns.Count; i++) { %>
            
this.<%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %> = <%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %>;
        
<% } %>
        }
        
        
/// <summary>
        
/// 有参构造函数
        
/// </summary>
        <% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
        
/// <param name="<%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %>"><%= SourceTable.Columns[i].Description %></param>
        <% } %>
        
public <%= SourceTable.Name %> (<% for (int i = 0; i < SourceTable.Columns.Count-1; i++){ %><%= SourceTable.Columns[i].SystemType %> <%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %>,<% } %><% for (int i = SourceTable.Columns.Count-1; i < SourceTable.Columns.Count; i++) { %><%= SourceTable.Columns[i].SystemType %> <%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %> <% } %>)
            :
this(<% for (int i = 1; i < SourceTable.Columns.Count-1; i++){ %><%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %>,<% } %><% for (int i = SourceTable.Columns.Count-1; i < SourceTable.Columns.Count; i++) { %><%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %> <% } %>)
        {
            
this.<%= StringUtil.ToCamelCase(SourceTable.Columns[0].Name) %> = <%= StringUtil.ToCamelCase(SourceTable.Columns[0].Name) %>;
        }
        
#endregion
        
        
#region 公共属性
        
<% for (int i = 0; i < SourceTable.Columns.Count; i++) { %>
        
///<summary>
        
///<%= SourceTable.Columns[i].Description %>
        
///</summary>
        public <%= SourceTable.Columns[i].SystemType %> <%= StringUtil.ToPascalCase(SourceTable.Columns[i].Name) %>
        {
            
get
            {
                
return this.<%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %>;
            }
            
set
            {
                
this.<%= StringUtil.ToCamelCase(SourceTable.Columns[i].Name) %> = value;
            }
        }
        
<% } %>
        
#endregion
    }
}

 

 

posted @ 2010-01-11 12:23 Mr_Sheng 阅读(215) 评论(1) 编辑

 

有时候可能会有这么一个需求,我在后台使用:

HttpUtility.UrlEncode(str, System.Text.Encoding.UTF8);

 

将Url进行编码,由于JS和.Net的编码后某些字符会有一些不同,js提供的decodeURI就不能正确的解析了,

这时候就需要自己写解码方法了,

 

代码
<script type="text/javascript"> 
var Sheng ={
    
/**
     * Url编码
    *
*/
    encode : 
function(inputStr){           
        
var resultArr=[];   
        
var chars="!\"#$%&'()*+,/:;<=>?[]^`{|}~%";                
        for(var i=0;i<inputStr.length;i++){              
            var tmpChar = inputStr.charAt(i);              
            var c = inputStr.charCodeAt(i);                      
            if(c > 0x7E){                   
                resultArr[i]=encodeURI(tmpChar);             
            }else{                   
                if(tmpChar==" ")                      
                    resultArr[i]="+";                   
                else if(chars.indexOf(tmpChar)!=-1)                      
                    resultArr[i]="%"+c.toString(16);                   
                else                      
                    resultArr[i]=tmpChar;                  
            }              
        }          
        return resultArr.join("");      
    },

    /**
     * Url解码
    **/
    decode:function(inputStr){     
        var resultArr =[];       
        for(var i=0;i<inputStr.length;i++){     
            var chr = inputStr.charAt(i);  
            if(chr == "+"){                   
                resultArr[resultArr.length]=" ";              
            }else if(chr=="%"){                   
                var asc = inputStr.substring(i+1,i+3);   
                if(parseInt("0x"+asc)>0x7f){                       
                    resultArr[resultArr.length]= decodeURI(inputStr.substring(i,i+9));                   
                    i+=8;                   
                }else{                       
                    resultArr[resultArr.length]=String.fromCharCode(parseInt("0x"+asc));                       
                    i+=2;                   
                }              
            }else{                   
                resultArr[resultArr.length]= chr;              
            }          
        }          
        return resultArr.join("");     
    } 
}

 

 

posted @ 2010-01-09 15:08 Mr_Sheng 阅读(928) 评论(8) 编辑
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace Sheng.Common
{
    
/// <summary>
    
/// 加密帮助类
    
/// </summary>
    public class EncryptHelper
    {
        
/// <summary>
        
/// MD5加密
        
/// </summary>
        
/// <param name="str"></param>
        
/// <returns></returns>
        public static string MD5DecryptString(string str)
        {
            MD5CryptoServiceProvider md5 
= new MD5CryptoServiceProvider();
            
byte[] md5Source = System.Text.Encoding.UTF8.GetBytes(str);
            
byte[] md5Out = md5.ComputeHash(md5Source);
            
return Convert.ToBase64String(md5Out);
        }

        
/// <summary>
        
/// DES加密字符串
        
/// </summary>
        
/// <param name="sInputString">输入字符</param>
        
/// <param name="sKey">Key</param>
        
/// <returns>加密结果</returns>
        public string DESEncryptString(string sInputString, string sKey)
        {
            
try
            {
                
byte[] data = Encoding.Default.GetBytes(sInputString);
                
byte[] result;
                DESCryptoServiceProvider DES 
= new DESCryptoServiceProvider();
                DES.Key 
= ASCIIEncoding.ASCII.GetBytes(sKey);                       //密钥
                DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);                        //初始化向量
                ICryptoTransform desencrypt = DES.CreateEncryptor();                //加密器对象
                result = desencrypt.TransformFinalBlock(data, 0, data.Length);      //转换指定字节数组的指定区域
                return BitConverter.ToString(result);
            }
            
catch (Exception ex)
            {
                
//ex.Message = "DES加密异常";
                throw ex;
            }
        }

        
/// <summary>
        
/// DES解密字符串
        
/// </summary>
        
/// <param name="sInputString">输入字符</param>
        
/// <param name="sKey">Key</param>
        
/// <returns>解密结果</returns>
        public string DESDecryptString(string sInputString, string sKey)
        {
            
try
            {
                
//将字符串转换为字节数组
                string[] sInput = sInputString.Split("-".ToCharArray());
                
byte[] data = new byte[sInput.Length];
                
byte[] result;
                
for (int i = 0; i < sInput.Length; i++)
                {
                    data[i] 
= byte.Parse(sInput[i], System.Globalization.NumberStyles.HexNumber);
                }

                DESCryptoServiceProvider DES 
= new DESCryptoServiceProvider();
                DES.Key 
= ASCIIEncoding.ASCII.GetBytes(sKey);
                DES.IV 
= ASCIIEncoding.ASCII.GetBytes(sKey);
                ICryptoTransform desencrypt 
= DES.CreateDecryptor();
                result 
= desencrypt.TransformFinalBlock(data, 0, data.Length);
                
return Encoding.Default.GetString(result);
            }
            
catch (Exception ex)
            {
                
//ex.Message = "DES解密异常";
                throw ex;
            }
        }
    }
}

 

posted @ 2010-01-08 12:37 Mr_Sheng 阅读(122) 评论(0) 编辑

 

JSONHelper
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Script.Serialization;
using System.Data;

namespace Sheng.Common
{
    
/// <summary>
    
/// JSON帮助类
    
/// </summary>
    public class JSONHelper
    {
        
/// <summary>
        
/// 对象转JSON
        
/// </summary>
        
/// <param name="obj">对象</param>
        
/// <returns>JSON格式的字符串</returns>
        public static string ObjectToJSON(object obj)
        {
            JavaScriptSerializer jss 
= new JavaScriptSerializer();
            
try
            {
                
return jss.Serialize(obj);
            }
            
catch (Exception ex)
            {
                
                
throw new Exception("JSONHelper.ObjectToJSON(): " + ex.Message);
            }
        }

        
/// <summary>
        
/// 数据表转键值对集合
        
/// 把DataTable转成 List集合, 存每一行
        
/// 集合中放的是键值对字典,存每一列
        
/// </summary>
        
/// <param name="dt">数据表</param>
        
/// <returns>哈希表数组</returns>
        public static List<Dictionary<stringobject>> DataTableToList(DataTable dt)
        {
            List
<Dictionary<stringobject>> list
                 
= new List<Dictionary<stringobject>>();

            
foreach (DataRow dr in dt.Rows)
            {
                Dictionary
<stringobject> dic = new Dictionary<stringobject>();
                
foreach (DataColumn dc in dt.Columns)
                {
                    dic.Add(dc.ColumnName, dr[dc.ColumnName]);
                }
                list.Add(dic);
            }
            
return list;
        }

        
/// <summary>
        
/// 数据集转键值对数组字典
        
/// </summary>
        
/// <param name="dataSet">数据集</param>
        
/// <returns>键值对数组字典</returns>
        public static Dictionary<string, List<Dictionary<stringobject>>> DataSetToDic(DataSet ds)
        {
            Dictionary
<string, List<Dictionary<stringobject>>> result = new Dictionary<string, List<Dictionary<stringobject>>>();

            
foreach (DataTable dt in ds.Tables)
                result.Add(dt.TableName, DataTableToList(dt));

            
return result;
        }

        
/// <summary>
        
/// 数据表转JSON
        
/// </summary>
        
/// <param name="dataTable">数据表</param>
        
/// <returns>JSON字符串</returns>
        public static string DataTableToJSON(DataTable dt)
        {
            
return ObjectToJSON(DataTableToList(dt));
        }

        
/// <summary>
        
/// JSON文本转对象,泛型方法
        
/// </summary>
        
/// <typeparam name="T">类型</typeparam>
        
/// <param name="jsonText">JSON文本</param>
        
/// <returns>指定类型的对象</returns>
        public static T JSONToObject<T>(string jsonText)
        {
            JavaScriptSerializer jss 
= new JavaScriptSerializer();
            
try
            {
                
return jss.Deserialize<T>(jsonText);
            }
            
catch (Exception ex)
            {
                
throw new Exception("JSONHelper.JSONToObject(): " + ex.Message);
            }
        }

        
/// <summary>
        
/// 将JSON文本转换为数据表数据
        
/// </summary>
        
/// <param name="jsonText">JSON文本</param>
        
/// <returns>数据表字典</returns>
        public static Dictionary<string, List<Dictionary<stringobject>>> TablesDataFromJSON(string jsonText)
        {
            
return JSONToObject<Dictionary<string, List<Dictionary<stringobject>>>>(jsonText);
        }

        
/// <summary>
        
/// 将JSON文本转换成数据行
        
/// </summary>
        
/// <param name="jsonText">JSON文本</param>
        
/// <returns>数据行的字典</returns>
        public static Dictionary<stringobject> DataRowFromJSON(string jsonText)
        {
            
return JSONToObject<Dictionary<stringobject>>(jsonText);
        }
    }
}

 

 

System.Web.Script.Serialization命名空间是.Net 3.5新添加的.

如果要在3.5以下版本中使用,可以下载3.5中的System.Web.Extensions.dll 引入到自己的应用中.

 

 

posted @ 2010-01-07 12:42 Mr_Sheng 阅读(1440) 评论(4) 编辑