-2

I'm having some difficulty finding how to do this. I have an asp.net web app that I'm creating. I need to connect it to SQL Server to pull data from a couple of tables. I've found what I think are the correct connection strings, but I don't know where to put them so that I can use them for several pages?

I found this connection string and this example that I am pretty sure I can use.

What I need to know is where do I put this so that I can use it on 5 - 6 different pages? If someone can point me in the right direction that would be enough.

Mike
  • 1,853
  • 3
  • 45
  • 75
  • 3
    There are literally thousands of examples on the web about this. What problem are you having? – Rick S Sep 14 '17 at 15:44
  • *Any* ASP.NET tutorial is going to cover using a database. When familiarizing yourself with a technology, it's always worth the time to at least walk through some introductory tutorials. – David Sep 14 '17 at 15:48
  • @RickS Perhaps I'm not using the right search parameters. I'm not finding examples of where to put the connection string. – Mike Sep 14 '17 at 15:49
  • @David The tutorial that I used did not mention using SQL Server. – Mike Sep 14 '17 at 15:50
  • Never mind found it here: https://msdn.microsoft.com/en-us/library/ms178371(v=vs.100).aspx – Mike Sep 14 '17 at 15:51
  • 2
    @Mike: Then it sounds like you need a tutorial that does. There are a variety of resources available from the vendor: https://www.asp.net/learn Usually a connection string is kept in the `Web.config` file and the database code will either find it by convention or allow you to specify it by name. How that happens differs by database connection technologies, which you don't specify here. – David Sep 14 '17 at 15:51

1 Answers1

3

There should be a file called web.config in your Visual Studio application which is an XML file. The connection string goes in there under

<configuration>
    <ConnectionStrings>
         <add name="ConnectionStringName" providerName="System.Data.SqlClient" connectionString="Data Source=ServerName\InstanceName;Initial  Catalog=DatabaseName; Integrated Security=True; MultipleActiveResultSets=True" />
    </ConnectionStrings>
</configuration>
SE1986
  • 2,534
  • 1
  • 10
  • 29
  • If I use this method and I edit the web.config file to modify the database configuration, will the application continue to work naturally (with the new configuration) or do I need to restart the ISS or something? – Adan Sandoval Feb 16 '21 at 22:12
  • @AdanSandoval I believe it needs a restart of the IIS application, though I may be wrong. The best way would be to test it – SE1986 Feb 17 '21 at 08:41