ODP.NET和TransactionScope
微软提供oracle的驱动默认是不支持TransactionScope,除非安装oracle相应的程序,但是odp.net即oracle提供驱动不存在此类问题,
namespace OracleTransactionScopeApp
{
using System;
using System.Data;
using Oracle.DataAccess.Client;
using System.Transactions;
using System.Data.Common;
class Program
{
static void Main(string[] args)
{
try
{
using (TransactionScope tran = new TransactionScope())
{
ExampleDao dao = new ExampleDao();
dao.Excute1();
dao.Excute2();
dao.Excute3();
tran.Complete();
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadLine();
}
}
public class ExampleDao
{
protected void Excute(Action<IDbCommand> action)
{
using (OracleConnection con = new OracleConnection("Password=\"password\";User ID=userid;Data Source=database;"))
{
using (OracleCommand com = con.CreateCommand())
{
try
{
con.Open();
action(com);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
con.Close();
}
}
}
}
public void Excute1()
{
this.Excute(com =>
{
com.CommandText = "select sysdate from dual";
Console.WriteLine(com.ExecuteScalar());
Console.WriteLine(Transaction.Current.TransactionInformation.LocalIdentifier);
}
);
}
public void Excute2()
{
this.Excute(com =>
{
com.CommandText = "select sysdate from dual";
Console.WriteLine(com.ExecuteScalar());
Console.WriteLine(Transaction.Current.TransactionInformation.LocalIdentifier);
});
}
public void Excute3()
{
this.Excute(com =>
{
com.CommandText = "select sysdate from dual";
Console.WriteLine(com.ExecuteScalar());
Console.WriteLine(Transaction.Current.TransactionInformation.LocalIdentifier);
});
}
}
}
但是在使用的时候发现一个小问题,当数据库服务器是Oracle11时,客户端是Oracle10,而程序一直报用户名和密码错误,在查询相关资料后,发现oracle11的密码可能区分大小写的。解决方式,一个是设置数据库密码不区分大小写,第二个就是在密码上加上引号。
引用: