Jpeh Noynay
Jpeh Noynay

Reputation: 173

Connection String in a VB.NET Application

I used to work only with asp.net websites before. I know that in ASP.NET website, the connection string is found in the web.config file.

My problem is now I have started working with VB.NET applications which needed to be interfaced to a database. How is a connection string created and where should I put it?

Thanks!

Here's the whole app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration> 
  <connectionStrings>
    <add name="dbAsthmaConnectionString" connectionString="Data Source=9300-00\SQLEXPRESS;Initial Catalog=dbStore;Persist Security Info=True;User ID=johnsmith;Password=1234" providerName="System.Data.SqlClient"/>
  </connectionStrings>

  <system.diagnostics>
    <sources>
      <!-- This section defines the logging configuration for My.Application.Log -->
      <source name="DefaultSource" switchName="DefaultSwitch">
         <listeners>
           <add name="FileLog"/>
             <!-- Uncomment the below section to write to the Application Event Log -->
             <!--<add name="EventLog"/>-->
         </listeners>
      </source>
    </sources>
    <switches>
      <add name="DefaultSwitch" value="Information" />
    </switches>
    <sharedListeners>
      <add name="FileLog"
           type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" 
           initializeData="FileLogWriter"/>
      <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
      <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
    </sharedListeners>
  </system.diagnostics>
</configuration>

Upvotes: 1

Views: 14165

Answers (4)

Jeremy Thompson
Jeremy Thompson

Reputation: 65702

The other answers tell you where to put the connection string: http://msdn.microsoft.com/en-us/library/ms254494(v=vs.80).aspx

This is the easiest way to create a Connection String.

a) Create a Text file, rename the extension from TXT to UDL, press enter.

b) Double click the UDL file and choose OLEDB Provider For SQL Server > Next > type the Database Server name > Select the database and click Test Connection.

enter image description here

c) When the Test passes, close the UDL file and open it with notepad, the bold lines are the connection string:

[oledb] ; Everything after this line is an OLE DB initstring
Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=YourDatabase;Data Source=SQLEXPRESS

Upvotes: 2

gwt
gwt

Reputation: 2423

Ok here is an axample : 1- Your app.config should look like this :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="Amlakconn" connectionString ="Data Source=KHASHAYAR-PC\SQLEXPRESS;Initial Catalog=Amlak;Integrated Security=True"/>
  </connectionStrings>
</configuration>

2- and in your code you can access the connectionsttring this way :

private string conn = ConfigurationManager.ConnectionStrings["Amlakconn"].ConnectionString;

go try It ;)

Upvotes: 3

Maciej
Maciej

Reputation: 7971

Non ASP.NET application can also use config file, it is just not named web.config. This file has exactly the same structure you know from ASP.NET including ConnectionStrings section. You can copy content from web.config and paste sections you need into app.config. In project it will show as app.config but in the bin folder it is named after the executable file name (with exe extension) plus ".config".

To create this file, go to project's Add -> New Item and select Application Configuration File. To get access to your connection strings create/copy <ConnectionString> section into <configuration> section and then use this code:

string conStr = ConfigurationManager.ConnectionStrings["ConStringName"].ConnectionString;
SqlDataAdapter adapter = new SqlDataAdapter("Select * from Users", conStr);

You need to add reference to `System.Configuration' assembly.

Upvotes: 0

gwt
gwt

Reputation: 2423

If you are working on a webapp project It's the same ! you can put that in the web.config file , and if you project is win app you can add an app.config file to your project and pu the connection string in there !

Upvotes: 0

Related Questions