1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Messaging;
using System.Data.ProviderBase;
using NP6.DAL;
using System.Data.SqlClient;
using System.IO;
namespace waPutMSMQ
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Creation XML
XmlDocument trafic = new XmlDocument();
trafic.LoadXml("<visites><visite><adresse_ip>62.39.140.121</adresse_ip><date>30/12/2006</date><heure>12:15:00</heure></visite><visite><adresse_ip>62.39.140.336</adresse_ip><date>30/11/2006</date><heure>15:15:00</heure></visite></visites>");
//ENVOI XML -> MSMQ
MessageQueueTransaction oTrans;
MessageQueue oMSMQueue;
oMSMQueue = new System.Messaging.MessageQueue(@"FormatName:DIRECT=OS:SC1-compta230XP\private$\test");
oTrans = new MessageQueueTransaction();
oTrans.Begin();
oMSMQueue.Send(trafic, "Trafic_" + DateTime.Now.ToString("yyyy.MM.dd_HH.mm.ss.ff"), oTrans);
oTrans.Commit();
oMSMQueue.Close();
oMSMQueue.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
//MSMQ -> traitement
MessageQueueTransaction oTransaction;
MessageQueue oMsgQueu;
//Récupération du message
oTransaction = new MessageQueueTransaction();
oMsgQueu = new MessageQueue(@".\private$\test");
System.Messaging.Message oMessage = null;
try
{
oTransaction.Begin();
oMessage = oMsgQueu.Receive(oTransaction);
Type[] targetTypes = new Type[1];
targetTypes[0] = typeof(string);
oMessage.Formatter = new XmlMessageFormatter(targetTypes); //XmlMessageFormatter:Sérialise l'objet en Xml
XmlDocument oDOM = new XmlDocument();
oDOM.PreserveWhitespace = true;
oDOM.LoadXml((string)oMessage.Body); //On utilise la propriété Body pour récupérer l'objet encapsulé
// Chaîne de connexion
string connectString = "data source=SC1-DEVECOM;initial catalog=TempsReel;uid=callens;pwd=cultura;";
SqlConnection connection = new SqlConnection(connectString);
connection.Open();
XmlNodeList oServeurs = oDOM.SelectNodes("visites/visite");
foreach (XmlNode oNode in oServeurs)
{
SqlCommand command = new SqlCommand("INSERT INTO Visite(Date_visite,Heure_visite) values (" + oNode.SelectSingleNode("date").LastChild.Value + "," + oNode.SelectSingleNode("heure").LastChild.Value + ")", connection);
SqlDataReader reader = command.ExecuteReader();
}
connection.Close();
}
catch (XmlException Exml)
{
oTransaction.Abort();
}
catch (Exception ex)
{
oTransaction.Abort(); //Pour sortir
}
}
}
} |
Partager