Can't create SqlConnection from CString [modified]
-
Hi All, I am newbie for .net. I have created the simple console application and trying to create connection to SQL database. Here is my code for console app:
#include "stdafx.h" #include "vcclr.h" #include "atlstr.h" #using "mscorlib.dll" #using "System.dll" #using "System.Data.dll" using namespace System; using namespace System::Data; using namespace System::Data::SqlClient; int _tmain(int argc, _TCHAR* argv[]) { CString hello("Hello"); gcroot m_SqlDbConnection = gcnew SqlConnection(gcnew String(hello)); return 0; }
I have the following exeption An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: Format of the initialization string does not conform to specification starting at index 0. on the stringgcroot m_SqlDbConnection = gcnew SqlConnection(gcnew String(hello));
Please help me to resolve the problem or point me to the article.:( P.S. I found that CString defined as in the atlstr.htypedef CStringT< TCHAR, StrTraitATL< TCHAR, ChTraitsCRT< TCHAR > > > CAtlString; typedef CAtlString CString; Maybe it helps.
-- modified at 14:32 Friday 20th April, 2007 -
Hi All, I am newbie for .net. I have created the simple console application and trying to create connection to SQL database. Here is my code for console app:
#include "stdafx.h" #include "vcclr.h" #include "atlstr.h" #using "mscorlib.dll" #using "System.dll" #using "System.Data.dll" using namespace System; using namespace System::Data; using namespace System::Data::SqlClient; int _tmain(int argc, _TCHAR* argv[]) { CString hello("Hello"); gcroot m_SqlDbConnection = gcnew SqlConnection(gcnew String(hello)); return 0; }
I have the following exeption An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: Format of the initialization string does not conform to specification starting at index 0. on the stringgcroot m_SqlDbConnection = gcnew SqlConnection(gcnew String(hello));
Please help me to resolve the problem or point me to the article.:( P.S. I found that CString defined as in the atlstr.htypedef CStringT< TCHAR, StrTraitATL< TCHAR, ChTraitsCRT< TCHAR > > > CAtlString; typedef CAtlString CString; Maybe it helps.
-- modified at 14:32 Friday 20th April, 2007It failed because the connection string you provided it, "Hello", isn't a valid description of what kind and which database you want to connect to. You might want to have a look at ConnectionStrings.com[^] for many examples of what connection strings for various databases are supposed to look like. My C++ is really rusty, but it loks like you need to back away from the database stuff for now and start by learning the basics of CLI before you jump into the deeper end of the .NET Pool.
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007