How to configure the Connection String in App.config?

Asked

Viewed 1,605 times

1

I would like to mount using saved String: InstanciaSQLServer, UserSQL and PWSQL, it is possible that?

Example of my app.config

<configSections>


    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <-section name="BancoDeHoras.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
</configSections>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>    
<userSettings>
    <BancoDeHoras.Properties.Settings>
        <setting name="InstanciaSQLServer" serializeAs="String">
            <value />
        </setting>
        <setting name="UserSQL" serializeAs="String">
            <value />
        </setting>
        <setting name="PWSQL" serializeAs="String">
            <value />
        </setting>
    </BancoDeHoras.Properties.Settings>
</userSettings>
<connectionStrings>
  <add
    name="conection"
    connectionString ="Data Source =[InstanciaSQLServer]; Initial Catalog = BancoDeHoras; User id = [UserSQL]; pwd=[PWSQL]"/>
</connectionStrings>  

  • 1

    Friend, welcome to the community, I suggest, read our FAQ, how to ask a question: https://answall.com/help/how-to-ask , because your question is not clear, and so it is difficult to help

  • @Ronivaldoroner: As complement: The Connection Strings Reference - https://www.connectionstrings.com/

  • https://social.msdn.microsoft.com/Forums/vstudio/en-US/e5ae2b18-0795-49e8-a274-c3d52eccb8b3/connection-string-in-a-wpf-application?forum=wpf

  • What I would like to do is use the values saved in the strings to mount the connection string. Example: string connection_BD = Data Source = [Instantiatsqlserver; Initial Catalog = Bancodehours; User id = [Usersql]; pwd=[PWSQL];

  • I fixed the example code, which is the correct way to write this connectionString?

  • I solved the problem in another way. I did not create the Connection String in the XML app.Config, I created it directly in the class responsible for making the connection. Thus my string was as follows: private Static string conexao_DB = $@"Data Source = {Properties.settings.Default.Instantiatsqlserver}; Initial Catalog = Bancodehoras; User id = {Properties.settings.Default.Usersql}; pwd={Properties.settings.Default.PWSQL}"; ---- I don’t know if this is the right way to go, but it’s the only one I’ve managed so far.

  • Is using EntityFramework?

Show 2 more comments

2 answers

2

Your code

<connectionStrings>
  <add
    name="conection"
    connectionString ="Data Source =[InstanciaSQLServer]; Initial Catalog = BancoDeHoras; User id = [UserSQL]; pwd=[PWSQL]"/>
</connectionStrings>  

Example, let’s say that we have an instance of SQLEXPRESS that contains a database name TESTE with user: sa , password: 123456

Your SQLEXPRESS instance matches your local or remote server.

It’ll stay that way:

<connectionStrings>
  <add
    name="conection"
    connectionString ="Data Source =SQLEXPRESS ; Initial Catalog = TESTE; User id = SA; pwd=123456"/>
</connectionStrings> 

Your code is right, just missed vc change the fields. A great site for you to check the types of existing connections https://www.connectionstrings.com/sql-server-2014/

  • That was the question, I can not enter Data Source, User and direct password in the code because this may vary according to each installation. I did, saving it on property. so the user will only inform this in the first access to the system, inserting User Instance and Password of the SQL instance.

0

I solved the problem in another way. I did not create the Connection String on XML app.Config, created it directly in the class responsible for making the connection. That way my string looked like this:

private static string conexao_DB = $@"Data Source = {Properties.Settings.Default.InstanciaSQLSERVER}; 
Initial Catalog = BancoDeHoras; 
User id = {Properties.Settings.Default.UserSQL}; 
pwd={Properties.Settings.Default.PWSQL}"; 

I don’t know if it’s the right way to do it, but it’s the only one I’ve managed so far.

  • 1

    There is a very easy way to create your connectio string, after that just open the tag <connectionstrin>. You start by creating a txt file and save it. After that, you trace the file with extension . UDL (Universal Data Link). Try

Browser other questions tagged

You are not signed in. Login or sign up in order to post.