简体   繁体   English

如何在matlab中检查本机ODBC连接的状态?

[英]how to check status of native ODBC connection in matlab?

Brief introduction of the problem: 问题简介:

the main problem is not in the connection procedure, i could connect to database successfully, and insert some rows in my database(firs code block shows this),but after closing the connection if someone tries to insert a row in the database ,matlab will terminate suddenly without any clear error message, (i expect to have a function to check if the connection is open or close or to get an error message to handle the error but non of these happened and just matlab closed because of a fatal error) 主要问题不在连接过程中,我可以成功连接到数据库,并在我的数据库中插入一些行(第一个代码块显示这个),但在关闭连接后,如果有人试图在数据库中插入一行,matlab将突然终止没有任何明确的错误消息,(我希望有一个功能来检查连接是打开还是关闭,或者得到一个错误消息来处理错误但是没有发生这些并且由于致命错误而被关闭了matlab)

i wrote the following code to connect to MS SQL SERVER database in matlab: 我写了以下代码连接到matlab中的MS SQL SERVER数据库:

conn=database.ODBCConnection('MS SQL SERVER','','');
insert(conn,'trace',{'obj_id','obj_type_id','time_step','pos_x','pos_y','vel_x','vel_y'},[1,1,1,0,0,0,0]);
close(conn);

and every thing was ok. 一切都很好。

then i tried to insert another row ( to check what is the error message ) then Matlab closed (due to fatal error) without showing any error message. 然后我试图插入另一行( 以检查错误消息是什么 )然后Matlab关闭(由于致命错误)而没有显示任何错误消息。

i tried to use following functions to get status of database connection before inserting new raws: 在插入新raw之前,我尝试使用以下函数来获取数据库连接的状态:

isconnection(conn);
ping(conn);

but it says 但它说

Undefined function 'ping' for input arguments of type 'database.ODBCConnection'. 未定义的函数'ping'表示'database.ODBCConnection'类型的输入参数。

Undefined function 'isconnection' for input arguments of type 'database.ODBCConnection' 对于'database.ODBCConnection'类型的输入参数,未定义的函数'isconnection'

even i tried to use try-catch block but it didn't work and Matlab Closed for fatal error. 即使我试图使用try-catch块,但它没有工作和Matlab关闭致命错误。

so i want to know is there any way to chek status of native ODBC to prevent sudden close of matlab in case of a closed connection?? 所以我想知道是否有任何方法来解决原生ODBC的状态,以防止在关闭连接的情况下突然关闭matlab?


Update: 更新:

>> conn=database.ODBCConnection('MS SQL SERVER','','')

conn = 

ODBCConnection with properties:

  Instance: 'MS SQL SERVER'
  UserName: ''
   Message: []
    Handle: [1x1 database.internal.ODBCConnectHandle]
   TimeOut: 0
AutoCommit: 0
      Type: 'ODBCConnection Object'

 >> close(conn)
 >> conn

 conn = 

 ODBCConnection with properties:

  Instance: 'MS SQL SERVER'
  UserName: ''
   Message: []
    Handle: [1x1 database.internal.ODBCConnectHandle]
   TimeOut: 0
AutoCommit: 0
      Type: 'ODBCConnection Object'

no properties or message changed before and after closing the connection, the problem is that i don't know how to check that if a connection is still open or closed in other parts of a program! 在关闭连接之前和之后没有更改任何属性或消息,问题是我不知道如果在程序的其他部分中连接仍然打开或关闭,如何检查! in this case if i use an insert command when a connection was closed before, matlab suddenly terminates (and show the message MATLAB(R2013B) has stopped working), so i want to know is there any way to check if a native odbc connection has closed before? 在这种情况下如果我在连接关闭之前使用插入命令,matlab突然终止(并显示消息MATLAB(R2013B)已停止工作),所以我想知道有没有办法检查本机odbc连接是否有关闭之前?


Further update 进一步更新

>> conn=database('MS SQL SERVER','','')

conn =

   Instance: 'MS SQL SERVER'
   UserName: ''
     Driver: []
        URL: []
Constructor: [1x1 com.mathworks.toolbox.database.databaseConnect]
    Message: []
     Handle: [1x1 sun.jdbc.odbc.JdbcOdbcConnection]
    TimeOut: 0
 AutoCommit: 'on'
       Type: 'Database Object'

>> isconnection(conn)

ans =

 1

>> close(conn)
>> isconnection(conn)

ans =

 0

i mean a function like "isconnection" in the example above for jdbc connection which returns 1 if a connection is open and 0 if the connection closed before. 我的意思是上面例子中的“isconnection”函数用于jdbc连接,如果连接打开则返回1,如果连接之前关闭则返回0。

I request you to check the database connection with toolstrip functionality of Matlab. 我请求您使用Matlab的toolstrip功能检查数据库连接。 You can find complete guide from here... 你可以从这里找到完整的指南......

You can perform the testing first so that you can ruled out of any problem with server.. 您可以先执行测试,以便排除服务器的任何问题。

Once it is connected successfully..you can check the code.connection settings and apply it in your code accordingly. 一旦连接成功..您可以检查code.connection设置并相应地将其应用于您的代码中。

Regards, 问候,

As per the documentation you can check status of an existing database.ODBCConnection or database.ODBCCursor in the Database Toolbox by checking the value of the Message property in the database.ODBCConnection object and the database.ODBCCursor Object. 根据文档,您可以通过检查database.ODBCConnection对象和database.ODBCCursor对象中Message属性的值来检查Database Toolbox中现有database.ODBCConnection或database.ODBCCursor的状态。

You may need to set Error handling to store using setdbprefs('ErrorHandling','store') . 您可能需要使用setdbprefs('ErrorHandling','store')将错误处理设置为存储。 Use setdbprefs('ErrorHandling','report') to switch it back again. 使用setdbprefs('ErrorHandling','report')再次将其切换回来。

ping and isconnection only work on database connection object and not on database.ODBCConnection objects. pingisconnection仅适用于数据库连接对象,而不适用于database.ODBCConnection对象。

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

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