Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Global database connection string in Windows Form Application

Global database connection string in Windows Form Application

Scheduled Pinned Locked Moved C#
databasehelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kyledunn
    wrote on last edited by
    #1

    Is there a way to have one connection string, in this case an OleDbConnection to an Access database, that can be accessed globally from more than one class? Is it possible to have one connection string that covers an entire application? Thanks for any help. Kyle

    P 1 Reply Last reply
    0
    • K kyledunn

      Is there a way to have one connection string, in this case an OleDbConnection to an Access database, that can be accessed globally from more than one class? Is it possible to have one connection string that covers an entire application? Thanks for any help. Kyle

      P Offline
      P Offline
      Peter Stephens
      wrote on last edited by
      #2

      I use a static method to retrieve my database connection:

      using System;
      using System.Data.SqlClient;

      namespace DbUtils {
      public class DataTools
      {
      public static String username = null;
      public static String password = null;

          public static SqlConnection GetConnection()
          {
              String connection = "database=mydb; network address=myserver; network library=dbnmpntw; "
              if(username != null)
                  connection += "user id=\\"" + username + "\\"; ";
              if(password != null)
                  connection += "password=\\"" + password + "\\"; ";
      
              return new SqlConnection(connection);
          }
      }
      

      }

      And then elsewhere in your app you just get a connection like:

      SqlConnection conn = DbUtils.DataTools.GetConnection();

      -- Peter Stephens

      K 1 Reply Last reply
      0
      • P Peter Stephens

        I use a static method to retrieve my database connection:

        using System;
        using System.Data.SqlClient;

        namespace DbUtils {
        public class DataTools
        {
        public static String username = null;
        public static String password = null;

            public static SqlConnection GetConnection()
            {
                String connection = "database=mydb; network address=myserver; network library=dbnmpntw; "
                if(username != null)
                    connection += "user id=\\"" + username + "\\"; ";
                if(password != null)
                    connection += "password=\\"" + password + "\\"; ";
        
                return new SqlConnection(connection);
            }
        }
        

        }

        And then elsewhere in your app you just get a connection like:

        SqlConnection conn = DbUtils.DataTools.GetConnection();

        -- Peter Stephens

        K Offline
        K Offline
        kyledunn
        wrote on last edited by
        #3

        That really helps me! Thank you. Kyle

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups