Home > Archive > Microsoft SQL Server forum > March 2005 > How to connect using C#









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to connect using C#
AHM

2005-03-31, 8:03 pm

Hi

I'm trying to connect to a MS SQL Server using ASP.NET / C#. I've
already got some VB code that works but I'm unable to translate it to
working C# code. Can anyone help me out?

Working VB code:

Dim Conn
Conn = CreateObject("ADODB.Connection")
Const ConnectionString = " DSN=dsn_name;databas
e=database_name"
Conn. Open(ConnectionStrin
g, "user_name", "password")

Not working C# code:

string ConnectionString = "Initial Catalog=database_nam
e;" +
"Data Source=dsn_name;" +
"User ID=user_name;" +
"Password=password;" +
"Integrated Security=SSPI;";

SqlConnection Connection = new SqlConnection(Connec
tionString);
Connection.Open();
Connection.Close();
elRoyFlynn

2005-03-31, 8:03 pm

Use exactly the same connection string form C# as for VB. If it works
in one, it'll work in the other.

Specifically, if you are using SSPI, you don't need to specify uid and
pw, and vice-versa.

Erland Sommarskog

2005-03-31, 8:03 pm

AHM (ingen@spam.tak) writes:
> I'm trying to connect to a MS SQL Server using ASP.NET / C#. I've
> already got some VB code that works but I'm unable to translate it to
> working C# code. Can anyone help me out?


If you help us to help you. Don't just post:

> Not working C# code:
>
> string ConnectionString = "Initial Catalog=database_nam
e;" +
> "Data Source=dsn_name;" +
> "User ID=user_name;" +
> "Password=password;" +
> "Integrated Security=SSPI;";
>
> SqlConnection Connection = new SqlConnection(Connec
tionString);
> Connection.Open();
> Connection.Close();


Tell us in what way it is not working. Do you get an error message?
Do you get unexpected results? Does the light go out?

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp
Dan Guzman

2005-03-31, 8:03 pm

> string ConnectionString = "Initial Catalog=database_nam
e;" +
> "Data Source=dsn_name;" +
> "User ID=user_name;" +
> "Password=password;" +
> "Integrated Security=SSPI;";


Note the change to 'Data Source' in the example below as well as the removal
of the unnecessary User ID and Password parameters:

string ConnectionString = "Initial Catalog=database_nam
e;" +
"Data Source=database_serv
er_name;" +
"Integrated Security=SSPI;";

--
Hope this helps.

Dan Guzman
SQL Server MVP

"AHM" <ingen@spam.tak> wrote in message
news:MPG. 1cb65eb0b33ffb619897
05@news.inet.tele.dk...
> Hi
>
> I'm trying to connect to a MS SQL Server using ASP.NET / C#. I've
> already got some VB code that works but I'm unable to translate it to
> working C# code. Can anyone help me out?
>
> Working VB code:
>
> Dim Conn
> Conn = CreateObject("ADODB.Connection")
> Const ConnectionString = " DSN=dsn_name;databas
e=database_name"
> Conn. Open(ConnectionStrin
g, "user_name", "password")
>
> Not working C# code:
>
> string ConnectionString = "Initial Catalog=database_nam
e;" +
> "Data Source=dsn_name;" +
> "User ID=user_name;" +
> "Password=password;" +
> "Integrated Security=SSPI;";
>
> SqlConnection Connection = new SqlConnection(Connec
tionString);
> Connection.Open();
> Connection.Close();



Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com