Wednesday, March 28, 2012

How to connect using C#

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;database=database_name"
Conn.Open(ConnectionString, "user_name", "password")

Not working C# code:

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

SqlConnection Connection = new SqlConnection(ConnectionString);
Connection.Open();
Connection.Close();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.|||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_name;" +
> "Data Source=dsn_name;" +
> "User ID=user_name;" +
> "Password=password;" +
> "Integrated Security=SSPI;";
> SqlConnection Connection = new SqlConnection(ConnectionString);
> 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|||> string ConnectionString = "Initial Catalog=database_name;" +
> "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_name;" +
"Data Source=database_server_name;" +
"Integrated Security=SSPI;";

--
Hope this helps.

Dan Guzman
SQL Server MVP

"AHM" <ingen@.spam.tak> wrote in message
news:MPG.1cb65eb0b33ffb61989705@.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;database=database_name"
> Conn.Open(ConnectionString, "user_name", "password")
> Not working C# code:
> string ConnectionString = "Initial Catalog=database_name;" +
> "Data Source=dsn_name;" +
> "User ID=user_name;" +
> "Password=password;" +
> "Integrated Security=SSPI;";
> SqlConnection Connection = new SqlConnection(ConnectionString);
> Connection.Open();
> Connection.Close();

No comments:

Post a Comment