Create Database Connection in the C#.net

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

Join Date: Nov 2007
Posts: 18
Reputation: AniWeb is an unknown quantity at this point 
Solved Threads: 0
AniWeb AniWeb is offline Offline
Newbie Poster

Create Database Connection in the C#.net

 
0
  #1
Feb 8th, 2008
hello all,

I have database named 'Marketing', and want to bound DropDownList control with it using 'add connection' dialog box in the C#.net.

I gave datasource: 'Microsoft SQL Server Database File (SqlClient)'. And when I give DataBase file name as 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\Marketing_Data.MDF', I get error while testing the connection:
"An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that
under default settings SQL Server dose not allow remote connections. (ProviderQL Network Interfaces, error: 26-Error Locating Server/Instance specified)".

So what DataBase File name I should give here for completing the database binding?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Create Database Connection in the C#.net

 
0
  #2
Feb 8th, 2008
If this database resides on the server, type its name and use your suitable authentication options and select target database from databases downlist.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Create Database Connection in the C#.net

 
0
  #3
Feb 9th, 2008
What Ramy is saying is that you do not specify a File name, but rather a ConnectionString.
  1. SqlConnection conn = new SqlConnection("Data Source=Servername;Initial Catalog=Marketing;Integrated Security=SSPI");
  2. conn.Open();
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Create Database Connection in the C#.net

 
0
  #4
Feb 9th, 2008
Thanks Jerry
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 1
Reputation: lesly is an unknown quantity at this point 
Solved Threads: 0
lesly lesly is offline Offline
Newbie Poster

Re: Create Database Connection in the C#.net

 
0
  #5
Aug 12th, 2009
Hi Jerry, I tried this connection but I am getting an error on "conn.Open();". Could you please help me to find out what would be the reason of this error?
following is the detail error message "Compiler Error Message: CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
"
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: Create Database Connection in the C#.net

 
0
  #6
Aug 12th, 2009
Post your code that sets up the connection string and I will take a look. The error you mention is a typical syntax issue.

// Jerry
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 26
Reputation: kool.net is an unknown quantity at this point 
Solved Threads: 0
kool.net kool.net is offline Offline
Light Poster
 
0
  #7
22 Days Ago
hi can you tell me after establish the connection , how to i use this connections in all forms
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: GAMacky is an unknown quantity at this point 
Solved Threads: 0
GAMacky GAMacky is offline Offline
Newbie Poster
 
0
  #8
13 Days Ago
You don't want to keep the connection open all the time, only when you need it. As in the matter of fact, the conn.Open(); shouldn't be in your code at all. Any sql command statement such as sqlCommand and sqlDataAdapter all can take a connection as a parameter. If you have conn declared as a global variable, then you can use that variable to pass as a connection to all the sql command statements. The statement will do the connection open and closing as necessary.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC