开发者

Cannot connect to local SQL database using C++ (SQL Server 2008 R2)

I am having issues to connect to a database from a C++ program (VS 2008).

I have created the database using SQL Server and both SQL Server and SQL Server Browser are running, TCP/IP and Remote Access开发者_Go百科 are enabled.

I can connect to my database using sqlcmd, but when I use:

System::Data::SqlClient::SqlConnection ^_SqlConnection = gcnew System::Data::SqlClient::SqlConnection();

_SqlConnection->ConnectionString = "Data Source=MyComp\SQLEXPRESS; Initial Catalog=DbName; Integrated Security=True";

_SqlConnection->Open();

I get the classic error

Named Pipes Provider, error: 40 - Could not open a connection to SQL Server

I have copied the connection string from the database properties found in the Server Explorer of VS, so I guess it's correct.

Thanks a lot for any help.


Found the solution. Correct syntax for connection string is:

"Data Source=MyComp\\SQLEXPRESS; Initial Catalog=DbName; Integrated Security=True"; 

With 2 back-slashes \\ in the server name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜