Click here to Skip to main content
15,885,138 members
Home / Discussions / C#
   

C#

 
AnswerRe: What's the most easy-to-read way of initializing a string-int-pair array? Pin
Richard Deeming23-Jul-20 22:39
mveRichard Deeming23-Jul-20 22:39 
GeneralRe: What's the most easy-to-read way of initializing a string-int-pair array? Pin
jsc4224-Jul-20 3:17
professionaljsc4224-Jul-20 3:17 
AnswerRe: What's the most easy-to-read way of initializing a string-int-pair array? Pin
Gerry Schmitz25-Jul-20 7:30
mveGerry Schmitz25-Jul-20 7:30 
AnswerRe: What's the most easy-to-read way of initializing a string-int-pair array? Pin
BillWoodruff27-Jul-20 17:25
professionalBillWoodruff27-Jul-20 17:25 
QuestionHow to convert C++ 2005 project to 2016 Pin
Sheryl Brock23-Jul-20 4:14
Sheryl Brock23-Jul-20 4:14 
AnswerRe: How to convert C++ 2005 project to 2016 Pin
OriginalGriff23-Jul-20 4:18
mveOriginalGriff23-Jul-20 4:18 
SuggestionRe: How to convert C++ 2005 project to 2016 Pin
Richard Deeming23-Jul-20 6:49
mveRichard Deeming23-Jul-20 6:49 
Questionclass to open its database Pin
ago248621-Jul-20 0:17
ago248621-Jul-20 0:17 
C#
public class ConnectionDB
    {

        // Declaration des objets sql
        public OleDbConnection sql_con;
     
        // declaration de la methode connecter
        public void CONNECTER()
        {
            try
            {
                string connetionString = null;
                //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);
                sql_con.Open();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erreur de connexion à la base donnée" + ex.ToString());
            }

            
        }

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

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

                    sql_con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erreur de deconnexion à la base donnée" + ex.ToString());
            }
        }






C#
public partial class Form1 : Form
    { 

        public Form1()
        {
            InitializeComponent();

        }

 ConnectionDB d = new ConnectionDB();

    using (OleDbCommand cmbox = d.sql_con.CreateCommand())
                {
                    cmbox.CommandText = "SELECT Code_article FROM Catalogue";
                    d.sql_con.Open();
                    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.sql_con.Close();

                    }
                }

            }

}



Quote:
I created a ConnectionDB class and 2 CONNECT and DISCONNECT methods.
I needed to open the database so I called the CONNECT method but I have an error telling me that my database is still closed so I change to sql-con.open and it works.
So my question is: Why and what is the difference.

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
ago248621-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
ago248621-Jul-20 1:22 
GeneralRe: class to open its database Pin
ago248621-Jul-20 1:32
ago248621-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
ago248621-Jul-20 1:40 
GeneralRe: class to open its database Pin
ago248621-Jul-20 5:59
ago248621-Jul-20 5:59 
QuestionCreating my own enhanced dos language Pin
Member 1270775219-Jul-20 5:30
Member 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
Member 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
Member 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 

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.