0

我第一次编写自己的数据访问层类,所以我想将它包装在一个(使用)括号内,但这需要类实现 Idisposable。

我放在那里的唯一东西是

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();
    }

调用自:

        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
        }

此外,在这种特殊情况下,调用 db.EndConnection() 方法会更好

4

2 回答 2

4

是的:调用Dispose()您班级中的任何一次性变量。

void Dispose()
{
    if(cmd != null)
        cmd.Dispose();
    if(transaction != null)
        transaction.Dispose();
    if(conn != null)
        conn.Dispose();
}

编辑:conn.Dispose()也会调用conn.Close(),所以如果你调用 dispose,你不应该也调用 close 它。

于 2013-11-12T14:18:24.753 回答
0

这只是您的选择,在InsertRequest方法中使用 using 范围,如下所示:

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)
    {

    }
} 

请注意,您不需要在 Constructure 中建立数据库连接,将其删除。并使用相同的概念来实现InsertBGs方法。这是调用

DAL db = new DAL();
foreach (var crop in UInfo.UserCropInfo)
{
   db.InsertRequest(UInfo, crop);
   foreach (BreedingGroups bg in crop.BGs)
   {
       db.InsertBGs(bg);
   }
}
于 2013-11-12T14:39:52.733 回答