Sql Connection打开和关闭 [英] Sql Connection Open and close
本文介绍了Sql Connection打开和关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有关于sql连接打开和关闭每个记录(1000次)的问题.
有人给我一种最有效的方法
选项1:打开和关闭每个记录的连接
选项2:保持打开所有记录插入的连接
Hi,
I have question regarding sql connection open and close each record (1000 times).
Some one give me an idea which is most efficient way to do
Option 1: Open and close connection for every record
Option 2: keep open the connection for all record insert
public void InsertEmployment(DLAEmployeeExport item, SqlConnection sqlConnection1)
{
try
{
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("InsertEmployment", sqlConnection1);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("@BusinessTitle", SqlDbType.Char);
cmd.Parameters["@BusinessTitle"].Direction = ParameterDirection.Input;
cmd.Parameters["@BusinessTitle"].Value = item.PositionCategory;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
}
catch (System.Exception se)
{
Console.WriteLine(se.Message);
}
finally
{
/// if the connection is not null
if (sqlConnection1 != null)
{
///check if the connection is open, if so then close it
if (sqlConnection1.State == ConnectionState.Open)
sqlConnection1.Close();
}
}
}
推荐答案
我认为答案将是显而易见的-将其打开直到完成为止.
我还要看看 http://msdn.microsoft.com /en-us/library/system.data.sqlclient.sqlbulkcopy.aspx [
I''d think the answer would be fairly obvious - keep it open until you''ve finished.
I''d also take a look at http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx[^] since it sounds like you are doing a bulk update
Best regards
Espen Harlinn
请了解有关连接池 [
Please learn about connection pooling[^]
Open your connection as late as possible, close it as soon as possible. Use a database system that manages an efficient connection pool that will take care of the details of managing the most efficient lifespan of a connection.
这篇关于Sql Connection打开和关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文