Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="orgCode"></param>
        /// <param name="contractCode"></param>
        public void SaveDocument(comm.Document doc, string orgCode, string contractCode)
        {
            using (SqlConnection con = new SqlConnection(_con)) {
                using (SqlCommand cmd = new SqlCommand()) {
                    cmd.Connection  = con;
                    cmd.CommandType = CommandType.StoredProcedure;

                    con.Open();
                    SqlTransaction tranc = con.BeginTransaction();
                    cmd.Transaction = tranc;

                    try {
                        //первая часть сохраняем сам документ
                        cmd.CommandText = "DocAdd";

                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@Name", doc.Name);
                        cmd.Parameters.AddWithValue("@Kind", doc.Kind.ID);
                        cmd.Parameters.AddWithValue("@UsrName", "EArchiv");
                        cmd.Parameters.AddWithValue("@Hash", doc.ConvertHash());

                        //TODO: Костыль позже убрать
                        long   len;
                        byte[] buf;
                        if (doc.Thumbnails != null)
                        {
                            len = doc.Thumbnails.StreamSource.Length;
                            buf = new byte[len];
                            doc.Thumbnails.StreamSource.Read(buf, 0, (int)len);
                            cmd.Parameters.AddWithValue("@Thumbnails", buf);
                        }
                        else
                        {
                            buf    = new byte[2];
                            buf[0] = 1;
                            buf[1] = 2;
                            cmd.Parameters.AddWithValue("@Thumbnails", buf);
                        }

                        FileStream s = new FileStream((doc.Source.StreamSource as FileStream).Name, FileMode.Open);

                        len        = s.Length;
                        buf        = new byte[len];
                        s.Position = 0;
                        s.Read(buf, 0, (int)len);
                        cmd.Parameters.AddWithValue("@Raw", buf);

                        int          key      = -1;
                        SqlParameter parametr = new SqlParameter("@Key", key);
                        parametr.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(parametr);

                        cmd.ExecuteNonQuery();
                        doc.setID(Convert.ToInt32(cmd.Parameters["@Key"].Value));

                        // вторая часть связываем документ с договором
                        cmd.CommandText = "DocContractAdd";

                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@Doc", doc.ID);
                        cmd.Parameters.AddWithValue("@OrgCode", orgCode);
                        cmd.Parameters.AddWithValue("@ContractCode", contractCode);
                        cmd.Parameters.AddWithValue("UsrName", "EContract");

                        parametr           = new SqlParameter("@Key", key);
                        parametr.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(parametr);

                        cmd.ExecuteNonQuery();

                        tranc.Commit();
                    }

                    catch (Exception e) {
                        tranc.Rollback();
                        throw new Exception(e.Message);
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="folder"></param>
        /// <param name="ls"></param>
        public void SaveDocument(comm.Document doc, comm.Folder folder, int ls)
        {
            using (SqlConnection con = new SqlConnection(_con)) {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection  = con;
                cmd.CommandType = CommandType.StoredProcedure;

                con.Open();
                SqlTransaction tranc = con.BeginTransaction();
                cmd.Transaction = tranc;

                try {
                    //первая часть сохраняем сам документ
                    cmd.CommandText = "DocAdd";

                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@Name", doc.Name);
                    cmd.Parameters.AddWithValue("@Kind", doc.Kind.ID);
                    cmd.Parameters.AddWithValue("@UsrName", "EArchiv");
                    cmd.Parameters.AddWithValue("@Hash", doc.ConvertHash());

                    //TODO: Костыль позже убрать
                    long   len;
                    byte[] buf;
                    if (doc.Thumbnails != null)
                    {
                        len = doc.Thumbnails.StreamSource.Length;
                        buf = new byte[len];
                        doc.Thumbnails.StreamSource.Read(buf, 0, (int)len);
                        cmd.Parameters.AddWithValue("@Thumbnails", buf);
                    }
                    else
                    {
                        buf    = new byte[2];
                        buf[0] = 1;
                        buf[1] = 2;
                        cmd.Parameters.AddWithValue("@Thumbnails", buf);
                    }

                    FileStream s = new FileStream((doc.Source.StreamSource as FileStream).Name, FileMode.Open);

                    len        = s.Length;
                    buf        = new byte[len];
                    s.Position = 0;
                    s.Read(buf, 0, (int)len);
                    cmd.Parameters.AddWithValue("@Raw", buf);

                    int          key      = -1;
                    SqlParameter parametr = new SqlParameter("@Key", key);
                    parametr.Direction = ParameterDirection.Output;
                    cmd.Parameters.Add(parametr);

                    cmd.ExecuteNonQuery();
                    doc.setID(Convert.ToInt32(cmd.Parameters["@Key"].Value));

                    //вторая часть добавляем документ в папку
                    //TODO: Надо подумать как переделать

                    /*
                     * cmd.CommandText = "DocFolderAdd";
                     *
                     * cmd.Parameters.Clear();
                     * cmd.Parameters.AddWithValue("@DocID", key);
                     * cmd.Parameters.AddWithValue("FolderID", folder.ID);
                     * cmd.Parameters.AddWithValue("UsrName", "EArchiv");
                     *
                     * parametr = new SqlParameter("@Key", key);
                     * parametr.Direction = ParameterDirection.Output;
                     * cmd.Parameters.Add(parametr);
                     *
                     * cmd.ExecuteNonQuery();
                     */

                    // третья часть связываем документ с лицевым счетом
                    cmd.CommandText = "DocLsAdd";

                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@Doc", doc.ID);
                    cmd.Parameters.AddWithValue("Ls", ls);
                    cmd.Parameters.AddWithValue("UsrName", "EArchiv");

                    parametr           = new SqlParameter("@Key", key);
                    parametr.Direction = ParameterDirection.Output;
                    cmd.Parameters.Add(parametr);

                    cmd.ExecuteNonQuery();

                    tranc.Commit();
                }

                catch (Exception e) {
                    try {
                        tranc.Rollback();
                        throw new Exception(e.Message);
                    }

                    catch (Exception ex) {
                        throw new Exception(ex.Message);
                    }
                }
            }
        }