none
Get name of current SqlDatabase connection RRS feed

  • Question

  • Hi everyone!

    I'm trying to get the name of the current database connection and display it in my statusbar (dB server name issufficient (eg, con42devtfs\sql2008))....

    I'm not sure how to do this, if anybody can please lead me in the right path or show me an example I would really appreciate it!

    thanks so much!


    Rising Storm Technologies

    Thursday, May 24, 2012 8:34 AM

Answers

  • Hi, 

    You mentioned you are working with Entitty Framework right? If you look at .edmx file you will see the line like, 

     public partial class {your class name}: ObjectContext
      {

    this class extends the ObjectContext which holds the property of Connection.

    I hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".



    Friday, May 25, 2012 7:11 AM
  • I'm trying to get the name of the current database connection and display it in my statusbar (dB server name issufficient (eg, con42devtfs\sql2008)).... I'm not sure how to do this, if anybody can please lead me in the right path or show me an example I would really appreciate it!

    You can make use of ConnectionStringBuilder class. May be the below code is what u r looking for.

    SqlConnection conn = GetConnection();
    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(conn.ConnectionString);
    string connectionName = builder.DataSource;
    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, May 29, 2012 6:04 AM
  • I see currently only replies which are done from about a partial knowledge of possible providers, this can be for every provider class different and even be different based on the way you implement them. 

    Try the dataplatform forum for the provider you use 

     http://social.msdn.microsoft.com/Forums/en-US/category/dataplatformdev

     It is surely not a question for the Base class and questions derived from that and in my opinion 100% off topic.

    This is that class

    http://social.msdn.microsoft.com/Forums/en-US/category/dataplatformdev

    for those who think it is windows forms.


    Success
    Cor

    Tuesday, May 29, 2012 9:08 AM

All replies

  • What are you using to connect to database? SqlConnection, LINQ-to-SQL, Entity Framework, etc.?

    And what kind of information do you want to show, besides the server name?


    Marco Minerva [MCPD]
    Blog: http://blogs.ugidotnet.org/marcom
    Twitter: @marcominerva

    Thursday, May 24, 2012 10:16 AM
  • Hi Marco,

    Thanks for your reply, I'm using Entity Framework (and generics)... Do you maybe have an idea on how to do this?

    Thanks!


    Rising Storm Technologies

    Thursday, May 24, 2012 10:48 AM
  • Hi, 

    You can read connection details from, 

    var con = dataContext.Connection;
    var conString = con.ConnectionString;
    I hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Thursday, May 24, 2012 10:52 AM
  • hi Kris444,

    I can't seem to get it right.... I haven't been working a long time on entity framework and not sure where to get the "dataContext" from within my application... I use a WCF Protal however, and some linq within the app as well? Is there another way?

    Thanks!


    Rising Storm Technologies

    Friday, May 25, 2012 5:53 AM
  • Hi, 

    You mentioned you are working with Entitty Framework right? If you look at .edmx file you will see the line like, 

     public partial class {your class name}: ObjectContext
      {

    this class extends the ObjectContext which holds the property of Connection.

    I hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".



    Friday, May 25, 2012 7:11 AM
  • I'm trying to get the name of the current database connection and display it in my statusbar (dB server name issufficient (eg, con42devtfs\sql2008)).... I'm not sure how to do this, if anybody can please lead me in the right path or show me an example I would really appreciate it!

    You can make use of ConnectionStringBuilder class. May be the below code is what u r looking for.

    SqlConnection conn = GetConnection();
    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(conn.ConnectionString);
    string connectionName = builder.DataSource;
    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, May 29, 2012 6:04 AM
  • I see currently only replies which are done from about a partial knowledge of possible providers, this can be for every provider class different and even be different based on the way you implement them. 

    Try the dataplatform forum for the provider you use 

     http://social.msdn.microsoft.com/Forums/en-US/category/dataplatformdev

     It is surely not a question for the Base class and questions derived from that and in my opinion 100% off topic.

    This is that class

    http://social.msdn.microsoft.com/Forums/en-US/category/dataplatformdev

    for those who think it is windows forms.


    Success
    Cor

    Tuesday, May 29, 2012 9:08 AM