C# 在我的课堂上放什么';s处理方法
我第一次编写了自己的数据访问层类,所以我想将其封装在(使用)括号中,但这需要该类实现Idisposable 我放进去的唯一东西是C# 在我的课堂上放什么';s处理方法,c#,class,dispose,C#,Class,Dispose,我第一次编写了自己的数据访问层类,所以我想将其封装在(使用)括号中,但这需要该类实现Idisposable 我放进去的唯一东西是 conn.close() 还有什么我应该补充的吗 课程概述: public class DAL : IDisposable { SqlConnection conn; SqlCommand cmd; SqlTransaction transaction; public int Status = 1; public DAL()
conn.close()
还有什么我应该补充的吗
课程概述:
public class DAL : IDisposable
{
SqlConnection conn;
SqlCommand cmd;
SqlTransaction transaction;
public int Status = 1;
public DAL()
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString);
conn.Open();
}
public void InsertRequest(UserAndRequestInfo UInfo, UserCrops UCrop)
{
}
public void InsertBGs(BreedingGroups BG)
{
}
public void EndTransaction()
{
if (Status == 1)
transaction.Commit();
if (Status < 0)
transaction.Rollback();
}
public void EndConnection()
{
conn.Close();
}
void Dispose()
{
conn.Close();
}
公共类DAL:IDisposable
{
SqlConnection-conn;
SqlCommand命令cmd;
SqlTransaction;
公共int状态=1;
公营部门()
{
conn=新的SqlConnection(ConfigurationManager.ConnectionString[“MyDB”].ConnectionString);
conn.Open();
}
公共void InsertRequest(userandrequestinfouinfo,UserCrops-UCrop)
{
}
公共空白插入BG(繁殖组BG)
{
}
公开作废EndTransaction()
{
如果(状态==1)
Commit();
如果(状态<0)
transaction.Rollback();
}
公共无效EndConnection()
{
康涅狄格州关闭();
}
无效处置()
{
康涅狄格州关闭();
}
致电:
using (DAL db = new DAL())
{
foreach (var crop in UInfo.UserCropInfo)
{
db.InsertRequest(UInfo, crop);
foreach (BreedingGroups bg in crop.BGs)
{
db.InsertBGs(bg);
}
db.EndTransaction();
}
db.EndConnection(); <---- //If 'using' is there I'll remove this line
}
使用(DAL db=new DAL())
{
foreach(UInfo.UserCropInfo中的变量crop)
{
db.InsertRequest(UInfo,crop);
foreach(作物中的繁殖组bg.BGs)
{
db.InsertBGs(bg);
}
db.EndTransaction();
}
db.EndConnection();Yes:对类中的任何一次性变量调用Dispose()
void Dispose()
{
if(cmd != null)
cmd.Dispose();
if(transaction != null)
transaction.Dispose();
if(conn != null)
conn.Dispose();
}
编辑:conn.Dispose()
也将调用conn.Close()
,因此,如果调用Dispose,则不需要同时调用Close。是:对类中的任何一次性变量调用Dispose()
void Dispose()
{
if(cmd != null)
cmd.Dispose();
if(transaction != null)
transaction.Dispose();
if(conn != null)
conn.Dispose();
}
编辑:conn.Dispose()
也将调用conn.Close()
,因此,如果调用Dispose,您不需要同时调用Close。这只是您的一个选择,使用InsertRequest方法中的使用范围,如下所示:
public class DAL
{
public void InsertRequest(UserAndRequestInfo UInfo, UserCrops UCrop)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString))
{
conn.Open();
SqlCommand command = conn.CreateCommand();
SqlTransaction transaction;
// Start a local transaction.
transaction = conn.BeginTransaction("SampleTransaction");
command.Connection = conn;
command.Transaction = transaction;
try
{
command.CommandText = "Insert into ...";
command.ExecuteNonQuery();
command.CommandText = "Insert into ...";
command.ExecuteNonQuery();
// Attempt to commit the transaction.
transaction.Commit();
}
catch (Exception ex)
{
// Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}
}
}
public void InsertBGs(BreedingGroups BG)
{
}
}
请注意,您不需要在构造中建立数据库连接,请将其删除。
并使用相同的概念实现InsertBGs方法。
这是调用
DAL db = new DAL();
foreach (var crop in UInfo.UserCropInfo)
{
db.InsertRequest(UInfo, crop);
foreach (BreedingGroups bg in crop.BGs)
{
db.InsertBGs(bg);
}
}
这只是您的一个选择,在InsertRequest方法中使用作用域,如下所示:
public class DAL
{
public void InsertRequest(UserAndRequestInfo UInfo, UserCrops UCrop)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString))
{
conn.Open();
SqlCommand command = conn.CreateCommand();
SqlTransaction transaction;
// Start a local transaction.
transaction = conn.BeginTransaction("SampleTransaction");
command.Connection = conn;
command.Transaction = transaction;
try
{
command.CommandText = "Insert into ...";
command.ExecuteNonQuery();
command.CommandText = "Insert into ...";
command.ExecuteNonQuery();
// Attempt to commit the transaction.
transaction.Commit();
}
catch (Exception ex)
{
// Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine(" Message: {0}", ex2.Message);
}
}
}
}
public void InsertBGs(BreedingGroups BG)
{
}
}
请注意,您不需要在构造中建立数据库连接,请将其删除。
并使用相同的概念实现InsertBGs方法。
这是调用
DAL db = new DAL();
foreach (var crop in UInfo.UserCropInfo)
{
db.InsertRequest(UInfo, crop);
foreach (BreedingGroups bg in crop.BGs)
{
db.InsertBGs(bg);
}
}
您是否使用终结器作为DAL类?不,如果是这样的话,则不调用GC。您是否使用终结器作为DAL类?不,如果是这样的话,则不调用GC。值得注意的是,如果没有此类变量,则使用空的Dispose()
是合法的。值得注意的是,如果没有此类变量,则使用空的Dispose()
是合法的。