English 中文(简体)
Proper Usage of SqlConnection in .NET
原标题:Proper Usage of SqlConnection in .NET

我只想就使用SqlConnection物体的适当使用或适当设计发表意见。 以下2种最佳用途:

一种数据提供者类别,其方法(其中每一种方法)含有SqlConnection物体(并在使用时处理)。 和:

IList<Employee> GetAllEmployees() 
{ 
  using (SqlConnection connection = new SqlConnection(this.connectionString)) { 
  // Code goes here... 
  } 
} 

Employee GetEmployee(int id) 
{ 
  using (SqlConnection connection = new SqlConnection(this.connectionString)) { 
  // Code goes here... 
  } 
}   

SqlConnection connection; // initialized in construct或 
IList<Employee> GetAllEmployees() 
{ 
  this.TryOpenConnection(); // tries to open member SqlConnection instance 
  // Code goes here... 
  this.CloseConnection(); 
  // return 
} 

Employee GetEmployee(int id) 
{ 
  this.TryOpenConnection(); // tries to open member SqlConnection instance 
  // Code goes here... 
  this.CloseConnection(); 
  // return 
}

Or is there a better approach than this? I have a focused web crawler type of application and this application will crawl 50 或 m或e websites simultaneously (multithreaded) with each website contained in a crawler object and each crawler object has an instance of a data provider class (above).

最佳回答

实际数据库链接将汇集起来。 只要你所有的<代码>SqlConnection都使用同样的连接线,它们就能够真正使用同样的连接线。

I find it cleaner to create the connection instance, use it, then dispose it (in a using block). That way, if the code needs to change to use a different connection string, to use a transaction, or whatever, you have all you need to make the change available right there.

问题回答

也许与实际无关,但我把任何联系分开的唯一时间反对叙级一级,是我要明确把对每种方法的关切分开...... 也许它太长,需要重新纳入几个部分,其中每一个部分都需要在作为交易一部分的同一关系下运作。

页: 1

Create a connection
Start transaction
Call a subroutine to update an order header (passing along the connection or get it from the instance)
Call a subroutine to update all order details (passing along the conneciton or get it from the instance)
End transaction
Close connection

否则,我基本上坚持选择。 1. W/联接基本上为你付出了代价。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签