以前某面试题要求给string扩展一个IsNotNullOrEmpty的方法。当时没有想到合适的办法。后来发现3.0以后加了个扩展方法的语法糖。现收集了几位园友的某些实际应用,自己也试试写了几个方法。大家用的着的,尽管Copy啊。 如果你觉得代码很熟悉,是你写的我在此谢过了。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinComman;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
//简写 foreach测试
string[] temp = new string[] {"White","Red","Green"};
temp.Each(str => Console.WriteLine(str));
IList<Employee> empList = new List<Employee> {
new Employee{EmpNo="001",Name="小李"},
new Employee{EmpNo="002",Name="小赵"},
new Employee{EmpNo="003",Name="小王"}
};
//获取某个List的每个Item的某个属性,返回为一个数组
empList.ToStringArray("Name").Each(str => Console.WriteLine(str));
Console.WriteLine(DateTime.Now.TruncateTime().ToString());
decimal amount = 9810298.36M;
Console.WriteLine(amount.ToRMB());
}
}
public class Employee
{
public string EmpNo
{
get;
set;
}
public string Name
{
get;
set;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WinComman
{
///<summary>
/// C#扩展类
///</summary>
public static class GhoughShawExtClass
{
///<summary>
/// 按指定属性返回T的指定属性ToString()后的数组
///</summary>
///<typeparam name="T"></typeparam>
///<param name="list">集合</param>
///<param name="propertyName">属性,需要完全匹配大小写</param>
///<returns></returns>
public static string[] ToStringArray<T>(this IEnumerable<T> list, string propertyName)
{
string[] result = new string[list.Count()];
int i = 0;
foreach (var item in list)
{
Type t = item.GetType();
var piList = t.GetProperties();
var pi = piList.Where(p => p.Name.Equals(propertyName)).FirstOrDefault();
if (pi != null)
{
result[i] = pi.GetValue(item, null).ToString();
i++;
}
else
{
break;
throw new Exception(string.Format("对象'{0}',不存在'{1}'属性!", t.Name, propertyName));
};
}
return result;
}
///<summary>
/// 日期时间型取整,舍去时间
///</summary>
///<param name="value"></param>
///<returns></returns>
public static DateTime TruncateTime(this DateTime value)
{
return DateTime.Parse(value.ToLongDateString() + " 00:00:00");
}
///<summary>
/// 货币转人民币大写
///</summary>
///<param name="amountValue"></param>
///<returns></returns>
public static string ToRMB(this decimal amountValue)
{
var money = new Money(amountValue);
return money.ToString();
}
///<summary>
/// 货币转人民币大写
///</summary>
///<param name="amountValue"></param>
///<returns></returns>
public static string ToRMB(this double amountValue)
{
var money = new Money(Convert.ToDecimal(amountValue));
return money.ToString();
}
#region 简化流程型语句 如Foreach For 非本人代码,取自博客园某牛人(忘记了,不好意思),在此谢过
///<summary>
/// 简化Foreach的写法
///</summary>
///<typeparam name="TSource"></typeparam>
///<param name="obj"></param>
public delegate void EachMethod<TSource>(TSource obj);
public static void Each<TSource>(this IEnumerable<TSource> source, EachMethod<TSource> method)
{
foreach (TSource obj in source)
{
method(obj);
}
}
///<summary>
/// 简化在循环内部,Item满足某个条件时,执行某个方法的写法
///</summary>
///<typeparam name="TSource"></typeparam>
///<param name="source"></param>
///<param name="selector"></param>
///<param name="method"></param>
public static void If<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> selector, EachMethod<TSource> method)
{
foreach (TSource obj in source)
{
if (selector(obj))
method(obj);
}
}
///<summary>
/// 简化For的写法
///</summary>
///<param name="max"></param>
///<param name="method"></param>
public static void For(this int max, Func<int, bool> method)
{
for (int i = 0; i < max; i++)
{
if (!method(i))
{
break;
}
}
}
///<summary>
/// 简化For的写法 Down To
///</summary>
///<param name="max"></param>
///<param name="method"></param>
public static void DownFor(this int max, int min, Func<int, bool> method)
{
for (int i = max; i > min; i--)
{
if (!method(i))
{
break;
}
}
}
#endregion
}
///<summary>
/// dudu 写的Wcf Client 扩展类,这个办法我试用过了。果然是享受
///</summary>
public class WcfClient
{
public static TReturn UseService<TChannel, TReturn>(Func<TChannel, TReturn> func)
{
var chanFactory = new ChannelFactory<TChannel>("*");
TChannel channel = chanFactory.CreateChannel();
TReturn result = func(channel);
try
{
((IClientChannel)channel).Close();
}
catch
{
((IClientChannel)channel).Abort();
}
return result;
}
}
///<summary>
/// 伍华聪的WCF client 扩展
///</summary>
public static class WcfExtensions
{
public static void Using<T>(this T client, Action<T> work) where T : ICommunicationObject
{
try
{
work(client);
client.Close();
}
catch (CommunicationException ex)
{
client.Abort();
}
catch (TimeoutException ex)
{
client.Abort();
}
catch (Exception ex)
{
client.Abort();
}
}
}
public class Money
{
public string Yuan = "元"; // “元”,可以改为“圆”、“卢布”之类
public string Jiao = "角"; // “角”,可以改为“拾”
public string Fen = "分"; // “分”,可以改为“美分”之类
static string Digit = "零壹贰叁肆伍陆柒捌玖"; // 大写数字
bool isAllZero = true; // 片段内是否全零
bool isPreZero = true; // 低一位数字是否是零
bool Overflow = false; // 溢出标志
long money100; // 金额*100,即以“分”为单位的金额
long value; // money100的绝对值
StringBuilder sb = new StringBuilder(); // 大写金额字符串,逆序
// 只读属性: "零元"
public string ZeroString
{
get { return Digit[0] + Yuan; }
}
// 构造函数
public Money(decimal money)
{
try { money100 = (long)(money * 100m); }
catch { Overflow = true; }
if (money100 == long.MinValue) Overflow = true;
}
// 重载 ToString() 方法,返回大写金额字符串
public override string ToString()
{
if (Overflow) return "金额超出范围";
if (money100 == 0) return ZeroString;
string[] Unit = { Yuan, "万", "亿", "万", "亿亿" };
value = System.Math.Abs(money100);
ParseSection(true);
for (int i = 0; i < Unit.Length && value > 0; i++)
{
if (isPreZero && !isAllZero) sb.Append(Digit[0]);
if (i == 4 && sb.ToString().EndsWith(Unit[2]))
sb.Remove(sb.Length - Unit[2].Length, Unit[2].Length);
sb.Append(Unit[i]);
ParseSection(false);
if ((i % 2) == 1 && isAllZero)
sb.Remove(sb.Length - Unit[i].Length, Unit[i].Length);
}
if (money100 < 0) sb.Append("负");
return Reverse();
}
// 解析“片段”: “角分(2位)”或“万以内的一段(4位)”
void ParseSection(bool isJiaoFen)
{
string[] Unit = isJiaoFen ?
new string[] { Fen, Jiao } :
new string[] { "", "拾", "佰", "仟" };
isAllZero = true;
for (int i = 0; i < Unit.Length && value > 0; i++)
{
int d = (int)(value % 10);
if (d != 0)
{
if (isPreZero && !isAllZero) sb.Append(Digit[0]);
sb.AppendFormat("{0}{1}", Unit[i], Digit[d]);
isAllZero = false;
}
isPreZero = (d == 0);
value /= 10;
}
}
// 反转字符串
string Reverse()
{
StringBuilder sbReversed = new StringBuilder();
for (int i = sb.Length - 1; i >= 0; i--)
sbReversed.Append(sb[i]);
return sbReversed.ToString();
}
}
}
前面技术架构和系统选型说到了使用oracle,看到园友们一些评论。我也不想卖弄,不想争论什么数据库好。喜欢就是理由,用了多年了为何不可。
我可以想象,大家没有使用oracle的原因可能有以下几点:
总之在我眼里,她俨然是位气质高雅而又风情万种的少妇,可远观也可近赏。Oracle软件本身是免费的,所以任何人都可以从Oracle官方网站下载并安装Oracle的数据库软件,收费的是License,即软件授权,如果数据库用于商业用途,就需要购买相应Oracle产品的License。美人等着你去亲近呢,在自己的虚拟机里操练下吧,不违法的。想必大家都知道去哪里下载吧!http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html?ssSourceSiteId=ocomen
开始吧...

截的图大小不一,就请将就着看吧!测试操作系统和硬件环境是否符合,我使用的是win2008企业版。下面的都是step by step看图就ok了,不再详细解释。



















请留意下面的总的设置步骤:-----------------------------------------------------------!


表空间其实应该理解为存储空间和可以设置为跨分区或者硬盘来存储呢。分永久和临时的两种。一般创建自己的永久的表空间就可以了,临时的使用系统自己建立的。



oracle是跨平台的数据库,其实图形界面或者网页界面最终还是使用DML语言来管理表空间、数据库(用户)、建立表、丢弃表等。其实命名行更快。这样也可以显出你是数据库高手。具体使用请找百度、谷歌吧。

表空间建好了,再建立用户。建立好了用户其实同名的数据库也建好了!建立用户时必须指定数据库存储的表空间名称和临时表空间名称。



临时表空间一般选“TEMP”那个。这是系统自动创建的。

角色的选择很重要,一般“connect”和"dba"都要,否则会被限制得很死,干不了啥。关于安全的知识,自己去其他地方了解。一下说不完,又不成体系。关键也说不好。
也可以使用sqlplus,用"system"登陆,然后使用: grant dba to "用户名";和grant connect to "用户名";在命令行搞定授权。


数据库建好了。好像11g还没有像mssql的企业管理器类似的管理工具。我们一般使用plsql developer,这个工具超级强大,你想要的功能都有。








下面再贴一下客户端链接oracle服务器的设置。如果是VS可以使用ODAC,这样就不用装客户端了,装好之后有VS的插件可用,也可以不使用第3方的plsql developer了。ODAC也请去oracle官方网站下载。








不知道各位用不用得上。高手或者dba就见笑了。后续会写.net使用oracle的一些体验。这篇没多少字,就是些图,也不想好好排版了,大家凑合着看吧。