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