943,922 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 624
  • C# RSS
Jun 23rd, 2009
0

Problem in Connecting to a database

Expand Post »
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.
C# Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nmakkena is offline Offline
14 posts
since Dec 2008
Jun 23rd, 2009
0

Re: Problem in Connecting to a database

Here is an example of connecting to an SQL Database, executing a query, and loading a DataTable:
c# Syntax (Toggle Plain Text)
  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):
C# Syntax (Toggle Plain Text)
  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/
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jun 23rd, 2009
0

Re: Problem in Connecting to a database

plus don't forget to check up network settings - firewalls and antivirus can do funny things
Reputation Points: 10
Solved Threads: 9
Junior Poster
VIeditorlover is offline Offline
137 posts
since Dec 2007
Jun 23rd, 2009
0

Re: Problem in Connecting to a database

Start SQL server instance.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: returning database back to access.
Next Thread in C# Forum Timeline: How to get a URI of an On memory file.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC