| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Social distancing? Try a better way to work remotely on your online files. Dokkio, a new product from PBworks, can help your team find, organize, and collaborate on your Drive, Gmail, Dropbox, Box, and Slack files. Sign up for free.

View
 

Lecture - How To - Read Connection String from Web Config

Page history last edited by Dr. Ron Eaglin 5 years, 10 months ago

Read Connection String from Web Config

 

Prerequisites

 

Knowledge of C#. This uses Visual Studio and SQL Server.

 

Summary

 

I create a connection string stored in the web.config and demonstrate how to access it with the .NET objects in code. Terminology and classes used for this are covered in this example.

 

 

Video 

 

 

http://online1.daytonastate.edu/player2.php?id=73f490f3f868edbcd80b5d3f7cedc403 

 

Reference Materials

 

The DatabaseHelper

 

namespace MyNewWebsite
{
    public class DatabaseHelper
    {
        public static string connectionString()
        {
            System.Configuration.Configuration rootWeb = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyNewWebsite");
            System.Configuration.ConnectionStringSettings connstr;
            if (rootWeb.ConnectionStrings.ConnectionStrings.Count > 0)
            {
                connstr = rootWeb.ConnectionStrings.ConnectionStrings["TraumaDataConnectionString"];
                return connstr.ConnectionString;
            }
            return String.Empty;
        }
    }
} 
namespace MyNewWebsite
{
    public partial class DatabaseConnection : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Text = DatabaseHelper.connectionString();
        }
    }
} 

 

Additional Information

 

Other lectures that cover the topic of connection strings

 

ADONET Object Model - Creating a Database Connection

 

Creating-a-Database-Connection-String

 

 

 

COP 4834 Lectures Page

Comments (0)

You don't have permission to comment on this page.