Click here to Skip to main content
14,663,954 members
Home » Discussions » C#
   

C#

 
GeneralRe: class to open its database Pin
Richard MacCutchan21-Jul-20 0:46
mveRichard MacCutchan21-Jul-20 0:46 
GeneralRe: class to open its database Pin
ago248621-Jul-20 0:55
Memberago248621-Jul-20 0:55 
GeneralRe: class to open its database Pin
Richard MacCutchan21-Jul-20 1:13
mveRichard MacCutchan21-Jul-20 1:13 
GeneralRe: class to open its database Pin
ago248621-Jul-20 1:22
Memberago248621-Jul-20 1:22 
GeneralRe: class to open its database Pin
ago248621-Jul-20 1:32
Memberago248621-Jul-20 1:32 
AnswerRe: class to open its database Pin
OriginalGriff21-Jul-20 1:19
mveOriginalGriff21-Jul-20 1:19 
GeneralRe: class to open its database Pin
ago248621-Jul-20 1:40
Memberago248621-Jul-20 1:40 
GeneralRe: class to open its database Pin
ago248621-Jul-20 5:59
Memberago248621-Jul-20 5:59 
public class ConnectionDB
    {

        // Declaration des objets sql
        public OleDbConnection sql_con;
        public OleDbCommand sql_cmd;
        public OleDbDataAdapter DB;
        public DataSet DS = new DataSet();
        public DataTable DT = new DataTable();
        public OleDbDataReader DR;
        public string connetionString = null;

        // declaration de la methode connecter
        public void CONNECTER()
        {                           
                //connection à la base de donnée avec mot de passe
                connetionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Application.StartupPath + @"\DB_CaisseEnregistreuse.mdb;Persist Security Info=True;Jet OLEDB:Database Password=B@sta08091987";
                sql_con = new OleDbConnection(connetionString);
        }

        // declaration de la methode deconnecter
        public void DECONNECTER()
        {
            

                if (sql_con.State == ConnectionState.Open)
                {

                    sql_con.Close();
                }
            
           
        }


 public partial class Form1 : Form
   {

       public Form1()
       {
           InitializeComponent();



       }
       ConnectionDB d = new ConnectionDB();

private void LoadDB()
       {
           using (OleDbConnection sql_con = new OleDbConnection(d.connetionString))
           {
               sql_con.Open();
               using (OleDbCommand sql_cmd = d.sql_con.CreateCommand())
               {
                   string CommandText = "SELECT * FROM Detail_temp";
                   d.DB = new OleDbDataAdapter(CommandText, d.sql_con);
                   d.DS.Reset();
                   d.DB.Fill(d.DS);
                   d.DT = d.DS.Tables[0];
                   dataGridView1.DataSource = d.DT;
                   d.DECONNECTER();
               }
           }
       }


private void Form1_Load(object sender, EventArgs e)
       {

           try
           {

               LoadDB();
               d.sql_con.Open();
               TxtQteCmd.Text = "1";
               Lbl_Affich_TotalCmd.Text = "0";
               LblAfficDate.Text = DateTime.Now.ToString();

               using (OleDbCommand cmbox = d.sql_con.CreateCommand())
               {
                   cmbox.CommandText = "SELECT Code_article FROM Catalogue";
                   //d.sql_con.Open();
                   using (d.DR = cmbox.ExecuteReader())
                   {

                       try
                       {
                           while (d.DR.Read())
                           {
                               CmbRef_Produit.Items.Add(d.DR["Code_article"]).ToString();
                           }

                           //si le textbox est vide
                           if (string.IsNullOrWhiteSpace(CmbRef_Produit.Text))
                           {
                               // Message box

                               Lbl_affich_TxtQteStock.Text = "";
                               Lbl_Affich_Designation.Text = "";
                               Lbl_Affich_PrixUnitaire.Text = "";
                           }

                       }
                       catch (Exception ex)
                       {
                           MessageBox.Show(ex.ToString());

                           d.DECONNECTER();

                       }
                       d.DECONNECTER();
                   }
               }

           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }

       }


Sir I tried to do as you suggested and I would like you to take a look at my code, just see if it's good. And I would also like to know if the DISCONNECT method is worth it

QuestionCreating my own enhanced dos language Pin
Member 1270775219-Jul-20 5:30
MemberMember 1270775219-Jul-20 5:30 
AnswerRe: Creating my own enhanced dos language Pin
Dave Kreskowiak19-Jul-20 6:09
mveDave Kreskowiak19-Jul-20 6:09 
AnswerRe: Creating my own enhanced dos language Pin
Richard MacCutchan19-Jul-20 6:42
mveRichard MacCutchan19-Jul-20 6:42 
AnswerRe: Creating my own enhanced dos language Pin
Gerry Schmitz19-Jul-20 7:39
mveGerry Schmitz19-Jul-20 7:39 
AnswerRe: Creating my own enhanced dos language Pin
OriginalGriff19-Jul-20 8:20
mveOriginalGriff19-Jul-20 8:20 
AnswerRe: Creating my own enhanced dos language Pin
Member 1270775219-Jul-20 9:26
MemberMember 1270775219-Jul-20 9:26 
GeneralRe: Creating my own enhanced dos language Pin
Dave Kreskowiak19-Jul-20 10:14
mveDave Kreskowiak19-Jul-20 10:14 
GeneralRe: Creating my own enhanced dos language Pin
Member 1270775220-Jul-20 9:19
MemberMember 1270775220-Jul-20 9:19 
GeneralRe: Creating my own enhanced dos language Pin
Dave Kreskowiak20-Jul-20 12:22
mveDave Kreskowiak20-Jul-20 12:22 
GeneralRe: Creating my own enhanced dos language Pin
Member 1270775221-Jul-20 2:15
MemberMember 1270775221-Jul-20 2:15 
GeneralRe: Creating my own enhanced dos language Pin
Richard MacCutchan21-Jul-20 2:55
mveRichard MacCutchan21-Jul-20 2:55 
GeneralRe: Creating my own enhanced dos language Pin
Dave Kreskowiak21-Jul-20 4:51
mveDave Kreskowiak21-Jul-20 4:51 
GeneralRe: Creating my own enhanced dos language Pin
Member 1270775221-Jul-20 8:22
MemberMember 1270775221-Jul-20 8:22 
GeneralRe: Creating my own enhanced dos language Pin
Dave Kreskowiak21-Jul-20 8:58
mveDave Kreskowiak21-Jul-20 8:58 
GeneralRe: Creating my own enhanced dos language Pin
Richard MacCutchan21-Jul-20 10:25
mveRichard MacCutchan21-Jul-20 10:25 
QuestionHelp with a program that's made in c# - I have the source Pin
Member 1489329719-Jul-20 2:32
MemberMember 1489329719-Jul-20 2:32 
AnswerRe: Help with a program that's made in c# - I have the source Pin
Garth J Lancaster19-Jul-20 2:42
professionalGarth J Lancaster19-Jul-20 2:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.