Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
2.00/5 (8 votes)
See more:
SqlConnection constr= new SqlConnection(@"Data Source=.\SQLEXPRESS; 
                         Integrated Security=True ; Database=abc");
  
string conn = constr.ToString();
              
return conn;


In the above code,I am unable to convert constr to string type.

Please suggest me as soon as possible.


[Edited]Code is wrapped in "pre" tag[/Edited]
Posted
Updated 23-Dec-10 19:12pm
v3

Simpy do
constr.ConnectionString.Tostring()


It's just waste of time to assigning and then fetching back.

Instead do the following.

String cs = //Yours connection string


SqlConnection Con = new SqlConnection(cs);


That will be preferable I think.
 
Share this answer
 
v2
The SqlConnection object is not needed to be converted to string. I guess, the phrase "Connection String" confused you to try to convert it to string. You don't need to do that.

Once you obtain the SqlConnection object and open the connection, you can execute commands using the SqlConnection object.

See Beginners guide to accessing SQL Server through C#[^] learn more about using SqlConnection and ADO.NET
 
Share this answer
 
C#
public string Connect()
    {
        return System.Configuration.ConfigurationManager.AppSettings["conn"];
    }
 
Share this answer
 
What do you want to achieve after converting constr to string. But if you still want then, you can write your own extension method or override the ToString method that will do the things that you require.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900