.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > How do i set the connection string in my APP.XAML File
Ask a questionAsk a question
 

AnswerHow do i set the connection string in my APP.XAML File

  • Friday, November 06, 2009 8:11 PMEhioze Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello Guys,
    how do i set my connection string for my database in my APP.XAML file in my WPF project ?

    am using C#

    Thanks
    Ehi

Answers

  • Thursday, November 12, 2009 3:45 PMOlaf Rabbachin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi Ehioze,

    as Codingsense pointed out, it makes much more sense to store a ConnectionString - it's a per-user setting for most of all cases, hence it needs to be changeable.
    Double-click the Properties -node in your SolutionExplorer, go to the Settings Tab and enter a name in the Name column; choose (ConnectionString) as the Type , and - depending on whether your ConnectionString is to be fixed or not, choose Application or User for Scope . From your code, you can then - assuming you named your setting "myConnectionString" - obtain the setting like this:

    strConnectionString = Properties.Settings.Default.myConnectionString;
    


    Cheers,
    Olaf
    • Marked As Answer byJim Zhou - MSFTModeratorFriday, November 13, 2009 7:49 AM
    • Unmarked As Answer byEhioze Monday, November 16, 2009 10:44 PM
    • Marked As Answer byEhioze Monday, November 16, 2009 11:21 PM
    •  
  • Friday, November 13, 2009 7:48 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Ehioze,

    In addition to Olaf Rabbachin's solution, you can also utilize the Application.Current.Properties to save the connection string:
    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e5ae2b18-0795-49e8-a274-c3d52eccb8b3

    Thanks.
    Sincerely.

    Jim Zhou -MSFT
    • Marked As Answer byJim Zhou - MSFTModeratorFriday, November 13, 2009 7:49 AM
    • Unmarked As Answer byEhioze Monday, November 16, 2009 10:45 PM
    • Marked As Answer byEhioze Monday, November 16, 2009 11:22 PM
    •  

All Replies

  • Thursday, November 12, 2009 12:11 PMcodingsense Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    Hi Ehi,

    Is there any specific reason to have the connection string for your database in App.XAML file?
    You can also have it in App.Config file.

    Thanks,
    Codingsense
    • Proposed As Answer bycodingsense Friday, November 13, 2009 8:56 AM
    •  
  • Thursday, November 12, 2009 3:45 PMOlaf Rabbachin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi Ehioze,

    as Codingsense pointed out, it makes much more sense to store a ConnectionString - it's a per-user setting for most of all cases, hence it needs to be changeable.
    Double-click the Properties -node in your SolutionExplorer, go to the Settings Tab and enter a name in the Name column; choose (ConnectionString) as the Type , and - depending on whether your ConnectionString is to be fixed or not, choose Application or User for Scope . From your code, you can then - assuming you named your setting "myConnectionString" - obtain the setting like this:

    strConnectionString = Properties.Settings.Default.myConnectionString;
    


    Cheers,
    Olaf
    • Marked As Answer byJim Zhou - MSFTModeratorFriday, November 13, 2009 7:49 AM
    • Unmarked As Answer byEhioze Monday, November 16, 2009 10:44 PM
    • Marked As Answer byEhioze Monday, November 16, 2009 11:21 PM
    •  
  • Friday, November 13, 2009 7:48 AMJim Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Ehioze,

    In addition to Olaf Rabbachin's solution, you can also utilize the Application.Current.Properties to save the connection string:
    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e5ae2b18-0795-49e8-a274-c3d52eccb8b3

    Thanks.
    Sincerely.

    Jim Zhou -MSFT
    • Marked As Answer byJim Zhou - MSFTModeratorFriday, November 13, 2009 7:49 AM
    • Unmarked As Answer byEhioze Monday, November 16, 2009 10:45 PM
    • Marked As Answer byEhioze Monday, November 16, 2009 11:22 PM
    •  
  • 20 hours 12 minutes agoEhioze Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    thanks a lot guys.

    Acutally, i bought a book shortly after and it confirms what you said.

    Ehi