我在 Transaction.cs 中有这段代码
using (TransactionScope scope = new TransactionScope())
{
// Setup nhibernate configuration
Configuration config = new Configuration();
config.SetProperty("hibernate.connection.connection_string", GlobalVar.TRUECONNSTRING);
config.SetProperty("hibernate.command_timeout", "3600");
config.AddAssembly(typeof(ProductionMovein).Assembly);
// Setup nhibernate session
ISessionFactory factory = config.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();
//Recalculate Number
PairData pairCabang = (PairData)comboCabang.SelectedItem;
textNo.Text = FormFunction.getNumber(2, pairCabang.key, dtpTanggal.Value);
// Insert data
try
{
//ProductionMoveIn
ProductionMovein productionMoveIn = new ProductionMovein();
productionMoveIn.Nomor = textNo.Text;
session.Save(productionMoveIn);
transaction.Commit();
session.Close();
}
catch (Exception ex)
{
transaction.Rollback();
session.Close();
MessageBox.Show(ex.InnerException.Message);
return 1;
}
scope.Complete();
}
错误是从
开始的textNo.Text = FormFunction.getNumber(2, pairCabang.key, dtpTanggal.Value);
我在 Formfunction.cs 中有这段代码
public static string getNumber(int formID, int cabangID, DateTime date)
{
string formNumber = "";
string strQuery = "";
formNumber += formNames[formID, 0] + "/" + + date.ToString("yy") + date.ToString("MM") + "/";
// Setup nhibernate configuration
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
config.SetProperty("hibernate.connection.connection_string", GlobalVar.TRUECONNSTRING);
config.SetProperty("hibernate.command_timeout", "3600");
config.AddAssembly(typeof(Login).Assembly);
//// Setup nhibernate session
ISessionFactory factory = config.BuildSessionFactory();
ISession session = factory.OpenSession();
strQuery = "SELECT MAX(REVERSE(SUBSTRING(REVERSE(a.Nomor), 1, 5))) as 'latest' FROM " + formNames[formID, 1] +
" a WHERE a.cabang = " + cabangID +
" AND YEAR(a.tanggal) = '" + date.ToString("yyyy");
Object result = session.CreateSQLQuery(strQuery)
.AddScalar("latest", NHibernateUtil.Int32)
.UniqueResult();
session.Close();
int nRow;
if (result == null)
nRow = 0;
else
nRow = (int)result;
formNumber += (nRow + 1).ToString("d5");
return formNumber;
}
我尝试将服务器更改为 10.10.7.10(我的 IP)并且它有效。但是,当我更改为其他IP时,它无法打开连接。 我尝试在我的计算机和我尝试连接的其他服务器上打开 msdtc,但仍然出现相同的错误。
有人可以帮我解决这个错误吗?
最佳答案
您使用哪个数据库? (我假设是 MS SQL) 您能发布异常详细信息吗?
这是一种方法。
- 首先注释掉
using (TransactionScope)
/scope.Complete
并尝试连接到远程数据库 - 即确保您的 本地Sql客户端配置为TCP/IP,远程服务器允许 远程 TCP/IP 连接(通常是端口 1433),并且您的登录 凭据和访问权限正确。 - 通常仅在使用 2 个或更多连接时才需要 DTC。如果 您恢复 TransactionScope,但然后删除 NHibernate 交易,那么可能根本不需要 DTC。另请注意 TransactionScopes 默认为读可序列化隔离 - TransactionScope functions
确保您的 PC 和服务器上都配置了 DTC,以便 允许远程连接等 - 。
您还需要解决防火墙问题 DTC firewall requirements?
编辑
默认情况下,SQL Express 不对远程连接开放 - 在远程服务器上,您需要在 SQL 配置管理器上启用 TCP/IP,打开 1433/1434 的防火墙,并且正如 @Özgür 提到的那样,确保浏览器服务正在运行的 SQL(或更改实例名称,或更改连接字符串以使用 ip、端口)。更多信息请参见:http://www.sevenforums.com/system-security/58817-remote-access-sql-server-express-2008-windows-7-a.html
关于c# - 如果服务器不是本地,则无法打开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10082660/