Advertisement
Guest User

Codecsharp

a guest
May 2nd, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using NSDBConnect;
  16. using System.Collections.ObjectModel;
  17. using MySql.Data.MySqlClient;
  18. using HtmlAgilityPack;
  19. namespace WpfApp2
  20. {
  21. /// <summary>
  22. /// Logique d'interaction pour MainWindow.xaml
  23. /// </summary>
  24. public partial class MainWindow : Window
  25. {
  26. public ObservableCollection<Person> test { get; set; }
  27. public MainWindow()
  28.  
  29. {
  30. InitializeComponent();
  31. }
  32.  
  33. private void Button_Click(object sender, RoutedEventArgs e)
  34. {/*
  35. DBConnect db1 = new DBConnect();
  36. Console.WriteLine("Initializing");
  37.  
  38. db1.Select();
  39. */
  40. Console.ReadLine();
  41. }
  42.  
  43. private void Button_Click_1(object sender, RoutedEventArgs e)
  44. {
  45.  
  46. List<Person> persons = new List<Person>();
  47. persons.Add(new Person("Misha", "Kozlov"));
  48. persons.Add(new Person("Joe", "Black"));
  49. MyGrid.ItemsSource = persons;
  50.  
  51. }
  52.  
  53. private void Button_Click_2(object sender, RoutedEventArgs e)
  54. {
  55. string connectionString = "server=localhost;user id=root;port=3306;Pwd=;database=csharp";
  56.  
  57.  
  58. MySqlConnection connection = new MySqlConnection(connectionString);
  59. MySqlCommand cmd = new MySqlCommand();
  60. MySqlDataReader reader;
  61.  
  62. cmd.CommandText = "PS_Admin_ListeUser";
  63. cmd.CommandType = System.Data.CommandType.StoredProcedure;
  64. cmd.Connection = connection;
  65.  
  66. connection.Open();
  67.  
  68. reader = cmd.ExecuteReader();
  69.  
  70. List<Person> persons = new List<Person>();
  71.  
  72. while (reader.Read())
  73. {
  74. string UnNom = reader.GetString("Nom");
  75. string UnPrenom = reader.GetString("Prenom");
  76. int UnID = reader.GetInt32("IDUser");
  77. persons.Add(new Person(UnID, UnNom, UnPrenom));
  78. }
  79. MyGrid.ItemsSource = persons;
  80. reader.Close();
  81.  
  82. connection.Close();
  83. }
  84.  
  85. private void Button_Click_3(object sender, RoutedEventArgs e)
  86. {
  87. string connectionString = "server=SRV-WAKANDA.cloudapp.net;user id=root;port=13306;Pwd=KCX96mtkhm!;database=parking";
  88.  
  89.  
  90. MySqlConnection connection = new MySqlConnection(connectionString);
  91. MySqlCommand cmd = new MySqlCommand();
  92.  
  93. cmd.CommandText = "PS_User_Delete";
  94. cmd.CommandType = System.Data.CommandType.StoredProcedure;
  95. cmd.Parameters.AddWithValue("@aIDUser", int.Parse(TB_ID.Text));
  96. cmd.Connection = connection;
  97.  
  98.  
  99.  
  100. connection.Open();
  101. cmd.ExecuteNonQuery();
  102. connection.Close();
  103.  
  104. }
  105.  
  106. private void MyGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  107. {
  108. try
  109. {
  110. if (MyGrid.SelectedItem != null)
  111. {
  112. if (MyGrid.SelectedItem is Person)
  113. {
  114. var Ligne = (Person)MyGrid.SelectedItem;
  115.  
  116. if (Ligne != null)
  117.  
  118. {
  119. TB_ID.Text = Ligne.ID.ToString();
  120.  
  121. }
  122. }
  123. }
  124. }
  125. catch (Exception)
  126. {
  127. }
  128. }
  129.  
  130. private void Button_Click_4(object sender, RoutedEventArgs e)
  131. {
  132. try
  133. {
  134. if (MyGrid.SelectedItem != null)
  135. {
  136. if (MyGrid.SelectedItem is Person)
  137. {
  138. var Ligne = (Person)MyGrid.SelectedItem;
  139.  
  140. if (Ligne != null)
  141.  
  142. {
  143. Form1 FormDetail = new Form1(Ligne);
  144. FormDetail.Show();
  145.  
  146. }
  147. }
  148. }
  149. }
  150. catch (Exception)
  151. {
  152.  
  153. }
  154. }
  155.  
  156. private void Button_Click_5(object sender, RoutedEventArgs e)
  157. {
  158. string url = "https://www.pagesjaunes.fr/annuaire/paris-75/natixis";
  159. HtmlWeb web = new HtmlWeb();
  160. HtmlDocument doc = web.Load(url);
  161. /*var titleElement =
  162. doc.DocumentNode
  163. .Element("html")
  164. .Element("head")
  165. .Element("title");
  166. if (titleElement != null)
  167. {
  168. string title = titleElement.InnerText;
  169. Console.WriteLine("Titre: {0}", title);
  170. }
  171. else
  172. {
  173. Console.WriteLine("Pas de titre");
  174. }*/
  175. /*
  176. var body =
  177. doc.DocumentNode
  178. .Element("html")
  179. .Element("body");
  180.  
  181. var titles = body.Elements("h2");
  182. foreach (var title in titles)
  183. {
  184. Console.WriteLine(title.InnerHtml);
  185. }
  186. */
  187. //var links = doc.DocumentNode.Descendants("a");
  188. var links = doc.DocumentNode.SelectNodes("//*[@class=\"cross-links\"]//li");
  189.  
  190. foreach (var link in links)
  191. {
  192. try
  193. {
  194. String value = link.InnerText;
  195. Char delimiter = ',';
  196. String[] substrings = value.Split(delimiter);
  197. /* foreach (var substring in substrings)
  198. Console.WriteLine(substring);*/
  199. //This is my connection string i have assigned the database file address path
  200. string MyConnection2 = "datasource=personne;port=3307;username=root;password=";
  201. //This is my insert query in which i am taking input from the user through windows forms
  202. string Query = "insert into personne(nom,adresse,code) values('" + substrings[0] + "','" + substrings[1] + "','" + substrings[2] + "');";
  203. //This is MySqlConnection here i have created the object and pass my connection string.
  204. MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
  205. //This is command class which will handle the query and connection object.
  206. MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
  207. MySqlDataReader MyReader2;
  208. MyConn2.Open();
  209. MyReader2 = MyCommand2.ExecuteReader(); // Here our query will be executed and data saved into the database.
  210. MessageBox.Show("Save Data");
  211. while (MyReader2.Read())
  212. {
  213. }
  214. MyConn2.Close();
  215. }
  216. catch (Exception ex)
  217. {
  218. MessageBox.Show(ex.Message);
  219. }
  220. Console.WriteLine("ok"+link.InnerText);
  221. }
  222.  
  223. /*
  224. var table = doc.GetElementbyId("listResults");
  225. var rows = table.Element("tbody").Elements("tr");
  226. var contacts =
  227. from r in rows
  228. let values = r.Elements("td").Select(c => c.InnerText).ToArray()
  229. select new Person(int.Parse(values[2]),values[0], values[1]);
  230.  
  231. MyGrid.Columns[2].Header = "Age";
  232. MyGrid.ItemsSource = contacts;*/
  233. }
  234. }
  235.  
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement