Simple ADO problem

A

Alex

I run the following and I get a messagebox telling me the connection
is closed (OK so far) and then it crashes. The error message is: SQL
Server does not exist or access denied.

SQL Manager is running and I tested it by running an older ADO
assignment (which worked). So I'm not sure what I'm doing wrong with
the following bare-bones code...

My code follows.
===
using System;
using System.Windows.Forms;
using System.Data.SqlClient;

public class ConnectionTest
{
public static void Main()
{
try
{
SqlConnection myConnection = new SqlConnection("Initial
Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");


MessageBox.Show("Connection is " +
myConnection.State.ToString());

myConnection.Open();
MessageBox.Show("Connection is " +
myConnection.State.ToString());

myConnection.Close();
MessageBox.Show("Connection is " +
myConnection.State.ToString());
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
 
C

Chris R. Timmons

I run the following and I get a messagebox telling me the
connection is closed (OK so far) and then it crashes. The error
message is: SQL Server does not exist or access denied.

SQL Manager is running and I tested it by running an older ADO
assignment (which worked). So I'm not sure what I'm doing wrong
with the following bare-bones code...

Alex,

Your code works fine on my machine.

Some things to try:

- Make sure you have SQL server set up to use either integrated
or mixed-mode security (see "mixed mode security" in
the SQL server books online for more info)
- Change your connection string to use a username/password
instead of integrated security.
- Use the MS Data Connection editor to build and test a
connection string. If you don't have Visual Studio, you
can invoke the editor by creating an empty file with a "UDL"
file extension. (that extension is registered with the
editor). Double-clicking on the file will bring up the
Data Connection editor. The connection string you build will
be saved to the UDL file in INI format.
- Check out www.connectionstrings.com for some more tips.

Hope this helps.

Chris.
 
A

Alex

Alex,

Your code works fine on my machine.

Some things to try:

- Make sure you have SQL server set up to use either integrated
or mixed-mode security (see "mixed mode security" in
the SQL server books online for more info)

I'm using the neutered sqlmangr.exe version (not the real SQL Server).
But, I don't think this is the problem...
- Change your connection string to use a username/password
instead of integrated security.

I want to make this secure and generic but, again, I don't think this
is the problem...
- Use the MS Data Connection editor to build and test a
connection string. If you don't have Visual Studio, you
can invoke the editor by creating an empty file with a "UDL"
file extension. (that extension is registered with the
editor). Double-clicking on the file will bring up the
Data Connection editor. The connection string you build will
be saved to the UDL file in INI format.

I did that - that's how I ended up with my connection string. I also
pressed the "test connection" button with no errors/warnings.
- Check out www.connectionstrings.com for some more tips.

What's really weird is that, in VB, this same code seems to work. So
I'm left with the conclusion that my C# code must be at fault yet you
have told me my code works fine.

My code again (different version but still giving me the same
problem):

===

using System;
using System.Windows.Forms;
using System.Data.SqlClient;

public class ConnectionTest
{
SqlConnection(connectionString);
public static void Main()
{
try
{
string connectionString = "Integrated Security=SSPI;Persist
Security Info=False;Initial Catalog=CI632Appointments;Data
Source=alex-jayme\netsdk";
SqlConnection myConnection = new
SqlConnection(connectionString);

MessageBox.Show("Connection is " +
myConnection.State.ToString());

myConnection.Open();
MessageBox.Show("Connection is " +
myConnection.State.ToString());

myConnection.Close();
MessageBox.Show("Connection is " +
myConnection.State.ToString());
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
 
C

Chris R. Timmons

What's really weird is that, in VB, this same code seems to
work. So I'm left with the conclusion that my C# code must be at
fault yet you have told me my code works fine.

My code again (different version but still giving me the same
problem):

string connectionString = "Integrated
Security=SSPI;Persist
Security Info=False;Initial Catalog=CI632Appointments;Data
Source=alex-jayme\netsdk";

Alex,

The \n is causing the error. That puts a newline in the string.
Either escape it (\\n), or precede the entire string with the @
symbol (e.g. @"Integrated Security;Persist...")

Hope this helps.

Chris.
 
A

Alex

The \n is causing the error. That puts a newline in the string.
Either escape it (\\n), or precede the entire string with the @
symbol (e.g. @"Integrated Security;Persist...")

Thank you very much! That's what I get for writing VB for too long...

One more question, though. How do I get the host name without typing
it? If I type it out, no errors, and everything performs as expected
but if I use "Data Source=localhost" or "Data Source=(local)" I get
the error again ("SQL Server does not exist or access denied.").
 
C

Chris R. Timmons

Thank you very much! That's what I get for writing VB for too
long...

One more question, though. How do I get the host name without
typing it? If I type it out, no errors, and everything performs
as expected but if I use "Data Source=localhost" or "Data
Source=(local)" I get the error again ("SQL Server does not
exist or access denied.").

Alex,

I'm not sure why the "localhost" or "(local)" name won't work for
you. Here's a Google thread that seems to cover most of the possible
causes:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-
8&threadm=xUxM0SWFEHA.1988%40cpmsftngxa06.phx.gbl&rnum=51&prev=/group
s%3Fas_q%3D%2522sql%2520server%2522%2520(local%2520OR%2520localhost)%
2520connection%26safe%3Dimages%26ie%3DUTF-8%26oe%3DUTF-
8%26as_ugroup%3Dmicrosoft.*%26as_scoring%3Dd%26lr%3D%26num%3D100%26hl
%3Den

or

http://tinyurl.com/2od46


Hope this helps.

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top