Problem in Connecting to a database

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 13
Reputation: nmakkena is an unknown quantity at this point 
Solved Threads: 0
nmakkena nmakkena is offline Offline
Newbie Poster

Problem in Connecting to a database

 
0
  #1
Jun 23rd, 2009
Hi all,

i am trying some code examples given in a book. I am getting an error while connecting to the database.I have installed sqlexpress edition and trying to connect to system database "master".

Below is the error message:
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"

And here is the code which i am trying.
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. // Create a connection string builder
  6. SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();
  7.  
  8. // Define connection string attributes using three techniques
  9. csb.DataSource = "(local)";
  10. csb.Add("Initial Catalog", "master");
  11. csb["Integrated Security"] = true;
  12.  
  13. // Output the connection string from the connection string builder
  14. Console.WriteLine("Connection string:\n{0}", csb.ConnectionString);
  15.  
  16. // Create a connection string from the connection string builder
  17. SqlConnection connection = new SqlConnection(csb.ConnectionString);
  18. // Open and close the connection
  19. connection.Open();
  20. Console.WriteLine("\nConnectionState = {0}", connection.State);
  21. connection.Close();
  22. Console.WriteLine("ConnectionState = {0}", connection.State);
  23.  
  24. Console.WriteLine("\nPress any key to continue.");
  25. Console.ReadKey();
  26. }
  27. }
pls help me...

Thanks
Last edited by Ancient Dragon; Jun 24th, 2009 at 9:12 am. Reason: add code tags and removed color tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Problem in Connecting to a database

 
0
  #2
Jun 23rd, 2009
Here is an example of connecting to an SQL Database, executing a query, and loading a DataTable:
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. const string connStr = "Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
  4. const string query = "Select * From Invoice Where InvNumber = @InvNumber";
  5. using (DataTable dt = new DataTable())
  6. {
  7. using (SqlConnection conn = new SqlConnection(connStr))
  8. {
  9. conn.Open();
  10. using (SqlCommand cmd = new SqlCommand(query, conn))
  11. {
  12. cmd.Parameters.Add(new SqlParameter("@InvNumber", 1100));
  13. using (SqlDataReader dr = cmd.ExecuteReader())
  14. {
  15. dt.Load(dr);
  16. }
  17. }
  18. conn.Close();
  19. }
  20. System.Diagnostics.Debugger.Break();
  21. }
  22. }

By default SQL does not enable network protocols which results in the error message you are seeing. Here is the parameters to install SQL 2005 express (the password needs to be changed, i set it as p@ssw0rd):
  1. "setup.exe" /qb ADDLOCAL=SQL_Engine,SQL_Data_Files,SQL_Replication,Client_Components,Connectivity,SQL_SSMSEE INSTANCENAME=SqlExpress SAVESYSDB=1 SQLBROWSERAUTOSTART=1 SQLAUTOSTART=1 AGTAUTOSTART=1 SECURITYMODE=SQL SAPWD=p@ssw0rd DISABLENETWORKPROTOCOLS=0 ERRORREPORTING=0 SQMREPORTING=0 ADDUSERASADMIN=1 INSTALLSQLDIR="%ProgramFiles%\Microsoft SQL Server\"
  2.  

SQL 2005 Express enable remote connections:
http://support.microsoft.com/kb/914277

SQL 2008 Express enable remote connections:
http://www.linglom.com/2009/03/28/en...-2008-express/
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 97
Reputation: VIeditorlover is an unknown quantity at this point 
Solved Threads: 5
VIeditorlover's Avatar
VIeditorlover VIeditorlover is offline Offline
Junior Poster in Training

Re: Problem in Connecting to a database

 
0
  #3
Jun 23rd, 2009
plus don't forget to check up network settings - firewalls and antivirus can do funny things
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,555
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 452
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Problem in Connecting to a database

 
0
  #4
Jun 23rd, 2009
Start SQL server instance.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC