2016-02-12 29 views
0

我想從託管的C++代碼連接到SQL Server。但是我無法與SQL Server建立連接。我從Visual Studio連接到SQL Server並複製/粘貼連接字符串。此連接字符串目前也適用於C#應用程序。使用C++/CLI的SQL Server

我的代碼如下:

#include "stdafx.h" 
#using <mscorlib.dll> 
#using <System.dll> 
#using <system.data.dll> 
#include <tchar.h> 
#include "SqlServer.h" 

using namespace System; 
using namespace System::Data; 
using namespace System::Data::SqlClient; 
using namespace System::Xml; 

bool getAllUsers() 
{ 
    SqlConnection^ connection; 
    //SqlDataAdapter^ dataAdapter; 
    //DataSet^ dataSet; 
    SqlDataReader^ dataReader; 
    SqlCommand^ sqlCommand; 

    connection = gcnew SqlConnection("Data Source=(local)\OMEGA;Initial Catalog=Omega;Integrated Security=True"); 
    //dataAdapter = gcnew SqlDataAdapter(); 
    //dataSet = gcnew DataSet(); 
    sqlCommand = gcnew SqlCommand("select * from applicationuser", connection); 

    connection->Open(); 

    dataReader = sqlCommand->ExecuteReader(); 

    while (dataReader->Read()) 
    { 
     Console::WriteLine(dataReader->GetSqlString(0)); 
    } 

    connection->Close(); 

    return true; 
} 

Exception

回答

2

反斜槓 '\' '歐米茄' 之前沒有逃脫。試試\\ OMEGA。

+0

這就是問題所在 – greyfox