Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ SQL » MSSQL access from Linux
MSSQL access from Linux [message #23437] Mon, 19 October 2009 11:34 Go to next message
chickenk is currently offline  chickenk
Messages: 169
Registered: May 2007
Location: Grenoble, France
Experienced Member
Hi,

I would like to know if some access to a MSSQL server from a linux box is possible using U++. I know that the library FreeTDS (http://www.freetds.org/) does it, so I wondered if Mirek got something working ?
That would be an invaluable help for me right now, since I must access a remote MSSQL server to link data with a test framework, which is currently (and will always be) running under Linux.

Thanks for your comments, remarks and suggestions. If somehow Mirek can have a look at FreeTDS... Maybe wrappers could be done.

regards,
Lionel
Re: MSSQL access from Linux [message #23438 is a reply to message #23437] Mon, 19 October 2009 14:11 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
chickenk wrote on Mon, 19 October 2009 05:34

Hi,

I would like to know if some access to a MSSQL server from a linux box is possible using U++. I know that the library FreeTDS (http://www.freetds.org/) does it, so I wondered if Mirek got something working ?



Yes. It works. Current MSSQL package uses ODBC and works in Linux.

All you need to do is to configure the damn thing, which is quite tricky.... Smile

Anyway, here is a piece of code I use to connect (direct copy&paste from the app, so you will have to sort that out :):

#ifdef flagODBC
bool Login(ODBCSession& session)
#else
bool Login(OleDBSession& session)
#endif
{
	LoginDlg dlg;
	dlg.Icon(HollyImg::holly());
	LoadFromFile(dlg, ConfigFile("login.cfg"));
	String s = GetIniKey("DATABASE");
	if(s.GetCount())
		dlg.database <<= s;
#ifndef _DEBUG
	dlg.password <<= Null;
	dlg.password.Password();
#endif
	for(;;) {
		if(dlg.Run() != IDOK) {
			StoreToFile(dlg, ConfigFile("login.cfg"));
			return false;
		}
	#ifdef PLATFORM_WIN32
		String provider = Nvl(GetIniKey("CONNECT"), "SQL Server Native Client 10.0");
	#else
		String provider = Nvl(GetIniKey("CONNECT"), "MSSQL");
	#endif
	#ifdef flagODBC
		#ifdef PLATFORM_POSIX
			String dfn = "/usr/lib/libtdsodbc.so.*";
			SetIfExists(dfn, "/usr/local/lib/libtdsodbc.so.*");
			SetIfExists(dfn, GetHomeDirFile("libtdsodbc.so.*"));
			SaveFile(GetHomeDirFile(".odbc.ini"),
				String().Cat() << "[MSSQL]\n"
					<< "Driver = " << dfn << '\n'
					<< "Description = MSSQL\n"
					<< "ServerName = MSSQL\n"
			);
			String c =
				"[MSSQL]\n"
				"tds version = 8.0\n"
				"client charset  = WINDOWS-1250\n"
			;
			String host = ~dlg.database;
			String port;
			int q = host.ReverseFind(',');
			if(q >= 0)
				c << "host = " << host.Mid(0, q) << '\n'
				  << "port = " << host.Mid(q + 1) << '\n';
			else
				c << "host = " << host << '\n';
			SaveFile(GetHomeDirFile(".freetds.conf"), c);
			String cs = "DSN=MSSQL;SERVER=MSSQL;";
		#else
			String cs = "Driver={SQL Server Native Client 10.0}";
			cs << ";Server=" << ~dlg.database << ';';
		#endif
		if(dlg.windows)
			cs << "Trusted_Connection=Yes;";
		else
			cs << "UID=" << ~dlg.username << ";PWD=" << ~dlg.password << ';';
		if(session.Connect(cs))
			break;
	#else
		String propset;
		propset << "Provider=" << provider << ";Data Source=" << ~dlg.database
		        << ";Trusted_Connection=Yes;";
		if(dlg.windows ? session.OpenProp(propset) :
		   session.Open(~dlg.username, ~dlg.password, ~dlg.database, provider))
			break;
	#endif
		Exclamation("[A4 Connection failed!]&[A1 " + DeQtfLf(session.GetLastError()));
	}
	StoreToFile(dlg, ConfigFile("login.cfg"));
	return true;
}


The nasty trick used there is that login process creates some .ini file for FreeTDS Smile

Mirek
Re: MSSQL access from Linux [message #23439 is a reply to message #23438] Mon, 19 October 2009 14:33 Go to previous message
chickenk is currently offline  chickenk
Messages: 169
Registered: May 2007
Location: Grenoble, France
Experienced Member
Amazing.

I believe the FreeTDS config stuff should make its way in one of upp packages to enable direct connectivity from u++ to mssql. And then, of course, advertise it in Upp features! Should interesting other people than me.

Thanks a lot Mirek, once again you saved my life. Very Happy

regards
Lionel
Previous Topic: OCILIB in U++?
Next Topic: Database Scheme(*.sch): Can two tables share a field with the same field name?
Goto Forum:
  


Current Time: Sat Apr 20 05:53:20 CEST 2024

Total time taken to generate the page: 1.19027 seconds