Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 107,697 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,066 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



connecting to sql server 2000 from c#2008

 
Reply to this topicStart new topic

connecting to sql server 2000 from c#2008, connecting to sql server 2000 from c#2008

nsncreations
post 29 Jun, 2008 - 11:18 AM
Post #1


New D.I.C Head

*
Joined: 29 Jun, 2008
Posts: 1

connecting to sql server 2000 from c#2008


dear friends,

i have sqlserver2000 installed in my desktop and vs 2008 in my laptop, now i am trying to connect local sqlserver2005 express on laptop i am able to connect, but if i am trying to connect to sqlserver2000 on my desktop i am not able to connect to my database.

hear is the sample code:-


csharp

using System;
using System.Data;
using System.Data.SqlClient;
namespace Chapter09
{
class SqlServerProvider
{
static void Main(string[] args)
{
// set up connection string
string connString = @"server = NSN\NSN;integrated security = true;database = northwind";

// string connString = @"server =nsn;integrated security = true;database = northwind";

// set up query string
string sql = @"select * from employees ";
// declare connection and data reader variables
SqlConnection conn = null;
SqlDataReader reader = null;
try
{
// open connection
conn = new SqlConnection(connString);
conn.Open();
// execute the query
SqlCommand cmd = new SqlCommand(sql, conn);
reader = cmd.ExecuteReader();

// display output header
Console.WriteLine( "This program demonstrates the use of " + "the SQL Server Data Provider." );
Console.WriteLine( "Querying database {0} with query {1}\n" , conn.Database , cmd.CommandText );
Console.WriteLine("First Name\tLast Name\n");
// process the result set
Console.WriteLine("|___________|___________|");
while (reader.Read())
{
Console.WriteLine("|{0} | {1}|", reader["FirstName"].ToString().PadLeft(10), reader[1].ToString().PadLeft(10));
Console.WriteLine("|___________|___________|");

}
}
catch (Exception e)
{
Console.WriteLine("error" + e);
Console.WriteLine("Databse Not Found..." );
}
finally
{
// close connection
Console.WriteLine("closing reder and connections");
Console.ReadLine();
reader.Close();
conn.Close();
}
}
}
}


Mod Edit: Please use code tags when posting your code. Code tags are used like so => code.gif

Thanks
PsychoCoder smile.gif
User is offlineProfile CardPM

Go to the top of the page


rgfirefly24
post 29 Jun, 2008 - 03:58 PM
Post #2


D.I.C Regular

Group Icon
Joined: 7 Apr, 2008
Posts: 307



Thanked 5 times

Dream Kudos: 150
My Contributions


some things to make sure of.

You need both remote connections and named pipes enabled on your SQL server. Make sure your computer is accepting incoming connections also. then i would suggest when you set up the server to assign it a port number and use the ip:port method of connecting


I.E:

if my computers ip was 192.168.1.110 and i assigned the port number of 1177 the connection would look like so:

c#

# string connString = @"server =192.168.1.110,1177;integrated security = true;database = northwind";
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/30/08 01:27AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month