question

Sheldon C4 avatar image
0 Likes"
Sheldon C4 asked Sheldon C4 commented

How to use Database.Connection in C++ SDK

I know Database.Connection in flexscript is how to work, but in the C++ SDK I don't know how to implement the DatabaseConnector.

in flexscript like this

Database.Connection con = Database.Connection("DatabaseConnector");//<<The connection string is my problem
con.connect();

but in C++ SDK? I try to stay in the same form, but it didn't work.

FlexSim::Database::Connection con;
con= FlexSim::Database::Connection("DatabaseConnector");//<<The connection string is my problem
con.connect();


FlexSim 22.1.0
FlexSim 22.0.1
c++database connectorsdk
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jeanette F avatar image Jeanette F ♦♦ commented ·

Hi @Sheldon C4, was one of Matthew Gillespie's or Jordan Johnson's answers helpful? If so, please click the "Accept" button at the bottom of the one that best answers your question. Or if you still have questions, add a comment and we'll continue the conversation.

If we haven't heard back from you within 3 business days we'll auto-accept an answer, but you can always unaccept and comment back to reopen your question.

0 Likes 0 ·
Matthew Gillespie avatar image
0 Likes"
Matthew Gillespie answered Sheldon C4 commented

I don't think you can use the Connection constructor like you can in FlexScript and so you probably need to use the connection's static global() method:

Database::Connection* con = Database::Connection::global("DatabaseConnector");
con->connect();
· 8
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Sheldon C4 avatar image Sheldon C4 commented ·

Hi Matthew, I use the global code, but it didn't work.

The error message is "LNK1120 1 unresolved externals".

globaltesterror.png

Thank's for your answer.


0 Likes 0 ·
globaltesterror.png (70.5 KiB)
Jordan Johnson avatar image Jordan Johnson ♦♦ Sheldon C4 commented ·

It looks like we didn't export that method for modules. Try this code instead:

treenode dbNode = executestring("Database.Connection(\"DatabaseConnection1\")");
if (!dbNode || !isclasstype(dbNode, "DatabaseConnection")) {
  // handle the invalid node case
} else {
  // This cast is safe because we checked in the if statement
  Database::Connection* con = dbNode->objectAs(Database::Connection);
  con->connect();
  // do other stuff
  con->disconnect();
}
0 Likes 0 ·
Sheldon C4 avatar image Sheldon C4 Jordan Johnson ♦♦ commented ·

I'm sorry late for response this question, but it didn't worked which the method "executestring" return variant type is double, can't cast into treenode.

1655046555478.png

0 Likes 0 ·
1655046555478.png (373.7 KiB)
Show more comments
Show more comments
Jordan Johnson avatar image
0 Likes"
Jordan Johnson answered Sheldon C4 edited

C++ and FlexScript are quite different. At one time, it was possible to write FlexScript code, and compile it as C++ code, but that isn't true anymore.

Based on the code sample, it looks like your issue is that con should be type FlexSim::Database::Connection*, not FlexSim::Database::Connection. The difference is the * at the end, to indicate that it's a pointer type.

If that isn't the issue, you'll need to clarify what you mean when you say it didn't work. Did you get compiler errors? Linker errors? Or maybe runtime errors? If so, what did the error say? The more specific you are, the better we will be able to assist you.

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Sheldon C4 avatar image Sheldon C4 commented ·

Jordan, thanks for your reply, the original problem is I don't know how to using the Flexsim's DatabaseConnector in C++ SDK, so I didn't get the error message.

Now I follow the Matthew's answer and trying to connect Flexsim DatabaseConnector,but it didn't work, the error message I wrote in Matthew's comment reply.


0 Likes 0 ·