[c#] Convertir Date

Résolu
cs_piep14 Messages postés 345 Date d'inscription lundi 15 mars 2004 Statut Membre Dernière intervention 13 mai 2011 - 12 juin 2005 à 18:51
cs_piep14 Messages postés 345 Date d'inscription lundi 15 mars 2004 Statut Membre Dernière intervention 13 mai 2011 - 15 juin 2005 à 18:03
Bonjour, dans ma bdd j'ai un string dans le format : 2005-06-11 18:55


et je souhaite le convertir en 11-06-2005 18:55 !! Mais je n'ai pas trouvé la méthode ? Merci

26 réponses

cs_piep14 Messages postés 345 Date d'inscription lundi 15 mars 2004 Statut Membre Dernière intervention 13 mai 2011
15 juin 2005 à 17:05
Résultat :



private void Principal_Load(object sender, System.EventArgs e)

{

Principal formtest = new Principal();

Rectangle rec = new Rectangle();

rec = Screen.GetWorkingArea(rec);

int x = rec.Width;

int largeur = formtest.Size.Width;

DateTime DateLancement = DateTime.Now.AddMinutes(1);


TimeSpan span = new
TimeSpan(DateLancement.Subtract(DateTime.Now).Ticks);

this.timer1.Interval = (int) span.TotalMilliseconds;

this.timer1.Enabled = true;

this.timer2.Enabled = true;

}



private void timer1_Tick(object sender, System.EventArgs e)

{


MySqlConnection connect = new
ByteFX.Data.MySqlClient.MySqlConnection("Database=carrefour;Data
Source=localhost;User Id=root;Password=");

try

{

connect.Open();

string MySQLCmd = "select * from tache";


MyAdapter.SelectCommand = new MySqlCommand(MySQLCmd,
connect);

MyAdapter.Fill(ds);



foreach ( DataRow dtRow in ds.Tables[0].Rows)

{


string dt1 =
(string)dtRow["horaire"];


string dt2 =
DateTime.Now.ToString("yyyy-MM-dd HH:mm");

timer1.Enabled = false;

if ( dt2 == dt1 )

{



vbMaf.Windows.Forms.DesktopAlert da = new
vbMaf.Windows.Forms.DesktopAlert();



da.ShowDialog();

}



else if ( dt2 != dt1)

{



}

}

connect.Close();

timer1.Enabled = true;

}



catch(MySqlException Ex)

{


MessageBox.Show("Erreur SQL:\n" + Ex.Message,
"Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);

}



}



Voila, il fallait jouer sur les timer !!!
3
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
12 juin 2005 à 19:00
Salut,

DateTime.Parse(oldDate).ToString("dd-MM-yyyy HH:mm")
devrait faire l'affaire.

Cocoricoooooooo !!!!
coq
MVP Visual C#
0
cs_piep14 Messages postés 345 Date d'inscription lundi 15 mars 2004 Statut Membre Dernière intervention 13 mai 2011
12 juin 2005 à 19:06
Par contre, est ce que tu pourrais m'aider a récupérer la date de la bdd. J'ai fais ceci pour le moment :



string dt1 = DateTime.Now.ToString("dd-MM-yyyy HH:mm");




MySqlConnection connect = new
ByteFX.Data.MySqlClient.MySqlConnection("Database=carrefour;Data
Source=localhost;User Id=root;Password=");

connect.Open();



//string dt2 = "06-06-2005 20:38";



string MySQLCmd = "select * from tache";

MySqlCommand cmd2 = new MySqlCommand(MySQLCmd, connect);

MyAdapter.Fill(ds);

dataGrid1.DataSource = ds.Tables[1];



//dataGrid1.Refresh();

//MyAdapter.ExecuteQuery();

//MessageBox.Show(ds);

DateTime.Parse(oldDate).ToString("dd-MM-yyyy HH:mm");

connect.Close();

timer1.Enabled = false;




vbMaf.Windows.Forms.DesktopAlert da = new
vbMaf.Windows.Forms.DesktopAlert();

da.ShowDialog();

}





Merci de votre aide. C'est tellement rare de voir un Admin CS être aussi sympatique :)
0
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
12 juin 2005 à 19:22
Ba je ne connais pas la base donc je ne vois pas comment je pourrais te dire comment recup la date.
Tu avais dit que tu la recupérais sous forme de chaine, dans mon exemple "oldDate" est la chaine ne question :-)

Cocoricoooooooo !!!!
coq
MVP Visual C#
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_piep14 Messages postés 345 Date d'inscription lundi 15 mars 2004 Statut Membre Dernière intervention 13 mai 2011
12 juin 2005 à 19:29
Dans ma bdd, j'ai

ID
horaire
infos periodicite

<!-- Results table body -->

----

,

1,
,
2005-06-12 18:56,
Texte,
1


Mais je n'arrive pas a récupérer la date. Merci
0
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
12 juin 2005 à 19:43
Exemple pour la premiere ligne retournée :

string oldDate = (string)ds.Tables[1].Rows[0][horaire];
string date = DateTime.Parse(oldDate).ToString("dd-MM-yyyy HH:mm");

Cocoricoooooooo !!!!
coq
MVP Visual C#
0
cs_piep14 Messages postés 345 Date d'inscription lundi 15 mars 2004 Statut Membre Dernière intervention 13 mai 2011
12 juin 2005 à 20:30
As oui, je commence a bien comprendre comment cela fonctionne.

En faite, au chargement de mon programme j'ai un timer pour analyser la
date qui est contenu dans la base de données et afficher un message si
la date du système est égale a celle de la bdd. Je fais donc :



private void Principal_Load(object sender, System.EventArgs e)

{

Principal formtest = new Principal();

Rectangle rec = new Rectangle();

rec = Screen.GetWorkingArea(rec);

int x = rec.Width;

int largeur = formtest.Size.Width;

DateTime DateLancement = DateTime.Now.AddMinutes(1);

TimeSpan span
= new TimeSpan(DateLancement.Subtract(DateTime.Now).Ticks);

this.timer1.Interval = (int) span.TotalMilliseconds;

this.timer1.Enabled = true;

this.timer2.Enabled = true;

}



private void timer1_Tick(object sender, System.EventArgs e)

{

string dt1 = DateTime.Now.ToString("dd-MM-yyyy HH:mm");


MySqlConnection connect = new
ByteFX.Data.MySqlClient.MySqlConnection("Database=carrefour;Data
Source=localhost;User Id=root;Password=");

connect.Open();

//string dt2 = "06-06-2005 20:38";

string MySQLCmd = "select * from tache";

MySqlCommand cmd2 = new MySqlCommand(MySQLCmd, connect);

MyAdapter.Fill(ds);



string oldDate = (string)ds.Tables[1].Rows[0][horaire];

string date = DateTime.Parse(oldDate).ToString("dd-MM-yyyy HH:mm");

connect.Close();

timer1.Enabled = false;




vbMaf.Windows.Forms.DesktopAlert da = new
vbMaf.Windows.Forms.DesktopAlert();

da.ShowDialog();

}





Mais cela ne fonctionne pas encore. Peut etre je ne récupere pas la date de la bdd ??? Merci de votre aide
0
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
12 juin 2005 à 22:11
"Mais cela ne fonctionne pas encore."
C'est beaucoup trop vague pour qu'on comprenne ce qui peut bien se passer.
Il y a une exception ? A quel endroit ? Message etc

Cocoricoooooooo !!!!
coq
MVP Visual C#
0
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
12 juin 2005 à 22:27
Perso, j'ai rien compris : )
0
cs_piep14 Messages postés 345 Date d'inscription lundi 15 mars 2004 Statut Membre Dernière intervention 13 mai 2011
13 juin 2005 à 11:50
Je vous explique un peu le code si vous voulez !

Au chargement de l'appz, j'ai une fonction avec un timer qui va me permettre d'aller lire dans la bdd

Ensuite, dans le timer, je fais donc le select de la date contenu dans la bdd.



Si je met en "dure" le code string dt2 = "06-06-2005 20:38"; ma fenetre
va bien se lancer. Par contre, je souhaite que cette date soit récupéré
a partir de la bdd. Donc il faut la récup et l'associer à la variable
dt2.



Premier probleme signalé :

string oldDate = (string)ds.Tables[1].Rows[0][horaire];

--> [C# Erreur] Principale.cs(266): Le nom
'horaire' n'existe pas dans la classe ni dans l'espace de noms
'Taches_planifiees.Principal'



Merci
0
cs_piep14 Messages postés 345 Date d'inscription lundi 15 mars 2004 Statut Membre Dernière intervention 13 mai 2011
13 juin 2005 à 12:53
J'arrive bien maintenant a afficher une ligne entière de la bdd en faisant :






Code:, ----
MySqlConnection connect = new
ByteFX.Data.MySqlClient.MySqlConnection("Database=carrefour;Data
Source=localhost;User Id=root;Password=");

try

{

connect.Open();

//string dt2 = "06-06-2005 20:38";

string MySQLCmd = "select * from tache";

//MySqlCommand cmd2 = new MySqlCommand(MySQLCmd, connect);

MyAdapter.SelectCommand = new MySqlCommand(MySQLCmd, connect);

MyAdapter.Fill(ds);

dataGrid1.DataSource = ds.Tables[0];

dataGrid1.Refresh();

//string oldDate = (string)ds.Tables[0].Rows[0][horaire];

//string date = DateTime.Parse(oldDate).ToString("dd-MM-yyyy HH:mm");

connect.Close();

}

catch(MySqlException Ex)

{

MessageBox.Show("Erreur
SQL:\n" + Ex.Message, "Erreur", MessageBoxButtons.OK,
MessageBoxIcon.Error);

}










Après, j'ai encore essayé ceci pour récuperer un seul champs :






Code:, ----
dataGrid1.DataSource = (string)ds.Tables[0].Rows[0][1];







Mais la :



Citation:, ----
Classe
d'exception non géréeSystem.exception avec le message 'DataBlinding
complexe accepte Ilist ou IlistSource comme source de données'







Je ne vois pas trop ce que cela signifie !! Merci
0
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
13 juin 2005 à 19:26
"
Premier probleme signalé :
string oldDate = (string)ds.Tables[1].Rows[0][horaire];
--> [C# Erreur] Principale.cs(266): Le nom 'horaire' n'existe pas dans la classe ni dans l'espace de noms 'Taches_planifiees.Principal'
"
Le message est plutôt explicite pourtant : j'ai oublier les "" autour du nom de colonne...
> string oldDate (string)ds.Tables[1].Rows[0]["horaire"];

Cocoricoooooooo !!!!
coq
MVP Visual C#
0
cs_piep14 Messages postés 345 Date d'inscription lundi 15 mars 2004 Statut Membre Dernière intervention 13 mai 2011
13 juin 2005 à 20:50
Excuse moi j'avais pas compris le message d'erreur . Par contre, j'ai donc maintenant :



private void Principal_Load(object sender, System.EventArgs e)

{

Principal formtest = new Principal();

Rectangle rec = new Rectangle();

rec = Screen.GetWorkingArea(rec);

int x = rec.Width;

int largeur = formtest.Size.Width;

DateTime DateLancement = DateTime.Now.AddMinutes(1);

TimeSpan span
= new TimeSpan(DateLancement.Subtract(DateTime.Now).Ticks);

this.timer1.Interval = (int) span.TotalMilliseconds;

this.timer1.Enabled = true;

this.timer2.Enabled = true;




MySqlConnection connect = new
ByteFX.Data.MySqlClient.MySqlConnection("Database=carrefour;Data
Source=localhost;User Id=root;Password=");

try

{

connect.Open();

string MySQLCmd = "select * from tache";


MyAdapter.SelectCommand = new MySqlCommand(MySQLCmd,
connect);

MyAdapter.Fill(ds);


string oldDate =
(string)ds.Tables[0].Rows[0]["horaire"];


string date =
DateTime.Parse(oldDate).ToString("dd-MM-yyyy HH:mm");

string dt2 = date;

connect.Close();

}



catch(MySqlException Ex)

{


MessageBox.Show("Erreur SQL:\n" + Ex.Message, "Erreur",
MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}



private void timer1_Tick(object sender, System.EventArgs e)

{

string dt1 = DateTime.Now.ToString("dd-MM-yyyy HH:mm");

//string dt2 ="13-06-2005 20:45";



timer1.Enabled = false;

if ( dt2 == dt1 )

{


vbMaf.Windows.Forms.DesktopAlert da = new
vbMaf.Windows.Forms.DesktopAlert();

da.ShowDialog();

}

}



Donc comme vous pouvez le voir, je récupere dans Load, la valeur :
string dt2 = date; mais en faite, j'en ai besoin dans mon timer1.
Comment je peux passer le valeur a cette seconde fonction ? Merci bocoup
0
cs_piep14 Messages postés 345 Date d'inscription lundi 15 mars 2004 Statut Membre Dernière intervention 13 mai 2011
13 juin 2005 à 20:56
Non, c'est bon j'ai reussi ;) !!!!!
0
cs_piep14 Messages postés 345 Date d'inscription lundi 15 mars 2004 Statut Membre Dernière intervention 13 mai 2011
13 juin 2005 à 21:05
Donc pour ce que cela intéresse, voici le code qui fonctionne :



private void Principal_Load(object sender, System.EventArgs e)

{

Principal formtest = new Principal();

Rectangle rec = new Rectangle();

rec = Screen.GetWorkingArea(rec);

int x = rec.Width;

int largeur = formtest.Size.Width;

DateTime DateLancement = DateTime.Now.AddMinutes(1);

TimeSpan span
= new TimeSpan(DateLancement.Subtract(DateTime.Now).Ticks);

this.timer1.Interval = (int) span.TotalMilliseconds;

this.timer1.Enabled = true;

this.timer2.Enabled = true;

}



private void timer1_Tick(object sender, System.EventArgs e)

{


MySqlConnection connect = new
ByteFX.Data.MySqlClient.MySqlConnection("Database=carrefour;Data
Source=localhost;User Id=root;Password=");

try

{

connect.Open();

string MySQLCmd = "select * from tache";


MyAdapter.SelectCommand = new MySqlCommand(MySQLCmd,
connect);

MyAdapter.Fill(ds);


string oldDate =
(string)ds.Tables[0].Rows[0]["horaire"];


string date =
DateTime.Parse(oldDate).ToString("dd-MM-yyyy HH:mm");

string dt2 = date;


string dt1 = DateTime.Now.ToString("dd-MM-yyyy
HH:mm");

//string dt2 ="13-06-2005 20:45";



timer1.Enabled = false;

if ( dt2 == dt1 )

{


vbMaf.Windows.Forms.DesktopAlert
da = new vbMaf.Windows.Forms.DesktopAlert();

da.ShowDialog();

}

connect.Close();

}



catch(MySqlException Ex)

{


MessageBox.Show("Erreur SQL:\n" + Ex.Message, "Erreur",
MessageBoxButtons.OK, MessageBoxIcon.Error);

}



}



Maintenant, j'ai un dernier probleme.

Ceci fonctionne car dans ma base de données, je n'ai qu'une ligne. Mais
disons, que j'ai deux taches a executer, c'est a dire, une qui se lance
à 21:00 et une a 21:05.

La premiere se lance correctement mais la seconde n'est pas executé !!!

Merci de votre patience
0
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
13 juin 2005 à 21:16
DataTable.Rows est une propriété donnant accès à la collection de lignes, à toi de la parcourir.

Cocoricoooooooo !!!!
coq
MVP Visual C#
0
cs_piep14 Messages postés 345 Date d'inscription lundi 15 mars 2004 Statut Membre Dernière intervention 13 mai 2011
13 juin 2005 à 21:56
Je viens de voir sur MSDN cette fonction. Je ne comprend pas du tout
son fonctionnement. Microsoft n'a pas choisi la simplicité dans sa
documentation . As tu un exemple plus concret qui peut m'aider a terminer ce post !!!



Merci sincerement
0
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
13 juin 2005 à 22:13
Pourtant elle est plutôt claire celle là justement lol

Exemple :

private void Principal_Load(object sender, System.EventArgs e)
{
Principal formtest = new Principal();
Rectangle rec = new Rectangle();
rec = Screen.GetWorkingArea(rec);
int x = rec.Width;
int largeur = formtest.Size.Width;
DateTime DateLancement = DateTime.Now.AddMinutes(1);
TimeSpan span = new TimeSpan(DateLancement.Subtract(DateTime.Now).Ticks);
this.timer1.Interval = (int) span.TotalMilliseconds;
this.timer1.Enabled = true;
this.timer2.Enabled = true;
}

private void timer1_Tick(object sender, System.EventArgs e)
{
MySqlConnection connect = new ByteFX.Data.MySqlClient.MySqlConnection("Database=carrefour;Data Source=localhost;User Id=root;Password=");
try
{
connect.Open();
string MySQLCmd = "select * from tache";
MyAdapter.SelectCommand = new MySqlCommand(MySQLCmd, connect);
MyAdapter.Fill(ds);

foreach ( DataRow dtRow in ds.Tables[0].Rows)
{
string oldDate = (string)dtRow["horaire"];
string date = DateTime.Parse(oldDate).ToString("dd-MM-yyyy HH:mm");
string dt2 = date;
string dt1 = DateTime.Now.ToString("dd-MM-yyyy HH:mm");
//string dt2 ="13-06-2005 20:45";

timer1.Enabled = false;
if ( dt2 == dt1 )
{
vbMaf.Windows.Forms.DesktopAlert da = new vbMaf.Windows.Forms.DesktopAlert();
da.ShowDialog();
}
}
connect.Close();
}

catch(MySqlException Ex)
{
MessageBox.Show("Erreur SQL:\n" + Ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

}

Cocoricoooooooo !!!!
coq
MVP Visual C#
0
cs_piep14 Messages postés 345 Date d'inscription lundi 15 mars 2004 Statut Membre Dernière intervention 13 mai 2011
13 juin 2005 à 22:24
Bah il y a un léger problème, plus rien ne se lance maintenant . Je ne comprend plus rien !!!
0
cs_piep14 Messages postés 345 Date d'inscription lundi 15 mars 2004 Statut Membre Dernière intervention 13 mai 2011
14 juin 2005 à 16:44
Je n'ai toujours pas trouver le moyens de faire lancer la nouvelle
fenetre maintenant que dans ma base de données j'ai plusiers
enregistrement !!!



Merci de votre aide
0
Rejoignez-nous