SqlConnection does't work with CString?????
-
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.
-
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.
I'm not sure why this isn't working. Try breaking it down to see what part is actually causing the exception: CString hello("Hello"); gcroot<String ^> HelloStr = gcnew String(hello); gcroot<SqlConnection ^> m_SqlDbConnection = gcnew SqlConnection(HelloStr); The System.String constructor should be using the LPCTSTR operator of CString so I expect it should work. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder