传统上,当从sproc中检索数据时使用DbCommand时,类似以下内容是一种很好的做法:
DbCommand cmdDbCommand...
dbGetData = DatabaseFactory.CreateDatabase("MyDatabase");
cmdDbCommand = dbGetData.GetStoredProcCommand("MySproc");
.
.
.
try
{
...
}
catch (System.Exception ex)
{
if (cmdDbCommandcmdDbCommand != null)
{
if (cmdDbCommand.Connection.State == ConnectionState.Open)
{
cmdDbCommand.Connection.Close();
cmdDbCommand.Dispose();
}
}
}
现在,给定以下类型的SubSonic呼叫:
try
{
StoredProcedure sp = SPs.GetSprocData(someID, result, errorMessage);
dsResults = sp.GetDataSet();
intResGetUserDetails = (int)sp.OutputValues[0];
errorMessage = (string)sp.OutputValues[1];
}
catch (System.Exception ex)
{
...
}
如何明确确保数据库连接已关闭?
答案 0 :(得分:1)
sproc完成后连接关闭。这是Sobsonic 2的内置。