简体   繁体   English

C#/甲骨文:打开/关闭连接

[英]c# / oracle: open/close connection

I need help with database connections and my winapp. 我需要数据库连接和winapp方面的帮助。

I have a windows app (C#) that, after I log in, starts running 5 or 6 different queries in Oracle database, every 5-10 seconds. 我有一个Windows应用程序(C#),登录后,每5-10秒开始在Oracle数据库中运行5或6个不同的查询。 Application is up 24/7. 申请24/7。

What is a proper way to do this? 什么是正确的方法? Should I open connection during login and never close it until I close the app, or should I open and close connection every time I run a query? 我应该在登录期间打开连接,而在关闭应用程序之前永远不要关闭它,还是应该在每次运行查询时打开和关闭连接? For example: 例如:

//first query
conn.Open();
DataSet ds1 = new DataSet();

string sql = "SELECT * FROM table1";

OracleCommand cmd = new OracleCommand(sql, conn);
cmd.CommandType = CommandType.Text;
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(ds1, "Result1");
conn.Dispose();

return ds1;

//second query    
conn.Open();
DataSet ds2 = new DataSet();

string sql = "SELECT * FROM table2";

OracleCommand cmd = new OracleCommand(sql, conn);
cmd.CommandType = CommandType.Text;
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(ds2, "Result2");
conn.Dispose();

return ds2;

What's the best way to do this? 最好的方法是什么?

The Oracle OleDB provider you use in your application implements pooling. 您在应用程序中使用的Oracle OleDB提供程序实现了池化。 So, when you create and clore a connection, you only take/release a connection from a pool of 'real connection'. 因此,在创建和克隆连接时,仅从“真实连接”池中进行连接/释放连接。

This makes creating and disposing Connection objects a really cheap operation. 这使得创建和处理Connection对象成为一项非常便宜的操作。 If I were you, I would open a connection, execute my batch of queries, and close+dispose that connection as soon as I'm done and going to sleep (even for a few seconds). 如果您是我,我将打开一个连接,执行我的一系列查询,并在完成并进入睡眠后(即使几秒钟)立即关闭并释放该连接。

Another option, of course is to have a single connection for each 'batch' of queries 当然,另一种选择是为每个“批”查询具有单个连接

However, if you're hitting the database that often then it sounds like you need a permanent connection for the duration of the application. 但是,如果您经常访问数据库,那么听起来好像在应用程序期间需要永久连接。

I asuume of course your sample was just by example, and you are not actaully hitting the database with raw SQL. 我当然认为您的示例只是作为示例,而您并不是用原始SQL来实际访问数据库。

I suggest you to use using block in order to clean your non managed object 我建议您使用using块来clean您的non managed object

using(var connection = new OracleConnection("..."))
{
 .....

}

Nota : Using execute dispose on your object in the end of treatment 注意:在处理结束时使用execute对您的对象进行处理

Link : http://msdn.microsoft.com/fr-fr/library/yh598w02(v=vs.80).aspx 链接: http : //msdn.microsoft.com/fr-fr/library/yh598w02(v=vs.80).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM