Tag Archives: C#

.Net中var的使用方式

The following restrictions apply to implicitly-typed variable declarations:

  • var can only be used when a local variable is declared and initialized in the same statement; the variable cannot be initialized to null, or to a method group or an anonymous function.
  • var cannot be used on fields at class scope.
  • Variables declared by using var cannot be used in the initialization expression. In other words, this expression is legal: int i = (i = 20); but this expression produces a compile-time error: var i = (i = 20);
  • Multiple implicitly-typed variables cannot be initialized in the same statement.
  • If a type named var is in scope, then the var keyword will resolve to that type name and will not be treated as part of an implicitly typed local variable declaration.

You may find that var can also be useful with query expressions in which the exact constructed type of the query variable is difficult to determine. This can occur with grouping and ordering operations.

The var keyword can also be useful when the specific type of the variable is tedious to type on the keyboard, or is obvious, or does not add to the readability of the code. One example where var is helpful in this manner is with nested generic types such as those used with group operations. In the following query, the type of the query variable is IEnumerable>. As long as you and others who must maintain your code understand this, there is no problem with using implicit typing for convenience and brevity.

However, the use of var does have at least the potential to make your code more difficult to understand for other developers. For that reason, the C# documentation generally uses var only when it is required.

Link: http://msdn.microsoft.com/en-us/library/bb384061.aspx

Transaction for OracleConnection in .Net

1. 单个数据库连接

可以直接使用OracleTransaction对象做事务处理

using(var transaction = connection.BeginTransaction())
{
.....在这个作用域上的数据库操作,必须基于这个transaction
using(var command = new OracleCommand(commandText, connection) { Transaction = transaction })
{
...
}
...
transaction.Commit(); //transaction.Rollback();
}

2. 多个数据库连接

可以使用TransactionScope对象来维护多个数据库连接中的事务处理

var options = new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted, Timeout = new TimeSpan(0, 2, 0) };

using (var transaction = new TransactionScope(TransactionScopeOption.Required, options))
{
.....在这个作用域里面的数据库操作,不需要传递transaction对象
using(var connection1 = new OracleConnection(connectionString1))
{
...
}

using(var connection2 = new OracleConnection(connectionString2))
{
...
}

transaction.Complete();
}

注1: 使用TransactionScope对象前,需要添加引用System.Transaction

注2: 对于多个Oracle连接来说,使用TransactionScope对象前,需要安装Oracle Services For MTS (Microsoft Transaction Server)。下载地址:http://goo.gl/XrKn

Convert integer to Enum instance

public void EnumInstanceFromInt()
{
// The .NET Framework contains an Enum called DayOfWeek.
// Let’s generate some Enum instances from int values.

// Usually you wouldn’t cast an instance of an existing Enum to an int
// in order to create an Enum instance. :-) You would have the actual
// integer value, perhaps a value from a database where the int value of
// the enum was stored.

DayOfWeek wednesday =
(DayOfWeek)Enum.ToObject(typeof(DayOfWeek), (int)DayOfWeek.Wednesday);
DayOfWeek sunday =
(DayOfWeek)Enum.ToObject(typeof(DayOfWeek), (int)DayOfWeek.Sunday);
DayOfWeek tgif =
(DayOfWeek)Enum.ToObject(typeof(DayOfWeek), (int)DayOfWeek.Friday);

lblOutput.Text = wednesday.ToString()
+ “. Int value = ” + ((int)wednesday).ToString() + “\n”;
lblOutput.Text += sunday.ToString()
+ “. Int value = ” + ((int)sunday).ToString() + “\n”;
lblOutput.Text += tgif.ToString()
+ “. Int value = ” + ((int)tgif).ToString() + “\n”;
}

Result:

Wednesday. Int value = 3
Sunday. Int value = 0
Friday. Int value = 5

Link: http://www.cambiaresearch.com/c4/52a7e5fe-c7fc-49ab-b21d-37e6194687f3/Convert-Integer-To-Enum-Instance-in-csharp.aspx