//Получения списка лотов из базы данных static public ObservableCollection <LotInfo> LoadLotsFromDataBase() { ObservableCollection <LotInfo> LotsCollection = new ObservableCollection <LotInfo>(); byte[] dataImage; string sqlCommand = "Select * from LotInfo"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand, connection); DataSet ds = new DataSet(); adapter.Fill(ds); DataTable tableLots = ds.Tables[0]; int n = tableLots.Rows.Count; LotInfo[] Lot = new LotInfo[n]; for (int i = 0; i < n; i++) { Lot[i] = new LotInfo(); Lot[i].ID = (int)tableLots.Rows[i][0]; Lot[i].Name = (string)tableLots.Rows[i][1]; Lot[i].Category = (string)tableLots.Rows[i][2]; Lot[i].Info = (string)tableLots.Rows[i][3]; Lot[i].StartPrice = (int)tableLots.Rows[i][4]; dataImage = (byte[])tableLots.Rows[i][5]; Lot[i].LotImage = LoadImage.BytesToBitmapImage(dataImage); } for (int i = 0; i < n; i++) { LotsCollection.Add(Lot[i]); } } return(LotsCollection); }
//Добавление нового лота static public void AddNewLot(LotInfo lot) { try { string sqlCommand = "Select * from LotInfo"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand, connection); DataSet ds = new DataSet(); adapter.Fill(ds); DataTable dt = ds.Tables[0]; DataRow newRow = dt.NewRow(); newRow["nameLot"] = lot.Name; newRow["category"] = lot.Category; newRow["lotInfo"] = lot.Info; newRow["startPrice"] = lot.StartPrice; newRow["lotImage"] = LoadImage.BitmapImageToBytes(lot.LotImage); dt.Rows.Add(newRow); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter); adapter.Update(ds); MessageBox.Show("Новый лот был добавлен"); } } catch (Exception exp) { MessageBox.Show(exp.Message); } }