943,669 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 79582
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 8th, 2008
-2

Create Database Connection in the C#.net

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AniWeb is offline Offline
18 posts
since Nov 2007
Feb 8th, 2008
0

Re: Create Database Connection in the C#.net

If this database resides on the server, type its name and use your suitable authentication options and select target database from databases downlist.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Feb 9th, 2008
0

Re: Create Database Connection in the C#.net

What Ramy is saying is that you do not specify a File name, but rather a ConnectionString.
C# Syntax (Toggle Plain Text)
  1. SqlConnection conn = new SqlConnection("Data Source=Servername;Initial Catalog=Marketing;Integrated Security=SSPI");
  2. conn.Open();
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Feb 9th, 2008
0

Re: Create Database Connection in the C#.net

Thanks Jerry
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Aug 12th, 2009
0

Re: Create Database Connection in the C#.net

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
"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lesly is offline Offline
1 posts
since Aug 2009
Aug 12th, 2009
0

Re: Create Database Connection in the C#.net

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
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Nov 5th, 2009
0
Re: Create Database Connection in the C#.net
hi can you tell me after establish the connection , how to i use this connections in all forms
Reputation Points: 10
Solved Threads: 0
Light Poster
kool.net is offline Offline
28 posts
since Oct 2009
Nov 14th, 2009
0
Re: Create Database Connection in the C#.net
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
GAMacky is offline Offline
1 posts
since Nov 2009
Dec 31st, 2009
0
Re: Create Database Connection in the C#.net
Interesting that this thread is back alive after a year.
The SqlDataAdapter will automatically take care of opening and closing the connection, but the others such as SqlCommand do not. The adpater is a nice and easy to use component, but on the down side, it is a heavy class. If you need high throughput, consider using a DataReader.

It is better to keep the connection string as a global var than an SqlConnection. It is better to create new, open, use, and dispose of an SqlConnection instance because connections can be broken when you least expect it.

However, sometimes, you do need to keep a connection open for an extended period of time when building things like mass data transports. Consider a process where 100 rows per second need to be transferred from a socket into a database. Creating 100 SqlConnections per second doesn't work (for very long .

Most commercial applications use a Data Access Layer (DAL) class to manage their database requirements. This is usually a static class which makes it available to all forms and classes in the entire application. A DAL also decouples your application from a specific DBMS. If your company decides to move to a different or newer version of the DBMS , (if written correctly) all that needs to change is the DAL class which lowers the cost because there is minimal regression testing.

Well, off my soap box, have a Happy New Year
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Apr 5th, 2010
0
Re: Create Database Connection in the C#.net
sqlconnection cn=new sqlconnection(user id=sqlserver username ;password=sqlserver password;database=database name);
cn.open();
Reputation Points: 10
Solved Threads: 0
Newbie Poster
krisss is offline Offline
1 posts
since Apr 2010

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: c# datagridview display last month data only when form load
Next Thread in C# Forum Timeline: duplicate a form





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


Follow us on Twitter


© 2011 DaniWeb® LLC