posts - 80,  comments - 17,  trackbacks - 6
  2012年2月3日

最近一段时间在做跟某公司数据对接的工作,我们双方的技术人员在观察对接数据的时候,发现部分数据无法通过MD5加密验证。

原始代码如下

1 [HttpPost]
2 public ActionResult Index(string lcData)
3 {
4     string lcDataDecode = HttpUtility.UrlDecode(lcData, Encoding.UTF8);
5     string response = DoSometing(lcDataDecode);
6 
7     return RedirectToAction("Index");
8 }

通过双方数据对比,发现上述代码中lcData的值是已经通过Decode之后的值,并不是原始的数据。由于这次Decode,导致数据中的加号“+”被过滤掉。

请参考一下dudu的文章HttpUtility.UrlEncode、HttpUtility.UrlDecode、Server.UrlEncode、Server.UrlDecode的区分与应用

在网上查找了一些解决办法,很多人提到用 Request.QueryString["key"].Replace(" ", "+"); 来处理。但是我们的数据无法这样处理,因为传递的数据中本身就有空格“ ”的存在。

因此,我们寻找到了下面的方法

 1 [HttpPost]
 2 public ActionResult Index(string lcData)
 3 {
 4     Regex r = new Regex(@"lcData=(?<value>[^&=]+)"); //针对我们的需求,如果需要解析所有的可以用(?<name>[^&=]+)=(?<value>[^&=]+)
 5     Match m = r.Match(Request.Params.ToString());
 6 
 7     lcData = m.Groups[1].Value;
 8     string lcDataDecode = HttpUtility.UrlDecode(lcData, Encoding.UTF8);
 9 
10     string response = DoSometing(lcDataDecode);
11 
12     return RedirectToAction("Index");
13 }

希望能帮助到其他有需要的朋友。

 

posted @ 2012-02-03 10:04 尚書 阅读(4) 评论(0) 编辑
  2012年1月6日
declare @count int
set @count = 0
while (@count < 10000)
begin
print RIGHT('0000' + CAST(@count AS VARCHAR), 4)
set @count = @count +1
end
posted @ 2012-01-06 13:51 尚書 阅读(2) 评论(0) 编辑
  2011年12月2日
USE [master]
EXEC sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'AllowInProcess' , 1
EXEC sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'DynamicParameters' , 1
EXEC sp_configure 'show advanced options', 1
RECONFIGURE 
EXEC sp_configure 'Ad Hoc Distributed Queries', 1 
RECONFIGURE  
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 8.0;IMEX=1;HDR=YES;Database=文件路径', 'SELECT * FROM [Sheet1$]')
EXEC sp_configure 'Ad Hoc Distributed Queries', 0 
RECONFIGURE  
EXEC sp_configure 'show advanced options', 0
RECONFIGURE 
EXEC sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'AllowInProcess' , 0
EXEC sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'DynamicParameters' , 0
posted @ 2011-12-02 12:03 尚書 阅读(15) 评论(0) 编辑
  2011年11月26日


用openrowset连接远程SQL或插入数据

--如果只是临时访问,可以直接用openrowset

--查询示例

SELECT * FROM OPENROWSET('SQLOLEDB','Data Source=ServerName;User ID=MyUID;Password=MyPass',数据库名.dbo.表名)

--导入示例

SELECT * INTO 表 FROM OPENROWSET('SQLOLEDB','Data Source=ServerName;User ID=MyUID;Password=MyPass',数据库名.dbo.表名)

--创建链接服务器

EXEC SP_ADDLINKEDSERVER 'srv_lnk','','SQLOLEDB','远程服务器名或ip地址'
EXEC SP_ADDLINKEDSRVLOGIN 'srv_lnk','false',null,'用户名','密码'
GO

--查询示例

SELECT * FROM srv_lnk.数据库名.dbo.表名

--导入示例

SELECT * INTO 表 FROM srv_lnk.数据库名.dbo.表名

--以后不再使用时删除链接服务器

EXEC SP_DROPSERVER 'srv_lnk','droplogins'
GO

--下面的示例访问来自某个表的数据,该表在 SQL Server 的另一个实例中。

SELECT * FROM OPENDATASOURCE('SQLOLEDB','Data Source=ServerName;User ID=MyUID;Password=MyPass').Northwind.dbo.Categories

下面是个查询的示例,它通过用于 Jet 的 OLE DB 提供程序查询 Excel 电子表格。

SELECT * FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0','Data Source="c:Financeaccount.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions

如果只是偶尔使用 就用opendatasource/openrowset, 固定的频繁使用建linked server

posted @ 2011-11-26 12:11 尚書 阅读(40) 评论(0) 编辑
  2011年11月24日

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using CSharpJExcel.Jxl;
using CSharpJExcel.Jxl.Write;
using CsvHelper.Configuration;

namespace Common.Extensions
{
    public static class ExcelExtension
    {
        public static IEnumerable<T> GetRecordsFromExcel<T>(this string path) where T : class, new()
        {
            var file = new FileInfo(path);

            Debug.Assert(!string.IsNullOrWhiteSpace(file.DirectoryName));

            Type type = typeof(T);
            var properties = type.GetProperties().Where(t => t.IsDefined(typeof(CsvFieldAttribute), true)).ToList();
            var headerNames = properties.Select(GetCsvFieldName).ToList();

            var workbook = Workbook.getWorkbook(file);
            Sheet sheet = workbook.getSheet(0);

            var headerCells = sheet.getRow(0);

            var query = from headerName in headerNames
                        join headerCell in headerCells
                            on headerName equals headerCell.getContents()
                        select new
                                   {
                                       Key = headerName,
                                       Value = headerCell.getColumn()
                                   };

            var nameIndexes = query.ToDictionary(t => t.Key, t => t.Value);
            var result = new List<T>();
            for (int row = 1; row < sheet.getRows(); row++)
            {
                var t = new T();

                foreach (var property in properties)
                {
                    string csvFiledName = property.GetCsvFieldName();
                    if (nameIndexes.ContainsKey(csvFiledName))
                    {
                        var column = nameIndexes[csvFiledName];
                        var value = sheet.getCell(column, row).getContents();
                        property.SetValue(t, value, null);
                    }
                }
                result.Add(t);
            }

            workbook.close();
            return result;
        }

        private static string GetCsvFieldName(this PropertyInfo property)
        {
            return ((CsvFieldAttribute)property.GetCustomAttributes(typeof(CsvFieldAttribute), true)[0]).Name;
        }

        public static void WriteRecordsToExcel<T>(this List<T> records, string path) where T : class
        {
            var file = new FileInfo(path);

            Debug.Assert(!string.IsNullOrWhiteSpace(file.DirectoryName));

            if (!Directory.Exists(file.DirectoryName))
                Directory.CreateDirectory(file.DirectoryName);

            var ws = new WorkbookSettings();
            ws.setEncoding("UTF8");
            WritableWorkbook workbook = Workbook.createWorkbook(file, ws);
            WritableSheet sheet = workbook.createSheet(file.Name, 0);

            Type type = typeof(T);

            var properties = type.GetProperties().Where(t => t.IsDefined(typeof(CsvFieldAttribute), true)).ToList();

            for (int j = 0; j < properties.Count(); j++)
            {
                var attribute = (CsvFieldAttribute)properties[j].GetCustomAttributes(typeof(CsvFieldAttribute), true)[0];
                var cell = new Label(j, 0, attribute.Name);
                sheet.addCell(cell);
            }

            for (int i = 0; i < records.Count(); i++)
            {
                for (int j = 0; j < properties.Count(); j++)
                {
                    object value = properties[j].GetValue(records[i], null);
                    string s = value == null ? "" : value.ToString();
                    var cell = new Label(j, i + 1, s);
                    sheet.addCell(cell);
                }
            }

            workbook.write();
            workbook.close();
        }
    }
}

posted @ 2011-11-24 14:12 尚書 阅读(14) 评论(0) 编辑
  2011年11月21日
摘要: 1、官方文档的命令是mongod--bind_ip0.0.0.0--logpathd:\mongo\logs--logappend--dbpathd:\mongo\data--directoryperdb--install2、另外不要忘了还要在Windows 2008 R2的防火墙中打开27017和28017端口,在入站规则里加一条就行了。阅读全文
posted @ 2011-11-21 22:37 尚書 阅读(3) 评论(0) 编辑
  2011年8月16日
摘要: Configure PerforceDownload the Perforce daemon file 'p4d' and client 'p4' files directly into /usr/local/bin. These files are for Linux installs using the 2.6 kernel version.cd /usr/local/binwget http://www.perforce.com/downloads/perforce/r10.2/bin.linux26x86/p4dwget http://www.perfo阅读全文
posted @ 2011-08-16 17:39 尚書 阅读(17) 评论(0) 编辑
  2011年7月22日
摘要: Google+FacebookYoutubeTwitter/Files/Magicworks/HOSTS.txt用文本编辑器打开 %SystemRoot%\system32\drivers\etc\hosts 文件,增加以下字段~更改Hosts之后 在cmd下输入ipconfig /displaydns 或ipconfig /flushdns来清理DNS最好用 https:// 来访问阅读全文
posted @ 2011-07-22 10:41 尚書 阅读(254) 评论(2) 编辑
摘要: 用文本编辑器打开 %SystemRoot%\system32\drivers\etc\hosts 文件,增加以下字段~阅读全文
posted @ 2011-07-22 10:15 尚書 阅读(86) 评论(0) 编辑
摘要: 用文本编辑器打开 %SystemRoot%\system32\drivers\etc\hosts 文件,增加以下字段~阅读全文
posted @ 2011-07-22 10:11 尚書 阅读(49) 评论(0) 编辑