SqlConnection in C#
-
In VB.NET I can use: Protected Conn As New SqlConnection(ConfigurationManager.ConnectionStrings("Active").ConnectionString) However, when I do the following in C#: protected SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings("conn")); I get the error: The name 'ConfigurationManager' does not exist in the current context Then if I change it to: protected SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("conn")); I get the error: Non-invocable member 'System.Configuration.ConfigurationManager.ConnectionStrings' cannot be used like a method. Why is this and how can I connect to my SQL Server database using C#?
-
Answer:
Try like this: protected SqlConnection conn = new SqlConnection( ConfigurationManager.ConnectionStrings["conn"].ConnectionString ); Notice the [] instead of () which is what is used to access an element of an array in C#. Also notice the usage of the .ConnectionString property call as the SqlConnection constructor expects a string.
Curt at Stack Overflow Visit the source
Other answers
Change the last pair of parentheses to square brackets. In C#, parentheses are used in method calls, whether square brackets are used to access members inside a collection (or so). In addition, use the using clause to ensure that the connection is always closed and disposed when you go out of scope: using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn"])) { ... } Read about it here: http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspx
Ofer Zelig
In C#, you read collections using square bracket syntax: e.g. string[] strings = new[] { "first", "second", "third" }; string secondString = strings[1]; So you access the Configuration collection like this: ConfigurationManager.ConnectionStrings["conn"];
Neil
Related Q & A:
- How to Convert a C++ Structure into C# Structure?Best solution by Stack Overflow
- What is the difference between C# and C#.NET?Best solution by Stack Overflow
- How to use GUI in c language?Which is best GUI for c?Best solution by Quora
- How to learn C# (moving from C++ to C#?Best solution by stackoverflow.com
- Should I make a C&C cage for my guinea pigs?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.