//Загрузка документа #region Глобальные функции public static ForecastDocument LoadDocument(int docnum, string headtable, string bodytable, string connection) { ForecastDocument result = new ForecastDocument(); result.bodyTable = bodytable; result.headTable = headtable; result.connectionString = connection; SqlConnection sql = new SqlConnection(connection); string sqlcmd = @"SELECT DocumentDate, RPID, ParentDocument, DocTypeID FROM " + headtable + @" WHERE DocumentID = " + docnum.ToString(); SqlCommand cmd = new SqlCommand(sqlcmd, sql); SqlDataReader reader = null; try { sql.Open(); reader = cmd.ExecuteReader(); } catch (Exception exception) { sql.Close(); //return null; throw new Exception("Ошибка соединения с сервером.", exception); } if (!reader.Read()) { sql.Close(); //return null; throw new Exception("Не удалось загрузить документ с заданным номером."); } ; result.Head.DocumentDate = reader.GetDateTime(0); result.Head.RPInfo = reader.GetInt32(1); result.Head.DocType = reader.GetInt32(3); result.Head.DocumentParent = !reader.IsDBNull(2) ? reader.GetInt32(2) : 0; sql.Close(); sqlcmd = @"SELECT DocumentID, DataHour, DataValue FROM " + bodytable + @" WHERE DocumentID = " + docnum.ToString(); var adapter = new SqlDataAdapter(sqlcmd, sql); try { adapter.Fill(result.DocumentBody); } catch (Exception exception) { //return null; throw new Exception("Ошибка загрузки документа.", exception); } result.SetBodySettings(); return(result); }
public static ForecastDocument CreateDocument(ForecastDocumentHead head, string headtable, string bodytable, string connection) { ForecastDocument result = new ForecastDocument(); result.Head = head; result.bodyTable = bodytable; result.headTable = headtable; result.connectionString = connection; if (head.DocumentParent == 0) // если нет связанного документа { result.DocumentBody.Columns.Add("DocumentID", Type.GetType("System.Int32")); result.DocumentBody.Columns.Add("DataHour", Type.GetType("System.Int32")); result.DocumentBody.Columns.Add("DataValue", Type.GetType("System.Double")); for (int i = 1; i <= 24; ++i) { DataRow row = result.DocumentBody.NewRow(); row["DocumentID"] = result.Head.DocumentNumber; row["DataHour"] = i; row["DataValue"] = 0; result.DocumentBody.Rows.Add(row); } } else //если есть связанный документ { //заполнение данными из связанного документа SqlConnection sql = new SqlConnection(result.connectionString); SqlDataAdapter adapter = new SqlDataAdapter(string.Format("SELECT {0} as DocumentID, DataHour, DataValue FROM {2} WHERE DocumentID = {1}", head.DocumentNumber, head.DocumentParent, bodytable), sql); try { adapter.Fill(result.DocumentBody); } catch (Exception exception) { //return null; throw new Exception("Ошибка загрузки связанного документа.", exception); } } result.SetBodySettings(); if (!result.SaveDocument()) { return(null); } return(result); }