MSDOTnet.org Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Connecting to (ConnectionString) SQL Server problem (generic

 
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> ODBC Dotnet
Author Message
raylopez99



Joined: 08 Aug 2007
Posts: 13

PostPosted: Sun Apr 01, 2007 12:42 pm    Post subject: Connecting to (ConnectionString) SQL Server problem (generic Reply with quote

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

Archived from group: microsoft>public>dotnet>framework>odbcnet
Back to top
View user's profile Send private message
raylopez99



Joined: 08 Aug 2007
Posts: 13

PostPosted: Sun Apr 01, 2007 5:50 pm    Post subject: Re: Connecting to (ConnectionString) SQL Server problem (gen Reply with quote

On Apr 1, 8:42 am, "raylopez99" 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
Back to top
View user's profile Send private message
raylopez99



Joined: 08 Aug 2007
Posts: 13

PostPosted: Mon Apr 02, 2007 12:43 pm    Post subject: Re: Connecting to (ConnectionString) SQL Server problem (gen Reply with quote

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

RL

On Apr 1, 3:41 pm, Erland Sommarskog 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

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
Problem with ConnectionString and MySQL Hey everybody, since lacks support of SQLTables, I decided to write a little Class to call this function by invoking it from the native odbc32.dll. It works by now really fine except, when I try to connect to a DSN without giving a uid an

Problem in connecting to FoxPro DB (Type your message here) Hi- I am trying to connect to a FoxPro db. My application is built as a C# user control in .NET. The DB is a free table directory. I created a DSN (Microsoft Visual Foxpro Driver type) on the server and linked to the folder. It cr

Problem connecting to MySQL in IIS through ASP/VB Hi, I've been having problems trying to connect to MySQL through IIS via ODBC in ASP.NET (using VB). When I run the code in Visual Studio it works fine -- i.e. I'm able to enter data into the MySQL table. But running it under IIS it just won't work - no e

Generic Connection String Hi, I need to present the user with a list of Tables from a database. After they have selected the table I need to present them with a list of Fields. After they select the Fields they require, I need to use an to retrieve the Data. It NEED

connecting to HP NonStop SQL Hi All, Anybody developed a .Net App which connects to "HP NonStop SQL" database . If anybody worked with it, can you please let me know, what should be installed on my machine. Do we have any drivers for connecting from .Net App Please help me Thanks in
Post new topic   Reply to topic    MSDOTnet.org Forum Index -> ODBC Dotnet All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group