Sei sulla pagina 1di 3

7/6/2016 databasehowtocheckstatusofnativeODBCconnectioninmatlab?

StackOverflow

signup login tour help

_
StackOverflowisacommunityof4.7 JointheStackOverflowcommunityto:
millionprogrammers,justlikeyou,
helpingeachother.

Jointhemitonlytakesaminute:
Askprogramming Answerandhelp Getrecognizedforyour
Signup questions yourpeers expertise

howtocheckstatusofnativeODBCconnectioninmatlab?

Briefintroductionoftheproblem:

themainproblemisnotintheconnectionprocedure,icouldconnecttodatabasesuccessfully,andinsertsomerowsinmydatabase(firscode
blockshowsthis),butafterclosingtheconnectionifsomeonetriestoinsertarowinthedatabase,matlabwillterminatesuddenlywithoutany
clearerrormessage,(iexpecttohaveafunctiontocheckiftheconnectionisopenorcloseortogetanerrormessagetohandletheerrorbut
nonofthesehappenedandjustmatlabclosedbecauseofafatalerror)

iwrotethefollowingcodetoconnecttoMSSQLSERVERdatabaseinmatlab:

conn=database.ODBCConnection('MSSQLSERVER','','');
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);

andeverythingwasok.

thenitriedtoinsertanotherrow(tocheckwhatistheerrormessage)thenMatlabclosed(duetofatalerror)withoutshowinganyerror
message.

itriedtousefollowingfunctionstogetstatusofdatabaseconnectionbeforeinsertingnewraws:

isconnection(conn);
ping(conn);

butitsays

Undefinedfunction'ping'forinputargumentsoftype'database.ODBCConnection'.

Undefinedfunction'isconnection'forinputargumentsoftype'database.ODBCConnection'

evenitriedtousetrycatchblockbutitdidn'tworkandMatlabClosedforfatalerror.

soiwanttoknowisthereanywaytochekstatusofnativeODBCtopreventsuddencloseofmatlabincaseofaclosedconnection??

Update:

>>conn=database.ODBCConnection('MSSQLSERVER','','')

conn=

ODBCConnectionwithproperties:

Instance:'MSSQLSERVER'
UserName:''
Message:[]
Handle:[1x1database.internal.ODBCConnectHandle]
TimeOut:0
AutoCommit:0
Type:'ODBCConnectionObject'

>>close(conn)
>>conn

conn=

http://stackoverflow.com/questions/30617852/howtocheckstatusofnativeodbcconnectioninmatlab 1/3
7/6/2016 databasehowtocheckstatusofnativeODBCconnectioninmatlab?StackOverflow
ODBCConnectionwithproperties:

Instance:'MSSQLSERVER'
UserName:''
Message:[]
Handle:[1x1database.internal.ODBCConnectHandle]
TimeOut:0
AutoCommit:0
Type:'ODBCConnectionObject'

nopropertiesormessagechangedbeforeandafterclosingtheconnection,theproblemisthatidon'tknowhowtocheckthatifaconnectionis
stillopenorclosedinotherpartsofaprogram!inthiscaseifiuseaninsertcommandwhenaconnectionwasclosedbefore,matlabsuddenly
terminates(andshowthemessageMATLAB(R2013B)hasstoppedworking),soiwanttoknowisthereanywaytocheckifanativeodbc
connectionhasclosedbefore?

Furtherupdate

>>conn=database('MSSQLSERVER','','')

conn=

Instance:'MSSQLSERVER'
UserName:''
Driver:[]
URL:[]
Constructor:[1x1com.mathworks.toolbox.database.databaseConnect]
Message:[]
Handle:[1x1sun.jdbc.odbc.JdbcOdbcConnection]
TimeOut:0
AutoCommit:'on'
Type:'DatabaseObject'

>>isconnection(conn)

ans=

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

ans=

imeanafunctionlike"isconnection"intheexampleaboveforjdbcconnectionwhichreturns1ifaconnectionisopenand0iftheconnection
closedbefore.

database matlab odbc native

editedJun18'15at22:04 askedJun3'15at10:44
darenshan
268 3 14

2Answers

Asperthedocumentationyoucancheckstatusofanexistingdatabase.ODBCConnectionor
database.ODBCCursorintheDatabaseToolboxbycheckingthevalueoftheMessageproperty
inthedatabase.ODBCConnectionobjectandthedatabase.ODBCCursorObject.

YoumayneedtosetErrorhandlingtostoreusing setdbprefs('ErrorHandling','store') .Use


setdbprefs('ErrorHandling','report') toswitchitbackagain.

ping and isconnection onlyworkondatabaseconnectionobjectandnoton


database.ODBCConnectionobjects.

answeredJun6'15at6:55
Appleman1234
12.2k 18 44

tnxalot,iupdatedthequestioninresponsetoyouranswer,ithinktheerroroccurinmatlaborin"native
odbc"layer,sothereisnomessageinconnectionobject, darenshan Jun6'15at17:46

http://stackoverflow.com/questions/30617852/howtocheckstatusofnativeodbcconnectioninmatlab 2/3
7/6/2016 databasehowtocheckstatusofnativeODBCconnectioninmatlab?StackOverflow

IrequestyoutocheckthedatabaseconnectionwithtoolstripfunctionalityofMatlab.Youcanfind
completeguidefromhere...

Youcanperformthetestingfirstsothatyoucanruledoutofanyproblemwithserver..

Onceitisconnectedsuccessfully..youcancheckthecode.connectionsettingsandapplyitin
yourcodeaccordingly.

Regards,

answeredJun12'15at2:46
SandeepGandhi
11 1 6

tnxalot,isawthatdocumentationbefore,butthemainproblemisnotintheconnectionprocedure,icould
connecttodatabasesuccessfully,andinsertsomerowinmydatabase(firscodeblockshowsthis),butafter
closingtheconnectionifsomeonetriestoinsertarowinthedatabase,matlabwillclosesuddenlywithout
anyclearerrormessage,(iexpecttohaveafunctiontocheckiftheconnectionisopenorcloseortogetan
errormessagetohandletheerrorbutnonofthesehappenedandjustmatlabclosedbecauseofafatalerror)
darenshan Jun12'15at13:36

http://stackoverflow.com/questions/30617852/howtocheckstatusofnativeodbcconnectioninmatlab 3/3

Potrebbero piacerti anche