Form1.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.SQLiteClient;
  9. namespace TestApp
  10. {
  11. public partial class Form1 : Form
  12. {
  13. SQLiteConnection _Connection;
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void AddOrder(SQLiteTransaction trans, int id, int items, string customer, double amount)
  19. {
  20. SQLiteCommand cmd = _Connection.CreateCommand("insert into Orders (Id, Items, Customer, Amount) values (@Id, @Items, @Customer, @Amount)");
  21. cmd.Transaction = trans;
  22. cmd.Parameters.Add("@Id", DbType.Int32).Value = id;
  23. cmd.Parameters.Add("@Items", DbType.Int32).Value = items;
  24. cmd.Parameters.Add("@Customer", DbType.String).Value = customer;
  25. cmd.Parameters.Add("@Amount", DbType.Single).Value = amount;
  26. cmd.ExecuteNonQuery();
  27. }
  28. private void AddOrderDetail(SQLiteTransaction trans, int id, int orderId, string article)
  29. {
  30. SQLiteCommand cmd = _Connection.CreateCommand("insert into OrderDetails (Id, OrderId, Article) values (@Id, @OrderId, @Article)");
  31. cmd.Transaction = trans;
  32. cmd.Parameters.Add("@Id", DbType.Int32).Value = id;
  33. cmd.Parameters.Add("@OrderId", DbType.Int32).Value = orderId;
  34. cmd.Parameters.Add("@Article", DbType.String).Value = article;
  35. cmd.ExecuteNonQuery();
  36. }
  37. private void InitializeTables()
  38. {
  39. SQLiteCommand cmd = _Connection.CreateCommand();
  40. cmd.CommandText = "create table Orders (Id int, items int, Customer varchar(50), amount float)";
  41. cmd.ExecuteNonQuery();
  42. cmd.CommandText = "create table OrderDetails (Id int, OrderId int, article varchar(50))";
  43. cmd.ExecuteNonQuery();
  44. SQLiteTransaction trans = _Connection.BeginTransaction();
  45. AddOrder(trans, 1, 3, "Peter Parker", 250.30);
  46. AddOrder(trans, 2, 2, "Superman", 504);
  47. AddOrder(trans, 3, 1, "Indiana Jones", 96.34);
  48. AddOrder(trans, 4, 4, "Dark Vader", 1111.11);
  49. AddOrderDetail(trans, 1, 1, "costume");
  50. AddOrderDetail(trans, 2, 1, "snacks");
  51. AddOrderDetail(trans, 3, 1, "shoes");
  52. AddOrderDetail(trans, 4, 2, "costume");
  53. AddOrderDetail(trans, 5, 2, "hairgel");
  54. AddOrderDetail(trans, 6, 3, "whip");
  55. AddOrderDetail(trans, 7, 4, "helmet");
  56. AddOrderDetail(trans, 8, 4, "sword");
  57. AddOrderDetail(trans, 9, 4, "cape");
  58. AddOrderDetail(trans, 10, 4, "The force");
  59. trans.Commit();
  60. }
  61. private void Form1_Load(object sender, EventArgs e)
  62. {
  63. _Connection = new SQLiteConnection("Data Source=test1.db;NewDatabase=True;Synchronous=Off;Encoding=UTF8;Emulator=true");
  64. _Connection.Open();
  65. InitializeTables();
  66. SQLiteCommand cmd = _Connection.CreateCommand();
  67. cmd.CommandText = "select id, items, customer, amount from orders";
  68. DataTable table = new DataTable();
  69. table.Columns.Add("Id");
  70. table.Columns.Add("Items");
  71. table.Columns.Add("Customer");
  72. table.Columns.Add("Amount");
  73. SQLiteDataReader reader = cmd.ExecuteReader();
  74. while (reader.Read())
  75. {
  76. DataRow row = table.NewRow();
  77. row["Id"] = reader.GetInt32(0);
  78. row["Items"] = reader.GetInt32(1);
  79. row["Customer"] = reader.GetString(2);
  80. row["Amount"] = reader.GetDouble(3);
  81. table.Rows.Add(row);
  82. }
  83. dataGridView1.DataSource = table;
  84. }
  85. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  86. {
  87. }
  88. private void dataGridView1_SelectionChanged(object sender, EventArgs e)
  89. {
  90. if (dataGridView1.SelectedRows.Count == 0) return;
  91. int orderId = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
  92. SQLiteCommand cmd = _Connection.CreateCommand();
  93. cmd.CommandText = "select id, orderid, article from orderdetails where OrderId = @OrderId";
  94. cmd.Parameters.Add("@OrderId", DbType.Int32).Value = orderId;
  95. DataTable table = new DataTable();
  96. table.Columns.Add("Id");
  97. table.Columns.Add("OrderId");
  98. table.Columns.Add("Article");
  99. SQLiteDataReader reader = cmd.ExecuteReader();
  100. while (reader.Read())
  101. {
  102. DataRow row = table.NewRow();
  103. row["Id"] = reader.GetInt32(0);
  104. row["OrderId"] = reader.GetInt32(1);
  105. row["Article"] = reader.GetString(2);
  106. table.Rows.Add(row);
  107. }
  108. dataGridView2.DataSource = table;
  109. }
  110. }
  111. }