C#OleDbConnection设置为超时 [英] C# OleDbConnection con set to timeout
本文介绍了C#OleDbConnection设置为超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个应用程序查询Access数据库并显示数据。我想连接(con)2分钟后超时。
I have an app that queries an Access database and shows the data. I want the connection(con) to timeout after 2 minutes. Does anyone have any suggestions on how i can code this?
这是我在开始时所拥有的
this is what i have in the beginning
OleDbConnection con;
OleDbDataReader dr;
OleDbCommand cmd;
con.Open();
cmd = new OleDbCommand(str, con);
dr = cmd.ExecuteReader();
谢谢
推荐答案
不要共享连接,在需要时创建连接,并使用块
Don't share the connection, create the connection when you need and wrap it by using block
,
如果您需要设置超时,可以使用连接字符串中的 ConnectionTimeout
属性(例如.....; (OleDbConnection con = new OleDbConnection(connectionString))$ b $连接超时= 30
if you need to set the timeout, you can set by using ConnectionTimeout
property in connection string (e.g. ".....;Connect Timeout=30"
using (OleDbConnection con = new OleDbConnection(connectionString))
using (OleDbCommand cmd = new OleDbCommand(str, con))
{
con.Open();
using (OleDbDataReader dr = cmd.ExecuteReader())
{
}
}
这篇关于C#OleDbConnection设置为超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文