怎么判断数据库连接超时
来源:互联网 发布:投诉数据分析 编辑:程序博客网 时间:2024/03/29 08:29
如何判断数据库连接超时
环境VS2010+MSSQL
连接数据库时,如果是远程数据库,如192.232.1.53,则要连接很长时间,如何才能设置超时时间。如5秒。
我用Connect Timeout=5,或Command.CommandTimeout = 5;都不起作用。
------解决方案--------------------
多线程
环境VS2010+MSSQL
连接数据库时,如果是远程数据库,如192.232.1.53,则要连接很长时间,如何才能设置超时时间。如5秒。
我用Connect Timeout=5,或Command.CommandTimeout = 5;都不起作用。
------解决方案--------------------
多线程
- C# code
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Threading;using System.Data.SqlClient;namespace WindowsApplication3{ public partial class Form1 : Form { bool Return=true; AutoResetEvent sleepSynchro = new AutoResetEvent(false); Exception err = null; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Thread controlThread = new Thread(new ThreadStart(test)); controlThread.Start(); if (!sleepSynchro.WaitOne(3000, false)) { MessageBox.Show("数据库连接超时.", "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (!Return) { MessageBox.Show("数据库连接失败:" + err.Message, "错误:", MessageBoxButtons.OK, MessageBoxIcon.Error); } } void test() { string sqlconn = @"Data Source=3.0.0.0;Initial Catalog=eDocument;Integrated Security=True"; SqlConnection conn = new SqlConnection(sqlconn); try { conn.Open(); Return = true; } catch (Exception e) { Return = false; err = e; } finally { conn.Close(); sleepSynchro.Set(); } } }}
------解决方案--------------------
- C# code
string str="server={0};database={1};uid={2};pwd={3};Connection Timeout={4} "; str=String.Format(str,Server,DBName,UserId,Passwd,5); SqlConnection Conn=new qlConnection(str); Conn.Open(); //5秒这个时间是指数据库连接的时间,不包括解析IP地址、寻找机器的时间,而且第一次连接会慢很多,//所以LZ可以把时间设置打点
------解决方案--------------------
让程序判断 try...catch 是最好
------解决方案--------------------
public DataTable getDataTable(string strSQL)
{
DataTable dt1 = new DataTable();
using (SqlConnection myConnection = new SqlConnection(connetion))
{
SqlDataAdapter da = new SqlDataAdapter(strSQL, myConnection);
da.SelectCommand.CommandTimeout = 1000;
da.SelectCommand.CommandType = CommandType.Text;
da.Fill(dt1);
da.Dispose();
myConnection.Close();
myConnection.Dispose();
}
return dt1;
}
0 0
- 怎么判断数据库连接超时
- Session超时怎么判断
- 数据库连接超时
- 超时判断
- Mysql5数据库连接超时问题
- 数据库连接超时的问题
- dbcp数据库连接超时解决方案
- Oracle数据库连接超时解决方案
- 【MySQL】关于数据库连接超时
- MySQL5数据库连接超时问题
- dbcp数据库连接超时解决方案
- MySql数据库连接超时处理
- SpagoBI数据库连接超时问题解决
- Oracle设置数据库连接超时
- mysql 数据库连接超时
- mySql数据库连接超时错误
- Mysql数据库连接超时解决方案
- 超时时间已到。 数据库连接超时
- JSP内置对象之四个作用域
- grub手动引导、修复win7 Mbr
- linux c 线程的创建与结束 (传参和返回值)
- Unable to create requested service [org.hibernate.engine.spi.CacheImple
- make 2>&1 | tee build.log 解释
- 怎么判断数据库连接超时
- 深度优先搜索 DFS 算法摘记
- weblogic启动项目报错
- springMvc 静态文件问题
- ffmpeg 截取视频中任意一帧或多帧图片
- 123123
- Vim 配置
- 临时文档11--字符串
- Problem C. Sorting---Round A China New Grad Test 2014