Advertisement
Guest User

Nem mondom meg!

a guest
Jun 9th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5.  
  6. namespace Adatbazis
  7. {
  8. static class Program
  9. {
  10. /// <summary>
  11. /// The main entry point for the application.
  12. /// </summary>
  13. [STAThread]
  14. static void Main()
  15. {
  16. Application.EnableVisualStyles();
  17. Application.SetCompatibleTextRenderingDefault(false);
  18. Application.Run(new frmBejelentkezes());
  19. }
  20. }
  21. }
  22. //2.
  23.  
  24.  
  25. using System;
  26. using System.Collections.Generic;
  27. using System.ComponentModel;
  28. using System.Data;
  29. using System.Drawing;
  30. using System.Linq;
  31. using System.Text;
  32. using System.Windows.Forms;
  33.  
  34. namespace Adatbazis
  35. {
  36. public partial class frmFoAblak : Form
  37. {
  38. private string user;
  39. private int restr;
  40. private string connection;
  41.  
  42. public string Connection
  43. {
  44. get { return connection; }
  45. set { connection = value; }
  46. }
  47.  
  48. public int Restr
  49. {
  50. get { return restr; }
  51. set { restr = value; }
  52. }
  53.  
  54. public string User
  55. {
  56. get { return user; }
  57. set { user = value; }
  58. }
  59.  
  60. public frmFoAblak()
  61. {
  62. InitializeComponent();
  63. }
  64.  
  65. private void btnExit_Click(object sender, EventArgs e)
  66. {
  67. Application.Exit();
  68. }
  69.  
  70. private void frmFoAblak_FormClosing(object sender, FormClosingEventArgs e)
  71. {
  72. Application.Exit();
  73. }
  74.  
  75. private void frmFoAblak_Shown(object sender, EventArgs e)
  76. {
  77. this.Text = "Nyilvántartás - " + User +
  78. " a bejelntkezett felhasználó";
  79.  
  80. // ha csak sima felhasználó
  81. if (Restr == 2) {
  82. menuEgyesevel.Enabled = false;
  83. importálásToolStripMenuItem.Enabled = false;
  84. }
  85. }
  86.  
  87. private void menuLista_Click(object sender, EventArgs e)
  88. {
  89. frmLista fLista = new frmLista();
  90. fLista.Connection = this.Connection;
  91. fLista.ShowDialog();
  92. }
  93.  
  94. private void menuEgyesevel_Click(object sender, EventArgs e)
  95. {
  96. frmEgyes fEgyes = new frmEgyes();
  97. fEgyes.Connection = this.Connection;
  98. fEgyes.ShowDialog();
  99. }
  100.  
  101. private void exportálásToolStripMenuItem_Click(object sender, EventArgs e)
  102. {
  103. frmExport fExport = new frmExport();
  104. fExport.Connection = this.Connection;
  105. fExport.ShowDialog();
  106. }
  107.  
  108. private void importálásToolStripMenuItem_Click(object sender, EventArgs e)
  109. {
  110. frmImport fImport = new frmImport();
  111. fImport.Connection = this.Connection;
  112. fImport.ShowDialog();
  113. }
  114. }
  115. }
  116.  
  117.  
  118. //3.
  119.  
  120. using System;
  121. using System.Collections.Generic;
  122. using System.ComponentModel;
  123. using System.Data;
  124. using System.Drawing;
  125. using System.Linq;
  126. using System.Text;
  127. using System.Windows.Forms;
  128. using MySql.Data.MySqlClient;
  129.  
  130. namespace Adatbazis
  131. {
  132. public partial class frmBejelentkezes : Form
  133. {
  134. private string server = "localhost";
  135. private string uid = "root";
  136. private string password = "";
  137. private string database = "proba";
  138. private string connString = string.Empty;
  139.  
  140. private MySqlConnection conn = null;
  141. private MySqlDataReader dr = null;
  142.  
  143. public frmBejelentkezes()
  144. {
  145. InitializeComponent();
  146. }
  147.  
  148. private void tbUser_KeyPress(object sender, KeyPressEventArgs e)
  149. {
  150. if (e.KeyChar == (char) 13 )
  151. {
  152. tbPassword.Focus();
  153. }
  154. }
  155.  
  156. private void tbPassword_KeyPress(object sender, KeyPressEventArgs e)
  157. {
  158. if (e.KeyChar == (char) 13)
  159. {
  160. btnLogin.PerformClick();
  161. }
  162. }
  163.  
  164. private void btnCancel_Click(object sender, EventArgs e)
  165. {
  166. Application.Exit();
  167. }
  168.  
  169. private void frmBejelentkezes_FormClosing(object sender, FormClosingEventArgs e)
  170. {
  171. Application.Exit();
  172. }
  173.  
  174. private void frmBejelentkezes_Shown(object sender, EventArgs e)
  175. {
  176. tbUser.Focus();
  177. }
  178.  
  179. private void btnLogin_Click(object sender, EventArgs e)
  180. {
  181. if (string.IsNullOrEmpty(tbUser.Text))
  182. {
  183. MessageBox.Show("Felhasználó nevet meg kell adni!",
  184. "Hiba", MessageBoxButtons.OK,
  185. MessageBoxIcon.Error);
  186. tbUser.Focus();
  187. return;
  188. }
  189.  
  190. try
  191. {
  192. connString = string.Format(
  193. "server={0};uid={1};password={2};database={3};",
  194. server, uid, password, database
  195. );
  196. conn = new MySqlConnection(connString);
  197. conn.Open();
  198.  
  199. string select = "select * from users where user=\'" +
  200. tbUser.Text + "\' and password=\'" + tbPassword.Text + "\'";
  201. MySqlCommand cmd = new MySqlCommand(select, conn);
  202. dr = cmd.ExecuteReader();
  203. int rows = 0;
  204. while (dr.Read())
  205. {
  206. rows++;
  207. }
  208. if (rows > 0)
  209. {
  210. //MessageBox.Show("Sikeres bejelentkezése!");
  211. MessageBox.Show("Sikeres belépés!", "Információ",
  212. MessageBoxButtons.OK, MessageBoxIcon.Information);
  213. frmFoAblak f = new frmFoAblak();
  214. f.User = tbUser.Text;
  215. f.Connection = connString;
  216. if (dr.GetInt32(3) == 1)
  217. {
  218. // admin
  219. f.Restr = 1;
  220. //MessageBox.Show("Admin");
  221. f.BackColor = Color.Red;
  222. }
  223. else
  224. {
  225. // user
  226. f.Restr = 2;
  227. //MessageBox.Show("User");
  228.  
  229. }
  230.  
  231. f.Show();
  232.  
  233. this.Hide();
  234. }
  235. else
  236. {
  237. MessageBox.Show("Sikertelen belépés!", "Hiba",
  238. MessageBoxButtons.OK, MessageBoxIcon.Error);
  239. }
  240. }
  241. catch(MySqlException ex)
  242. {
  243. MessageBox.Show(ex.Message.ToString(), "Hiba",
  244. MessageBoxButtons.OK, MessageBoxIcon.Error);
  245. }
  246. finally
  247. {
  248. if (dr != null)
  249. {
  250. dr.Close();
  251. }
  252.  
  253. if (conn != null)
  254. {
  255. conn.Close();
  256. }
  257. }
  258.  
  259. }
  260. }
  261. }
  262.  
  263.  
  264. //4.
  265.  
  266. using System;
  267. using System.Collections.Generic;
  268. using System.ComponentModel;
  269. using System.Data;
  270. using System.Drawing;
  271. using System.Linq;
  272. using System.Text;
  273. using System.Windows.Forms;
  274. using MySql.Data.MySqlClient;
  275.  
  276. namespace Adatbazis
  277. {
  278. public partial class frmEgyes : Form
  279. {
  280. private MySqlConnection conn = null;
  281. private MySqlDataAdapter da = null;
  282. private MySqlCommand mcd = null;
  283. private DataSet ds = null;
  284. private DataTable dt = new DataTable();
  285. private int i = 0;
  286. private string connection;
  287.  
  288. public string Connection
  289. {
  290. get { return connection; }
  291. set { connection = value; }
  292. }
  293.  
  294. public frmEgyes()
  295. {
  296. InitializeComponent();
  297. }
  298.  
  299. public void Zarol()
  300. {
  301. btnModosit.Enabled = false;
  302. btnSzerkeszt.Enabled = false;
  303. btnTorol.Enabled = false;
  304. btnFel.Enabled = false;
  305. }
  306.  
  307. public void labelNullazas()
  308. {
  309. tbVezeteknev.Text = "";
  310. tbKeresztnev.Text = "";
  311. tbTelefon.Text = "";
  312. tbEmail.Text = "";
  313. tbID.Text = "";
  314. }
  315.  
  316. public void modMegnyit()
  317. {
  318. if (tbVezeteknev.ReadOnly == true)
  319. {
  320. tbVezeteknev.ReadOnly = false;
  321. tbKeresztnev.ReadOnly = false;
  322. tbTelefon.ReadOnly = false;
  323. tbEmail.ReadOnly = false;
  324.  
  325. btnFirst.Enabled = false;
  326. btnLast.Enabled = false;
  327. btnRev.Enabled = false;
  328. btnFor.Enabled = false;
  329.  
  330. btnModosit.Enabled = true;
  331. btnTorol.Enabled = true;
  332. btnFel.Enabled = true;
  333. btnLabelUres.Enabled = true;
  334. }
  335. }
  336.  
  337. public void modBezar()
  338. {
  339. if (tbVezeteknev.ReadOnly == false)
  340. {
  341. tbVezeteknev.ReadOnly = true;
  342. tbKeresztnev.ReadOnly = true;
  343. tbTelefon.ReadOnly = true;
  344. tbEmail.ReadOnly = true;
  345.  
  346. btnFirst.Enabled = true;
  347. btnLast.Enabled = true;
  348. btnRev.Enabled = true;
  349. btnFor.Enabled = true;
  350.  
  351. btnModosit.Enabled = false;
  352. btnTorol.Enabled = false;
  353. btnFel.Enabled = false;
  354. btnLabelUres.Enabled = false;
  355. }
  356. }
  357.  
  358. public void adatFrissit()
  359. {
  360. string select = "select * from names";
  361.  
  362. ds = new DataSet();
  363. dt.Clear();
  364. da = new MySqlDataAdapter(select, conn);
  365. da.Fill(dt);
  366. }
  367.  
  368. private void frmEgyes_Shown(object sender, EventArgs e)
  369. {
  370. conn = new MySqlConnection(this.Connection);
  371. try
  372. {
  373. string select = "select * from names";
  374. conn.Open();
  375. ds = new DataSet();
  376. da = new MySqlDataAdapter(select, conn);
  377. da.Fill(dt);
  378. // hibakezelés ha nincs benne adat
  379. ShowData();
  380.  
  381. if (tbVezeteknev.Text == "")
  382. {
  383. Zarol();
  384. }
  385. }
  386. catch (Exception)
  387. {
  388.  
  389. throw;
  390. }
  391. btnModosit.Enabled = false;
  392. btnTorol.Enabled = false;
  393. btnFel.Enabled = false;
  394. btnLabelUres.Enabled = false;
  395. }
  396.  
  397. private void btnVisszalepes_Click(object sender, EventArgs e)
  398. {
  399. this.Close();
  400. }
  401.  
  402. public void ShowData()
  403. {
  404. tbID.Text = dt.Rows[i][0].ToString();
  405. tbVezeteknev.Text = dt.Rows[i][1].ToString();
  406. tbKeresztnev.Text = dt.Rows[i][2].ToString();
  407. tbEmail.Text = dt.Rows[i][4].ToString();
  408. tbTelefon.Text = dt.Rows[i][3].ToString();
  409. }
  410.  
  411. private void btnFirst_Click(object sender, EventArgs e)
  412. {
  413. i = 0;
  414. ShowData();
  415. }
  416.  
  417. private void btnRev_Click(object sender, EventArgs e)
  418. {
  419. if (i != 0)
  420. {
  421. i--;
  422. ShowData();
  423. }
  424. }
  425.  
  426. private void btnFor_Click(object sender, EventArgs e)
  427. {
  428.  
  429. if (i < dt.Rows.Count - 1)
  430. {
  431. i++;
  432. ShowData();
  433. }
  434. else
  435. {
  436. MessageBox.Show("Vége");
  437. }
  438. }
  439.  
  440. private void btnLast_Click(object sender, EventArgs e)
  441. {
  442. i = dt.Rows.Count - 1;
  443. ShowData();
  444. }
  445.  
  446. private void btnSzerkeszt_Click(object sender, EventArgs e)
  447. {
  448. modMegnyit();
  449. }
  450.  
  451. private void btnModosit_Click(object sender, EventArgs e)
  452. {
  453. string parancs = "update names set veznev='" + tbVezeteknev.Text + "', kernev='" + tbKeresztnev.Text + "', telefon='" + tbTelefon.Text + "', email='" + tbEmail.Text + "' where id=" + tbID.Text + "";
  454. mcd = new MySqlCommand(parancs, conn);
  455. if (mcd.ExecuteNonQuery() == 1)
  456. {
  457. MessageBox.Show("Siker");
  458. adatFrissit();
  459. modBezar();
  460. }
  461. }
  462.  
  463. private void btnTorol_Click(object sender, EventArgs e)
  464. {
  465. string parancs = "delete from names where id=" + tbID.Text + "";
  466. mcd = new MySqlCommand(parancs, conn);
  467. if (mcd.ExecuteNonQuery() == 1)
  468. {
  469. MessageBox.Show("Siker");
  470. labelNullazas();
  471. adatFrissit();
  472. modBezar();
  473. }
  474. }
  475.  
  476. private void btnFel_Click(object sender, EventArgs e)
  477. {
  478. string parancs = "insert into `names` (`veznev`,`kernev`,`telefon`,`email`) values ('" + tbVezeteknev.Text + "','" + tbKeresztnev.Text + "','" + tbTelefon.Text + "','" + tbEmail.Text + "')";
  479. mcd = new MySqlCommand(parancs, conn);
  480. if (mcd.ExecuteNonQuery() == 1)
  481. {
  482. MessageBox.Show("Siker");
  483. labelNullazas();
  484. adatFrissit();
  485. modBezar();
  486. }
  487. }
  488.  
  489. private void btnLabelUres_Click(object sender, EventArgs e)
  490. {
  491. labelNullazas();
  492. }
  493.  
  494. private void btnMegse_Click(object sender, EventArgs e)
  495. {
  496. adatFrissit();
  497. modBezar();
  498. }
  499. }
  500. }
  501.  
  502. //5.
  503.  
  504. using System;
  505. using System.Collections.Generic;
  506. using System.ComponentModel;
  507. using System.Data;
  508. using System.Drawing;
  509. using System.Linq;
  510. using System.Text;
  511. using System.Windows.Forms;
  512. using MySql.Data.MySqlClient;
  513. using System.IO;
  514.  
  515. namespace Adatbazis
  516. {
  517. public partial class frmExport : Form
  518. {
  519. private MySqlConnection conn = null;
  520. private MySqlDataAdapter da = null;
  521. private MySqlCommand mcd = null;
  522. private DataSet ds = null;
  523. private DataTable dt = new DataTable();
  524. private int i = 0;
  525. private string connection;
  526. private FileStream file;
  527. private StreamWriter fileKi;
  528.  
  529. public string Connection
  530. {
  531. get { return connection; }
  532. set { connection = value; }
  533. }
  534.  
  535. public frmExport()
  536. {
  537. InitializeComponent();
  538. }
  539.  
  540. private void frmExport_Shown(object sender, EventArgs e)
  541. {
  542.  
  543. }
  544.  
  545. private void btnKilep_Click(object sender, EventArgs e)
  546. {
  547. this.Close();
  548. }
  549.  
  550. private void btnExport_Click(object sender, EventArgs e)
  551. {
  552. try
  553. {
  554. if (saveDialog.ShowDialog() == DialogResult.OK)
  555. {
  556. // adatbázis rész
  557. conn = new MySqlConnection(this.Connection);
  558.  
  559. string select = "select * from names";
  560. conn.Open();
  561. ds = new DataSet();
  562. da = new MySqlDataAdapter(select, conn);
  563. da.Fill(dt);
  564.  
  565. // file rész
  566. file = new FileStream(saveDialog.FileName, FileMode.Create);
  567. fileKi = new StreamWriter(file);
  568.  
  569. for (int i = 0; i < dt.Rows.Count; i++)
  570. {
  571. string adat = "";
  572.  
  573. for (int j = 0; j < 5; j++)
  574. {
  575. if (j != 4)
  576. {
  577. adat += dt.Rows[i][j].ToString() + ",";
  578. }
  579. else
  580. {
  581. adat += dt.Rows[i][j].ToString();
  582. }
  583. }
  584. fileKi.WriteLine(adat);
  585. }
  586. }
  587. }
  588. catch (Exception ex)
  589. {
  590. MessageBox.Show(ex.Message);
  591. }
  592. finally
  593. {
  594. if (fileKi != null)
  595. {
  596. fileKi.Close();
  597. file.Close();
  598. MessageBox.Show("Bezárva!");
  599. }
  600. }
  601. }
  602. }
  603. }
  604.  
  605. //6.
  606.  
  607. using System;
  608. using System.Collections.Generic;
  609. using System.ComponentModel;
  610. using System.Data;
  611. using System.Drawing;
  612. using System.Linq;
  613. using System.Text;
  614. using System.Windows.Forms;
  615. using MySql.Data.MySqlClient;
  616.  
  617. namespace Adatbazis
  618. {
  619. public partial class frmLista : Form
  620. {
  621. private MySqlConnection conn = null;
  622. private MySqlDataAdapter da = null;
  623. private DataSet ds = null;
  624.  
  625. private string connection;
  626.  
  627. public string Connection
  628. {
  629. get { return connection; }
  630. set { connection = value; }
  631. }
  632.  
  633. public frmLista()
  634. {
  635. InitializeComponent();
  636. }
  637.  
  638. private void frmLista_FormClosing(object sender, FormClosingEventArgs e)
  639. {
  640.  
  641. }
  642.  
  643. private void btnClose_Click(object sender, EventArgs e)
  644. {
  645. this.Close();
  646. }
  647.  
  648. private void frmLista_Shown(object sender, EventArgs e)
  649. {
  650. conn = new MySqlConnection(this.Connection);
  651. try
  652. {
  653. string select = "select veznev, kernev, telefon, email from names";
  654. conn.Open();
  655. ds = new DataSet();
  656. da = new MySqlDataAdapter(select, conn);
  657. da.Fill(ds, "names");
  658. dgvAdatok.DataSource = ds.Tables["names"];
  659. }
  660. catch (Exception)
  661. {
  662.  
  663. throw;
  664. }
  665. }
  666. }
  667. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement