Не могу записать данные в базу MySql.
#39285069
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
//AccessToDataLibrury.cs
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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Provider;
namespace AccessToDataLibrury
{
public partial class AccessToDataLibrury
{
institutelibraryDataSet libruryDataSet;
Provider.Provider provider = new Provider.Provider();
public AccessToDataLibrury(SourceType dataType, string targetData)
{
this.libruryDataSet = this.provider.GetAllData(dataType,targetData);
}
private string MakeFilteredQuery(Dictionary<string, string> searchedTable)
{
string filter = string.Empty;
if (searchedTable == null)
{
return filter;
}
foreach (KeyValuePair<string, string> kvp in searchedTable)
{
if (kvp.Value == null || kvp.Value == string.Empty)
{
continue;
}
if (String.IsNullOrEmpty(filter))
{
filter += string.Format("[{0}] Like '%{1}%'", kvp.Key, kvp.Value);
}
else
{
filter += string.Format(" And [{0}] Like '%{1}%'", kvp.Key, kvp.Value);
}
}
return filter;
}
}
}
//AccessToDataLibrury(Book).cs
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. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. 234. 235. 236. 237. 238. 239. 240. 241. 242. 243. 244. 245. 246. 247. 248. 249. 250. 251. 252. 253. 254. 255. 256. 257. 258. 259. 260. 261. 262. 263. 264. 265. 266. 267. 268. 269. 270. 271. 272. 273. 274. 275. 276. 277. 278. 279. 280. 281. 282. 283. 284. 285. 286. 287. 288. 289. 290. 291. 292. 293. 294. 295. 296. 297. 298. 299. 300. 301. 302. 303. 304. 305. 306. 307. 308. 309. 310. 311. 312. 313. 314. 315. 316. 317. 318. 319. 320. 321. 322. 323. 324. 325. 326. 327. 328. 329. 330. 331. 332. 333. 334. 335. 336. 337. 338. 339. 340. 341. 342. 343. 344. 345. 346. 347. 348. 349. 350. 351. 352. 353. 354. 355. 356. 357. 358. 359. 360. 361. 362. 363. 364. 365. 366. 367. 368. 369. 370. 371. 372. 373. 374.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Common;
namespace AccessToDataLibrury
{
public partial class AccessToDataLibrury
{
public List<Book> GetAllBokks()
{
if(this.libruryDataSet==null)
{
return null;
}
//1 вариант
//DataRowCollection searchedRow = this.libruryDataSet.books.Rows;
//2 Вариант
DataRow[] searchedRow = this.libruryDataSet.books.Select();
return this.GetRestPartsOfBook(searchedRow);
}
List<Book> GetRestPartsOfBook(DataRow[] searchedRows)
{
if(searchedRows == null || searchedRows.Length==0)
{
return null;
}
List<Book> books = new List<Book>();
foreach(institutelibraryDataSet.booksRow bookRow in searchedRows)
{
Book book = new Book();
book.ISBN = bookRow.ISBN;
/*DataRow[] itemRows = this.libruryDataSet.items.Select("ID = "+ bookRow.ItemID);
if(itemRows != null && itemRows.Length!=0)
{
institutelibraryDataSet.itemsRow itemRow =
(institutelibraryDataSet.itemsRow)itemRows[0];
book.ID = bookRow.itemsRow.ID;
book.Name = bookRow.itemsRow.Name;
book.Publisher = bookRow.itemsRow.Publisher;
book.PublishedDate = bookRow.itemsRow.PublishedDate;
}*/
if(bookRow.itemsRow!= null)
{
book.ID = bookRow.itemsRow.ID;
book.Name = bookRow.itemsRow.Name;
book.Publisher = bookRow.itemsRow.Publisher;
book.PublishedDate = bookRow.itemsRow.PublishedDate;
}
else
{
throw new Exception("Cannot find corresponding Item");
}
if (bookRow.authorsRow != null)
{
book.AuthorName = bookRow.authorsRow.Name;
}
else
{
throw new Exception("Cannot find corresponding Item");
}
books.Add(book);
}
return books;
}
List<Book> GetRestPartsOfBook(DataRowCollection searchedRows)
{
if (searchedRows == null || searchedRows.Count == 0)
{
return null;
}
List<Book> books = new List<Book>();
foreach (institutelibraryDataSet.booksRow bookRow in searchedRows)
{
Book book = new Book();
book.ISBN = bookRow.ISBN;
if(bookRow.itemsRow != null)
{
book.ID = bookRow.itemsRow.ID;
book.Name = bookRow.itemsRow.Name;
book.Publisher = bookRow.itemsRow.Publisher;
book.PublishedDate = bookRow.itemsRow.PublishedDate;
}
else
{
throw new Exception("Cannot find corresponding Item");
}
if(bookRow.authorsRow!=null)
{
book.AuthorName = bookRow.authorsRow.Name;
}
else
{
throw new Exception("Cannot find corresponding Item");
}
books.Add(book);
}
return books;
}
public List<Book> GetSearchedBooks(Book searchedBook)
{
/*List<Book> books = new List<Book>();
//items books радительские таблицы
string filter = "Parent(items_books).Name Like '%" + searchedBook.Name +
"' AND Parent(authors_books).Name Like '%" + searchedBook.AuthorName +
"' AND Parent(items_books).Publisher Like '%" + searchedBook.Publisher +
"' AND Convert(Parent(items_books).PublishedDate,'System.String') Like '%" + searchedBook.PublishedDate +
"%'";
DataRow[] searchedBooks = libruryDataSet.books.Select(filter);
//1 метод
/*foreach(DataRow book in searchedBooks)
{
books.Add(new Book(book.GetParentRow("items_books")["Name"].ToString(),
book.GetParentRow("items_books")["Publisher"].ToString(),
book.GetParentRow("items_books")["PublishedDate"].ToString(),
book["ISBN"].ToString(),
book.GetParentRow("authors_books")["Name"].ToString()));
}*/
//2 меторд
/* foreach (institutelibraryDataSet.booksRow book in searchedBooks)
{
books.Add(new Book(book.itemsRow.Name,
book.itemsRow.Publisher,
book.itemsRow.PublishedDate,
book.ISBN,
book.authorsRow.Name));
}
return books;*/
//получаем набор найденных строк из таблицы Items
institutelibraryDataSet.itemsRow[] searchedItemsRows =
(institutelibraryDataSet.itemsRow[])libruryDataSet.items.Select(MakeFilteredQuery(searchedBook.ItemFields));
//получаем набор найденных строк из таблицы Authors
institutelibraryDataSet.authorsRow[] searchedAuthorsRows =
(institutelibraryDataSet.authorsRow[])libruryDataSet.authors.Select(MakeFilteredQuery(searchedBook.AuthorFields));
//искать строки в таблице Books не стоит поскольку поиск по полю ISBN не
//производиться, а это поле находиться именно в таблице Books
if (
(searchedItemsRows == null || searchedItemsRows.Length == 0) ||
(searchedAuthorsRows == null || searchedAuthorsRows.Length == 0)
)
{
return new List<Book>(0);
}
//создаем списки для набора строк Items и Books для более гибкого
//манипулирования массивами
List<institutelibraryDataSet.itemsRow> searchedItemsRowsList =
searchedItemsRows.ToList<institutelibraryDataSet.itemsRow>();
List<institutelibraryDataSet.booksRow> searchedBooksRowsList =
new List<institutelibraryDataSet.booksRow>();
//заполняем список книг. Для каждой записи в таблице Items
//существует всего одна запись в таблице Books
foreach (institutelibraryDataSet.itemsRow itemRow in searchedItemsRowsList)
{
institutelibraryDataSet.booksRow[] bookRow = itemRow.GetbooksRows();
if (bookRow != null && bookRow.Length != 0)
{
searchedBooksRowsList.Add(bookRow[0]);
}
}
//конечный набор искомых строк
List<institutelibraryDataSet.booksRow> realBooksRows =
new List<institutelibraryDataSet.booksRow>();
//выбираем те книги, которые написаны данным алгоритмом
for (int i = 0; i < searchedAuthorsRows.Length; i++)
{
List<institutelibraryDataSet.booksRow> tempSearched =
searchedBooksRowsList.FindAll(
val => val.AuthorID == searchedAuthorsRows[i].ID);
realBooksRows.AddRange(tempSearched);
}
if (realBooksRows.Count == 0)
return new List<Book>(0);
return GetRestPartsOfBook(realBooksRows.ToArray());
}
public void AddBook(List<Book> book_)
{
DataRow authorDataRow;
foreach(Book book in book_)
{
bool canAdd = IsUniqueBookInDB(book, out authorDataRow,LibruryAction.AddBook);
if (!canAdd)
{
continue;
}
institutelibraryDataSet.itemsRow itemRow =
libruryDataSet.items.AdditemsRow(Guid.NewGuid().ToString(), book.Name,
book.Publisher);//, book.PublishedDate
institutelibraryDataSet.authorsRow authorRow;
//если такого автора еще нет в БД
if(authorDataRow==null)
{
authorRow = libruryDataSet.authors.AddauthorsRow(Guid.NewGuid().ToString(),
book.AuthorName);
}
else
{
authorRow=(institutelibraryDataSet.authorsRow)authorDataRow;
}
libruryDataSet.books.AddbooksRow(itemRow,authorRow,book.ISBN);
}
provider.UpdateAllData();
}
bool IsUniqueBookInDB(Book book, out DataRow authorRow, LibruryAction action)
{
DataRow[] bookRows = null;
try
{
bookRows = libruryDataSet.books.Select("[ISBN]='" + book.ISBN + "'");
}
catch (NullReferenceException)
{
bookRows = null;
}
authorRow = null;
if (action == LibruryAction.AddBook)
{
if (bookRows != null && bookRows.Length > 0)
{
return false;
}
}
else if (action == LibruryAction.AddBook)
{
if (bookRows != null && bookRows.Length > 0)
{
foreach (institutelibraryDataSet.booksRow bookRow in bookRows)
{
if (bookRow.ItemID != book.ID)
{
return false;
}
}
}
}
else
{
return false;
}
DataRow[] authorsRows;
try
{
authorsRows =
libruryDataSet.authors.Select("Name='"+book.AuthorName+"'");
}
catch(NullReferenceException)
{
authorsRows =null;
}
if (authorsRows != null && authorsRows.Length > 0)
{
authorRow = authorsRows[0];
foreach(DataRow authorR in authorsRows)
{
institutelibraryDataSet.booksRow[] booksRows =
((institutelibraryDataSet.authorsRow)authorR).GetbooksRows();
foreach(institutelibraryDataSet.booksRow bookR in booksRows)
{
if(book.ID != bookR.ItemID)
{
if(book.Name==bookR.itemsRow.Name)
return false;
}
}
}
}
return true;
}
public void UpdateBooks(List<Book> book_)
{
DataRow author;
foreach(Book book in book_)
{
bool canUpdate = IsUniqueBookInDB(book, out author,LibruryAction.AddBook);
if(!canUpdate)
{
continue;
}
institutelibraryDataSet.itemsRow itemRow =
libruryDataSet.items.FindByID(book.ID);
itemRow.Name = book.Name;
itemRow.Publisher = book.Publisher;
itemRow.PublishedDate = book.PublishedDate;
institutelibraryDataSet.authorsRow authorRow;
if(author==null)
{
authorRow = libruryDataSet.authors.AddauthorsRow(
Guid.NewGuid().ToString(), book.AuthorName);
}
else
{
authorRow=(institutelibraryDataSet.authorsRow)author;
}
institutelibraryDataSet.booksRow bookRow =
libruryDataSet.books.FindByItemID(book.ID);
bookRow.ISBN = book.ISBN;
bookRow.AuthorID = authorRow.ID;
}
provider.UpdateAllData();
}
public List<Copy> GetBookCopy(string bookID, out string bookName)
{
DataRow[] bookCopy = libruryDataSet.copies.Select("ItemID='"+bookID+"'");
bookName = libruryDataSet.items.FindByID(bookID).Name;
List<Copy> copy = new List<Copy>();
foreach(institutelibraryDataSet.copiesRow singleCop in bookCopy)
{
bool isBorrowed = false;
//1
// DataRow borrowed = libruryDataSet.borrows.Rows.Find(singleCop.ID);
//2
// DataRow[] borrowed = singleCop.GetborrowsRows();
//3
DataRow[] borrowed = singleCop.GetChildRows("copies_borrows");
if(borrowed!=null && borrowed.Length>0)
{
isBorrowed = true;
}
copy.Add(new Copy(singleCop.ID,singleCop.ItemID,isBorrowed));
}
return copy;
}
public bool Borrowingook(string copyID)
{
return true;
}
public bool BorrowingBook(List<BorrowedCopy> borCops)
{
try
{
foreach (BorrowedCopy borCop in borCops)
{
libruryDataSet.borrows.AddborrowsRow(
libruryDataSet.copies.FindByID(borCop.CopyID),
borCop.BorrowedDate,
libruryDataSet.users.FindByID(borCop.UserID));
}
provider.UpdateAllData();
}
catch
{
return false;
}
return true;
}
}
}
//NewBook.cs
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.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
namespace Library
{
public partial class NewBook : Form
{
public List<Book> NewBook_ { get; private set; }
public NewBook()
{
InitializeComponent();
NewBook_ = new List<Book>();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void AddBook_bnt_Click(object sender, EventArgs e)
{
string name = Name_txbx.Text.Trim();
string publisher = Publisher_txbx.Text.Trim();
string publishedDate = Publisher_txbx.Text.Trim();
string isbn = ISBN_txbx.Text.Trim();
string authorName = AuthorName_txbx.Text.Trim();
if(name!= string.Empty &&
publisher != string.Empty &&
publishedDate!=string.Empty &&
isbn != string.Empty &&
authorName != string.Empty)
{
NewBook_.Add(new Book(name,publisher,publishedDate,isbn,authorName));
bookCount_lbl.Text = "Новых книг:" + NewBook_.Count;
return;
}
MessageBox.Show("Не все поля указаны");
return;
}
private void SaveBooks_btn_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
}
}
|
|