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. Database connection question

Database connection question

Scheduled Pinned Locked Moved C#
databasequestioncsharpwinformshelp
5 Posts 4 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.
  • T Offline
    T Offline
    tonyrdye
    wrote on last edited by
    #1

    New to C# - long time cobol programmer.... How do you define a db connection so it can be used across several windows forms? I have the connection working but I have to establish the connection in each form. iDB2Connection cn = new iDB2Connection(); cn.ConnectionString = "DataSource=" + txtiSeries.Text.Trim(' ') + ";DefaultCollection=" + txtDataLibrary.Text.Trim(' ') + "; UserID=" + txtUserId.Text.Trim(' ') + "; Password=" + txtPwd.Text.Trim(' ') + ";LibraryList=*USRLIBL;"; try { cn.Open(); } catch (iDB2CommErrorException x) I would like to be able to establish the connection 1 time when the application starts and then be able to use it across all of the windows forms. Just not sure were or how to define it. Any help would be appreciated. Thanks

    M K S 4 Replies Last reply
    0
    • T tonyrdye

      New to C# - long time cobol programmer.... How do you define a db connection so it can be used across several windows forms? I have the connection working but I have to establish the connection in each form. iDB2Connection cn = new iDB2Connection(); cn.ConnectionString = "DataSource=" + txtiSeries.Text.Trim(' ') + ";DefaultCollection=" + txtDataLibrary.Text.Trim(' ') + "; UserID=" + txtUserId.Text.Trim(' ') + "; Password=" + txtPwd.Text.Trim(' ') + ";LibraryList=*USRLIBL;"; try { cn.Open(); } catch (iDB2CommErrorException x) I would like to be able to establish the connection 1 time when the application starts and then be able to use it across all of the windows forms. Just not sure were or how to define it. Any help would be appreciated. Thanks

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      It would be a bad idea to keep the database connection open constantly, It is much better to create a connection each time you need one. I tend to use a class that does all my database work, which includes creating a connection when needed. This class provides functions for specifying search criteria. You could consider a simple class that simply takes an SQL query string and returns a datatable of the results (for search functionality) - In this case database connection string could be a static member of the class which is set when the application first loads.

      Life goes very fast. Tomorrow, today is already yesterday.

      1 Reply Last reply
      0
      • T tonyrdye

        New to C# - long time cobol programmer.... How do you define a db connection so it can be used across several windows forms? I have the connection working but I have to establish the connection in each form. iDB2Connection cn = new iDB2Connection(); cn.ConnectionString = "DataSource=" + txtiSeries.Text.Trim(' ') + ";DefaultCollection=" + txtDataLibrary.Text.Trim(' ') + "; UserID=" + txtUserId.Text.Trim(' ') + "; Password=" + txtPwd.Text.Trim(' ') + ";LibraryList=*USRLIBL;"; try { cn.Open(); } catch (iDB2CommErrorException x) I would like to be able to establish the connection 1 time when the application starts and then be able to use it across all of the windows forms. Just not sure were or how to define it. Any help would be appreciated. Thanks

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        Create a data class that handles all data calls to & from the data source. I have one if you need it. Then in your main class, mine is called csApp, create a class variable that holds it:

        public static DataClass DataAccess;

        Next, in the Main method do:

        public static void Main(String[] args)
        {
        DataAccess = new DataClass();
        }

        Then, anytime you need to call into the data source you can do:

        DataSet ds = csApp.DataAccess.GetDataSet("select * from ....")

        This way it's always available. And you should always close the connection when you're not using it.

        Everything makes sense in someone's mind

        1 Reply Last reply
        0
        • T tonyrdye

          New to C# - long time cobol programmer.... How do you define a db connection so it can be used across several windows forms? I have the connection working but I have to establish the connection in each form. iDB2Connection cn = new iDB2Connection(); cn.ConnectionString = "DataSource=" + txtiSeries.Text.Trim(' ') + ";DefaultCollection=" + txtDataLibrary.Text.Trim(' ') + "; UserID=" + txtUserId.Text.Trim(' ') + "; Password=" + txtPwd.Text.Trim(' ') + ";LibraryList=*USRLIBL;"; try { cn.Open(); } catch (iDB2CommErrorException x) I would like to be able to establish the connection 1 time when the application starts and then be able to use it across all of the windows forms. Just not sure were or how to define it. Any help would be appreciated. Thanks

          S Offline
          S Offline
          seoamitk
          wrote on last edited by
          #4

          Hi You can an app.config file to your project and write this code into it if you have done this write this code your destination form where you have to make connection. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsApplication9 { public partial class Form1 : Form { SqlConnection connection; string con; public Form1() { con = System.Configuration.ConfigurationSettings.AppSettings["Accesskey"].ToString(); connection = new SqlConnection(con); InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { connection.Open(); } } } Here this code writern in C# language you have to just convert it into VB.NET.

          1 Reply Last reply
          0
          • T tonyrdye

            New to C# - long time cobol programmer.... How do you define a db connection so it can be used across several windows forms? I have the connection working but I have to establish the connection in each form. iDB2Connection cn = new iDB2Connection(); cn.ConnectionString = "DataSource=" + txtiSeries.Text.Trim(' ') + ";DefaultCollection=" + txtDataLibrary.Text.Trim(' ') + "; UserID=" + txtUserId.Text.Trim(' ') + "; Password=" + txtPwd.Text.Trim(' ') + ";LibraryList=*USRLIBL;"; try { cn.Open(); } catch (iDB2CommErrorException x) I would like to be able to establish the connection 1 time when the application starts and then be able to use it across all of the windows forms. Just not sure were or how to define it. Any help would be appreciated. Thanks

            S Offline
            S Offline
            seoamitk
            wrote on last edited by
            #5

            hi ?xml version="1.0" encoding="utf-8" ? <configuration> <appSettings> <add key="Accesskey" value="Data source=.;Initial catalog=kirti;user id=sa;Password=./> </appSettings> </configuration> using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsApplication9 { public partial class Form1 : Form { SqlConnection connection; string con; public Form1() { con = System.Configuration.ConfigurationSettings.AppSettings["Accesskey"].ToString(); connection = new SqlConnection(con); InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { try { connection.Open(); SqlCommand commad = new SqlCommand("select *from " + textBox1.Text.ToString() + "", connection); SqlDataAdapter ad = new SqlDataAdapter(commad); DataSet ds = new DataSet(); ad.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } finally { connection.Close(); } } } } This code is writren in C# you have just convert into VB.NET.

            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