检查数据库连接/打开 [英] check database connection/open

查看:78
本文介绍了检查数据库连接/打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,我想查看数据库连接是否打开/是否打开,如果没有,它会在消息框中向用户发布数据库

连接错误


代码如下:


Dim objConnection As New SqlConnection _

(" Server = server1; database = db1; user

id = user1; password = whatever")

Dim objDataAdapter As New SqlDataAdapter()

Dim objDataSet


objDataAdapter.SelectCommand =新的SqlCommand()

objDataAdapter.SelectCommand.Connection = objConnection

cmdtext ="从table1中选择参数"


objDataAdapter .SelectCommand.CommandText =声明cmdtext

objDataAdapter.SelectCommand.CommandType = CommandType.Text


objConnection.Open()

objDataAdapter。填充(objDataSet," table1")

objConnection.Close()

因此,如果连接有问题,它会发布如下错误的masseage


MessageBox.Show(" Ths是数据库连接问题,数据库连接

问题)

我的问题是使用哪个代码行检查数据库连接/打开或

不是,它是objConnection.Open()?如果是的话,怎么检查?谢谢

All, I want to check wether db connection/open or not, if not it will post db
connection error to user in message box

The code is like:

Dim objConnection As New SqlConnection _
("Server=server1; database=db1;user
id=user1;password=whatever")
Dim objDataAdapter As New SqlDataAdapter()
Dim objDataSet

objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = objConnection
cmdtext = "Select parameter from table1"

objDataAdapter.SelectCommand.CommandText = cmdtext
objDataAdapter.SelectCommand.CommandType = CommandType.Text

objConnection.Open()
objDataAdapter.Fill(objDataSet, "table1")
objConnection.Close()

So if connection problem, it will post error masseage like below

MessageBox.Show("Ths is Database connenction issue", "DataBase Connection
problem")

My question is which code line is used to check if db connection/open or
not, it is objConnection.Open()? if it is, how to check? Thanks


推荐答案



martin1写道:

martin1 wrote:

全部,我想检查数据库连接是否打开/是否打开,如果没有,它会在消息框中向用户发布数据库

连接错误


代码如下:

Dim objConnection As New SqlConnection _

(" Server = server1; database = db1; user

id = user1; password = whatever")

Dim objDataAdapter As New SqlDataAdapter()

Dim objDataSet


objDataAdapter.SelectCommand =新的SqlCommand()

objDataAdapter.SelectCommand.Connection = objConnection

声明cmdtext = QUOT;从表1"选择参数;


objDataAdapter.SelectCommand.CommandText =声明cmdtext

objDataAdapter.SelectCommand.CommandType = CommandType.Text


objConnection.Open()

objDataAdapter.Fill(objDataSet," table1& ;)

objConnection.Close()


因此,如果连接有问题,它会发布如下错误的masseage


MessageBox.Show(Ths is Database connenction issue,DataBase Connection

problem)


我的问题是使用了哪个代码行检查db connection / open或

是不是,它是objConnection.Open()?如果是的话,怎么检查?谢谢
All, I want to check wether db connection/open or not, if not it will post db
connection error to user in message box

The code is like:

Dim objConnection As New SqlConnection _
("Server=server1; database=db1;user
id=user1;password=whatever")
Dim objDataAdapter As New SqlDataAdapter()
Dim objDataSet

objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = objConnection
cmdtext = "Select parameter from table1"

objDataAdapter.SelectCommand.CommandText = cmdtext
objDataAdapter.SelectCommand.CommandType = CommandType.Text

objConnection.Open()
objDataAdapter.Fill(objDataSet, "table1")
objConnection.Close()

So if connection problem, it will post error masseage like below

MessageBox.Show("Ths is Database connenction issue", "DataBase Connection
problem")

My question is which code line is used to check if db connection/open or
not, it is objConnection.Open()? if it is, how to check? Thanks



好​​像你需要做的就是把一个.Fill方法调用到一个

Try / Catch块中。

Seems like all you need to do is put the .Fill method call inside of a
Try/Catch block.


连接具有状态。物业,你可以检查。


-tom


martin1 ha scritto:
Connections have the "state" property, which you can check.

-tom

martin1 ha scritto:

全部,我想检查数据库连接是否打开/是否打开,如果没有,它会在消息框中向用户发布数据库

连接错误


代码如下:


Dim objConnection As New SqlConnection _

(" Server = server1; database = db1; user

id = user1; password = whatever")

Dim objDataAdapter As New SqlDataAdapter()

Dim objDataSet


objDataAdapter.SelectCommand =新的SqlCommand()

objDataAdapter.SelectCommand.Connection = objConnection

cmdtext ="从table1中选择参数"


objDataAdapter .SelectCommand.CommandText =声明cmdtext

objDataAdapter.SelectCommand.CommandType = CommandType.Text


objConnection.Open()

objDataAdapter。填充(objDataSet," table1")

objConnection.Close()


因此,如果出现连接问题,它会发布如下错误的masseage


MessageBox.Show(" ;这是数据库连接问题,数据库连接

问题)

我的问题是用哪个代码行来检查db connection / open或者

不是,它是objConnection.Open()?如果是的话,怎么检查?谢谢
All, I want to check wether db connection/open or not, if not it will post db
connection error to user in message box

The code is like:

Dim objConnection As New SqlConnection _
("Server=server1; database=db1;user
id=user1;password=whatever")
Dim objDataAdapter As New SqlDataAdapter()
Dim objDataSet

objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = objConnection
cmdtext = "Select parameter from table1"

objDataAdapter.SelectCommand.CommandText = cmdtext
objDataAdapter.SelectCommand.CommandType = CommandType.Text

objConnection.Open()
objDataAdapter.Fill(objDataSet, "table1")
objConnection.Close()

So if connection problem, it will post error masseage like below

MessageBox.Show("Ths is Database connenction issue", "DataBase Connection
problem")

My question is which code line is used to check if db connection/open or
not, it is objConnection.Open()? if it is, how to check? Thanks


谢谢。 Tom


它的工作原理


"到************** @ uniroma1.it"写道:
Thank you. Tom

it works

"to**************@uniroma1.it" wrote:

连接具有状态物业,你可以检查。


-tom


martin1 ha scritto:
Connections have the "state" property, which you can check.

-tom

martin1 ha scritto:

全部,我想检查数据库连接是否打开/是否打开,如果没有,它会在消息框中向用户发布数据库

连接错误


代码如下:


Dim objConnection As New SqlConnection _

(" Server = server1; database = db1; user

id = user1; password = whatever")

Dim objDataAdapter As New SqlDataAdapter()

Dim objDataSet


objDataAdapter.SelectCommand =新的SqlCommand()

objDataAdapter.SelectCommand.Connection = objConnection

cmdtext ="从table1中选择参数"


objDataAdapter .SelectCommand.CommandText =声明cmdtext

objDataAdapter.SelectCommand.CommandType = CommandType.Text


objConnection.Open()

objDataAdapter。填充(objDataSet,ta ble1")

objConnection.Close()


因此如果连接有问题,它会发布如下错误的masseage


MessageBox.Show(Ths is Database connenction issue,DataBase Connection

problem)


我的问题是使用了哪个代码行检查db connection / open或

是不是,它是objConnection.Open()?如果是的话,怎么检查?谢谢
All, I want to check wether db connection/open or not, if not it will post db
connection error to user in message box

The code is like:

Dim objConnection As New SqlConnection _
("Server=server1; database=db1;user
id=user1;password=whatever")
Dim objDataAdapter As New SqlDataAdapter()
Dim objDataSet

objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = objConnection
cmdtext = "Select parameter from table1"

objDataAdapter.SelectCommand.CommandText = cmdtext
objDataAdapter.SelectCommand.CommandType = CommandType.Text

objConnection.Open()
objDataAdapter.Fill(objDataSet, "table1")
objConnection.Close()

So if connection problem, it will post error masseage like below

MessageBox.Show("Ths is Database connenction issue", "DataBase Connection
problem")

My question is which code line is used to check if db connection/open or
not, it is objConnection.Open()? if it is, how to check? Thanks



这篇关于检查数据库连接/打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆