[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.odbcnet

Connecting to (ConnectionString) SQL Server problem (generic opening a dB problem

raylopez99

4/1/2007 3:43:00 PM

I’m trying to connect to a database using C++.NET (Visual Studio
2005), however my problem--a Connection String problem--is pretty
generic I think and not limited to C++.NET.

I’m having problems—seems like the database, created in MS SQL Server
Express 2005, which ‘works’ and is open (I've run a Procedure on it,
and it works fine, and the syntax is fine insofar as I can 'save' it
with no problems), can’t be found by the program. The program
searches locally, then the Internet, then fails after a few minutes.

Below is the program.

One potential problem I see (BTW I’m clueless, my second day doing
this) is that the source code resides on a separate hard drive, the
“D” drive, and is complied on that hard drive, while the database is
on the “C” drive. Hence the program searches local (but not every
drive IMO), then my firewall asks permission to search the Internet,
and after a few minutes (or rather about 45 sec) says “No connection
the following error occurred!!!: An error has occurred while
establishing a connection to the server. When connecting to SQL
Server 2005, this failure may be caused by the fact that under the
default settings SQL Server does not allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a
connection to SQL Server) The connection to the database has been
closed Press any key to continue . . .”

Any ideas? Perhaps the ‘true path’ has to be specified (with all the
backslashes, etc)? Notice I am using “local” below for the Data
Source (Server). Also I assume it's OK for the database to be "open"
rather than "closed" while the console program below runs.

RL

C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data //?this is my
path

>>Data Source=MyPentiumPCNameHere\SQLEXPRESS;Initial Catalog=DCV_DB;Integrated Security=True;Pooling=False << ?This is my Connection String (says SQL Server 2005 Express, under “Properties”)

// The database DCV_DB resides in the path above

// This is my source code (compiles OK). Though in C++, I trust C#
and even other language developers can follow it, as it’s pretty
simple

////start
using namespace System;
using namespace System::Data;
using namespace System::Data::SqlClient;
//above namespaces recommended as default by textbook (Frasier)
////////////
void main()
{
SqlConnection^ connection = gcnew SqlConnection();

connection->ConnectionString = "User ID=sa; Password=;"
"Data Source=(local); Initial Catalog=DCV_DB;";

//the above connection string recommended by my textbook by Stephen
Fraser; I tried other variants without luck

try
{
connection->Open();
Console::WriteLine("We got a connection!");
}
catch (SqlException ^e)
{
Console::WriteLine("No connection the following error
occurred!!!: {0}",
e->Message);
}
finally
{
connection->Close();
Console::WriteLine("The connection to the database has been
closed");
}
}
///////// end

2 Answers

raylopez99

4/1/2007 8:51:00 PM

0

On Apr 1, 8:42 am, "raylopez99" <raylope...@yahoo.com> wrote:
> I''m trying to connect to a database using C++.NET (Visual Studio
> 2005), however my problem--a Connection String problem--is pretty
> generic I think and not limited to C++.NET.
>
> I''m having problems-seems like the database, created in MS SQL Server
> Express 2005, which ''works'' and is open (I''ve run a Procedure on it,
> and it works fine, and the syntax is fine insofar as I can ''save'' it
> with no problems), can''t be found by the program. The program
> searches locally, then the Internet, then fails after a few minutes.
>

Upon further research I see this is a hairy problem in general
(connecting to a dB)

RL


raylopez99

4/2/2007 3:43:00 PM

0

Turns out the solution was almost trivial, thanks to a spot by Erland
Sommarskog below

RL

On Apr 1, 3:41 pm, Erland Sommarskog <esq...@sommarskog.se> wrote:
> raylopez99 (raylope...@yahoo.com) writes:

> > Yes, but for some reason \SQLEXPRESS doesn''t compile in VS2005 "S not
> > recognized" error
>
> If you program in C++ (and I assume C#) you need to double the \, as
> \ has a special meaning in string literals in C++.
>
> --

Yes it worked! You are correct Erland. The double slash did work,
and the program compiled and worked perfectly. (for some reason the
forward slash, which usually also works for paths in lieu of the
double slash, did not compile however).

For the record here is what worked for the connection string:

connection->ConnectionString = "Persist Security Info = False;
Integrated Security=SSPI;" "Data Source=(local)\\SQLEXPRESS; Initial
Catalog=MyDATABASEnameHERE_DB;";

//note the double slash: \
No need to supply a password in the connection string (using Windows
Authentication).

Thanks again! Now I can pickup this book on SQL again, which I set
aside.

RL