Пример #1
0
        /// <summary>
        /// Сохранение автора в БД
        /// </summary>
        /// <param name="author"></param>
        public void SaveAuthor(Author author)
        {
            lock (_locker)
            {
                if (string.IsNullOrEmpty(author.Id))
                {
                    //author.Id = Guid.NewGuid().ToString();
                    author.CheckID();
                }
                //author.timeStamp = DateTime.Now.ToUniversalTime();
                string author_xml = Author2Xml(author);
                if (!string.IsNullOrEmpty(author_xml))
                {
                    // проверим, есть ли такой автор в БД
                    using (SQLiteCommand cmd = new SQLiteCommand("", conn))
                    {
                        string sql = "";
                        sql             = string.Format("select * from authors where id='{0}'", author.Id);
                        cmd.CommandText = sql;
                        int cnt = cmd.ExecuteNonQuery();
                        //long timeStamp = author.timeStamp.ToBinary();

                        if (ds != null)
                        {
                            ds.Dispose();
                        }
                        ds = new DataSet();
                        da = new SQLiteDataAdapter();
                        da.SelectCommand = cmd;
                        var cb = new System.Data.SQLite.SQLiteCommandBuilder(da);
                        da.Fill(ds, "author");
                        if (ds.Tables["author"].Rows.Count == 0)
                        {
                            DataRow dr = ds.Tables["author"].NewRow();
                            dr["id"] = author.Id;
                            ds.Tables["author"].Rows.Add(dr);
                        }
                        ds.Tables["author"].Rows[0]["author_xml"] = author_xml;
                        //ds.Tables["author"].Rows[0]["stamp"] = timeStamp;
                        da.Update(ds.Tables["author"]);
                        //if (cnt > 0)
                        //    sql = string.Format("update authors set author_xml='{0}', stamp={1} where id='{1}'", author_xml, timeStamp, author.Id);
                        //else
                        //    sql = string.Format("insert into authors (id, author_xml, stamp) ('{0}','{1}',{2})", author.Id, author_xml, timeStamp);
                        //cmd.CommandText = sql;
                        //cmd.ExecuteNonQuery();
                    }
                }
            }
        }