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. SQL Connection

SQL Connection

Scheduled Pinned Locked Moved C#
databasequestionsysadminhelp
5 Posts 3 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.
  • G Offline
    G Offline
    gonad
    wrote on last edited by
    #1

    Can anyone tell me the best way to do this? Form1 has a listview that pulls data from a sql table and displays it in the listview. From Form1, how can i call a class that would make the SQL connection and run a query that would display my data to Form1? basically, i have a lot of Forms that individually make a SQL connection, but in the event of having to change SQL servers, i dont want to have to go into all 20 of these forms to change the SQL Connection string to point to a new server. Form1 ----- private void listfromDB() { string sqlString = "select * from table"; mySQLClass getSQLdata = new mySQLClass(); getSQLdata.SQLdata(sqlString); <...code to add data from query to listview...> } Class1 ------ public giveSQLdata(string _sqlString) { string sqlCon = "workstation id=LOCALHOST;packet size=4096;...etc"; SqlConnection sqlConnect = new SqlConnection(sqlCon); SqlCommand cmd = new SqlCommand(_sqlString, sqlConnect); sqlConnect.Open(); SqlDataReader dr = cmd.ExecuteReader(); return something... } i'm new to this and need help...i freely admit this. :) thank you so much for your help. .gonad

    C 1 Reply Last reply
    0
    • G gonad

      Can anyone tell me the best way to do this? Form1 has a listview that pulls data from a sql table and displays it in the listview. From Form1, how can i call a class that would make the SQL connection and run a query that would display my data to Form1? basically, i have a lot of Forms that individually make a SQL connection, but in the event of having to change SQL servers, i dont want to have to go into all 20 of these forms to change the SQL Connection string to point to a new server. Form1 ----- private void listfromDB() { string sqlString = "select * from table"; mySQLClass getSQLdata = new mySQLClass(); getSQLdata.SQLdata(sqlString); <...code to add data from query to listview...> } Class1 ------ public giveSQLdata(string _sqlString) { string sqlCon = "workstation id=LOCALHOST;packet size=4096;...etc"; SqlConnection sqlConnect = new SqlConnection(sqlCon); SqlCommand cmd = new SqlCommand(_sqlString, sqlConnect); sqlConnect.Open(); SqlDataReader dr = cmd.ExecuteReader(); return something... } i'm new to this and need help...i freely admit this. :) thank you so much for your help. .gonad

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      A number of solutions come to mind. What I normally end up doing is adding information to the app.config or web.config that has the name of the SQL Server. Then in my .NET application (of what ever type) I create a class that I use to access the config file. This class will build a connection object for me based on the contents of the config file. The class might look something like this:

      using System;
      using System.Configuration;
      using System.Data;
      using System.Data.SqlClient;
      namespace MyNamespace
      {
      public class ConfigSettings
      {
      public static SqlConnection DatabaseConnection;
      {
      get
      {
      string server = ConfigurationSettings.AppSettings["SqlServer"];
      string database = ConfigurationSettings.AppSettings["Database"];
      string connectionString = string.Format("Server={0};Database={1};uid=???;pwd=???", server, database);
      return new SqlConnection(connectionString);
      }
      }
      }
      }

      The corresponding config file will look something like this:

      <configuration>
      <appSettings>
      <add key="SqlServer" value="ServerMachineName" />
      <add key="Database" value="Northwind" />
      </appSettings>
      </configuration>

      Does this help?


      Do you want to know more? WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums


      Upcoming talk: SELECT UserName, Password FROM Users -- Getting unauthorised access to a SQL Server, and how to prevent it.

      P G 2 Replies Last reply
      0
      • C Colin Angus Mackay

        A number of solutions come to mind. What I normally end up doing is adding information to the app.config or web.config that has the name of the SQL Server. Then in my .NET application (of what ever type) I create a class that I use to access the config file. This class will build a connection object for me based on the contents of the config file. The class might look something like this:

        using System;
        using System.Configuration;
        using System.Data;
        using System.Data.SqlClient;
        namespace MyNamespace
        {
        public class ConfigSettings
        {
        public static SqlConnection DatabaseConnection;
        {
        get
        {
        string server = ConfigurationSettings.AppSettings["SqlServer"];
        string database = ConfigurationSettings.AppSettings["Database"];
        string connectionString = string.Format("Server={0};Database={1};uid=???;pwd=???", server, database);
        return new SqlConnection(connectionString);
        }
        }
        }
        }

        The corresponding config file will look something like this:

        <configuration>
        <appSettings>
        <add key="SqlServer" value="ServerMachineName" />
        <add key="Database" value="Northwind" />
        </appSettings>
        </configuration>

        Does this help?


        Do you want to know more? WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums


        Upcoming talk: SELECT UserName, Password FROM Users -- Getting unauthorised access to a SQL Server, and how to prevent it.

        P Offline
        P Offline
        Paul Lyons
        wrote on last edited by
        #3

        Colin Angus Mackay wrote: Does this help? Well, even if it doesn't help him, It sure helped me! :) It's scary how the obvious sometimes escapes me. :doh: Thanks Colin!!

        Paul Lyons, CCPL
        Certified Code Project Lurker

        C 1 Reply Last reply
        0
        • P Paul Lyons

          Colin Angus Mackay wrote: Does this help? Well, even if it doesn't help him, It sure helped me! :) It's scary how the obvious sometimes escapes me. :doh: Thanks Colin!!

          Paul Lyons, CCPL
          Certified Code Project Lurker

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Paul Lyons wrote: It's scary how the obvious sometimes escapes me It happens to all of us sometimes.


          Do you want to know more? WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums


          Upcoming talk: SELECT UserName, Password FROM Users -- Getting unauthorised access to a SQL Server, and how to prevent it.

          1 Reply Last reply
          0
          • C Colin Angus Mackay

            A number of solutions come to mind. What I normally end up doing is adding information to the app.config or web.config that has the name of the SQL Server. Then in my .NET application (of what ever type) I create a class that I use to access the config file. This class will build a connection object for me based on the contents of the config file. The class might look something like this:

            using System;
            using System.Configuration;
            using System.Data;
            using System.Data.SqlClient;
            namespace MyNamespace
            {
            public class ConfigSettings
            {
            public static SqlConnection DatabaseConnection;
            {
            get
            {
            string server = ConfigurationSettings.AppSettings["SqlServer"];
            string database = ConfigurationSettings.AppSettings["Database"];
            string connectionString = string.Format("Server={0};Database={1};uid=???;pwd=???", server, database);
            return new SqlConnection(connectionString);
            }
            }
            }
            }

            The corresponding config file will look something like this:

            <configuration>
            <appSettings>
            <add key="SqlServer" value="ServerMachineName" />
            <add key="Database" value="Northwind" />
            </appSettings>
            </configuration>

            Does this help?


            Do you want to know more? WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums


            Upcoming talk: SELECT UserName, Password FROM Users -- Getting unauthorised access to a SQL Server, and how to prevent it.

            G Offline
            G Offline
            gonad
            wrote on last edited by
            #5

            yes! thank you very much for this. i haven't used the app.config before so this opens a lot of doors for me. thanks for the insight! gonad.

            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