![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Mysql Read
Hi All,
I did quite a few searches online trying to find an example on mysql read in an MT4 expert to no avail. There are bits here and there but not one complete example of this. Does anyone have a full example they can share?
I would settle for an odbc read as well.
Thank you all!
Hi All,
I did quite a few searches online trying to find an example on mysql read in an MT4 expert to no avail. There are bits here and there but not one complete example of this. Does anyone have a full example they can share?
I would settle for an odbc read as well.
Thank you all!I've moved your post to this thread. I do not understand what you need but maybe it could help.
All,
I've seen a couple of threads about using databases in Metatrader, but I havent seen anything specific about connecting to a Microsoft SQL Server. So, I thought I'd put together a quick tutorial of how to do this in case anybody wants to do something similar. This process should work for any data source that can be accessed via ADO/ODBC/OLE DB (SQL, Access, Excel, etc...)
The following code snippets are a little technical, and will require some basic coding knowledge.
For this task, I started with the ExpertSample sample C++ project in your Metatrader folder "experts\samples\DLLSample". I didn't want to start from scratch figuring out the right type of COM library to build, so I simply used this sample project.
Once you've opened up the project (you'll need Visual Studio), you're ready to use SQL in 4 easy steps!
1) Add a reference to the appopriate ADO dlls at the top of your ExpertSample.cpp page, like so:
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
[/PHP]
2) Then you're ready to add the ExecuteScalar method to your code. This function will only return one value from SQL. (Metatrader can only receive basic data types).
MT4_EXPFUNC char * __stdcall ExecuteScalar(char *strSQL)
{
HRESULT hr = S_OK;
char tmpChar[255];
try {
// Define string variables.
_bstr_t strCnn(MY_CONNECTION_STRING); //http://www.connectionstrings.com for more info
::CoInitialize(NULL);
_RecordsetPtr pRstAuthors = NULL;
// Call Create instance to instantiate the Record set
hr = pRstAuthors.CreateInstance(__uuidof(Recordset));
if(FAILED(hr))
{
::CoUninitialize();
return "ERROR: Failed creating record set instance";
}
//Open the Record set for getting records from Author table
try {
pRstAuthors->Open(strSQL,strCnn, adOpenStatic, adLockReadOnly,adCmdText);
} catch (_com_error & ce1) {
::CoUninitialize();
return "ERROR: Unable to open SQL Server";
}
try {
pRstAuthors->MoveFirst();
} catch(_com_error) {
::CoUninitialize();
return ""; //empty data
}
//Loop through the Record set
if (!pRstAuthors->EndOfFile)
{
_variant_t tmpvariant;
//Get the first column value
tmpvariant = pRstAuthors->GetFields()->GetItem((long)0)->GetValue();
strcpy(tmpChar,(_bstr_t)tmpvariant);
}
if (pRstAuthors->State == adStateOpen)
pRstAuthors->Close();
pRstAuthors = NULL;
::CoUninitialize();
}
catch(_com_error & ce)
{
//_bstr_t strError = ce.ErrorMessage;
::CoUninitialize();
return "ERROR: Failed to get data.";
}
return tmpChar;
}
[/PHP]
3) Make sure to add you method name in the ExpertSample.def so that you can call it via Metatrader.
[PHP]
EXPORTS GetIntValue
GetDoubleValue
GetStringValue
GetArrayItemValue
SetArrayItemValue
GetRatesItemValue
SortStringArray
ProcessStringArray
ExecuteScalar
4) Once you add your compiled DLL to the "experts\libraries" folder, you can call your DLL method by adding the following to the top of your MQ4 file:
[PHP]
#import "ExpertSample.dll"
string ExecuteScalar(string strSQL);
#import
That's it! You're ready to start making database calls straight from Metatrader. I'm using this right now to play around with some SELECT/INSERT statements into my SQL Server, trying to play around with some advanced ordering logic.
My next goal: using Sockets from Metatrader to communicate to an external application, preferrably written in C#.
JuanHi Juan,
I managed to follow your steps using visual studio 2008 but when compiling the code I got the following error:
1>------ Build started: Project: ExpertSample, Configuration: Debug Win32 ------
1>Compiling...
1>ExpertSampleSrc.cpp
1>d:\studio\expertsample\expertsample\expertsamplesrc.cpp(221) : warning C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files\microsoft visual studio 9.0\vc\include\string.h(79) : see declaration of 'strcat'
1>d:\studio\expertsample\expertsample\expertsamplesrc.cpp(235) : error C2065: 'MY_CONNECTION_STRING' : undeclared identifier
1>d:\studio\expertsample\expertsample\expertsamplesrc.cpp(287) : warning C4172: returning address of local variable or temporary
1>Build log was saved at "file://d:\Studio\ExpertSample\ExpertSample\Debug\BuildLog.htm"
1>ExpertSample - 1 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
It looks like it doesnt recognize the "MY_CONNECTION_STRING".
I have compiled the original ExpertSample.cpp before adding your updates and it worked just fine generating the DLL.
Can you please help.
Thanks in advance,
way2Freedom
how to connect MSSQL from MT4
Hi,
Can anyone help to connect MSSQL from MT4, using DLL?
--thanks
V-Man