/// <summary>
        /// 添加source
        /// </summary>
        /// <param name="source"></param>
        /// <exception cref="ItemAlreadyExitedException"></exception>
        /// <exception cref="ItemOperationFaildException"></exception>
        /// <returns></returns>
        public HostsSource AddHostSource(HostsSource source)
        {
            var s = FindByUrl(source.Url);

            if (s != null)
            {
                throw new ItemAlreadyExitedException();
            }
            var conn = new SQLiteConnection(ExtentionClass.ConnectinString);

            conn.Open();
            var h = new SQLiteHelper(new SQLiteCommand(conn));

            var sql = $"insert into hostssource (name,url) values('{source.Name}','{source.Url}')";
            var n   = h.Execute(sql);

            if (n > 0)
            {
                sql = "select last_insert_rowid() as Id from hostssource;";
                var obj = h.ExecuteScalar(sql);
                int id;
                int.TryParse(obj + "", out id);
                return(FindById(id));
            }
            throw new ItemOperationFaildException();
        }
示例#2
0
        /// <summary>
        /// hosts源列表上移下移
        /// </summary>
        /// <param name="pos"></param>
        private void ChangeSequence(int pos)
        {
            var sourceNow = lstSource.SelectedItem as HostsSource;

            if (sourceNow != null)
            {
                var         nowindex = _hostsSources.IndexOf(sourceNow);
                HostsSource alter    = null;
                if (nowindex + pos >= 0 && nowindex + pos < _hostsSources.Count)
                {
                    alter = _hostsSources[nowindex + pos];
                }
                try
                {
                    if (alter != null)
                    {
                        var n = HostsSourceManager.Instance.ChangeSequence(sourceNow.Id, alter.Id);
                        if (n > 0)
                        {
                            LoadHostSource(alter.Id);
                        }
                        else
                        {
                            Message("操作失败");
                        }
                    }
                }
                catch (ItemNotFoundException)
                {
                    Message("要操作的对象不存在");
                }
            }
        }
        public HostsSource DataRowToHostsSource(DataRow row)
        {
            var source = new HostsSource();

            source.Id        = int.Parse(row["Id"] + "");
            source.IsEnabled = int.Parse(row["IsEnabled"] + "");
            source.Name      = row["Name"] + "";
            source.Url       = row["Url"] + "";
            return(source);
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="source"></param>
        /// <exception cref="ItemNotFoundException"></exception>
        /// <returns></returns>
        public int UpdateHostsSource(HostsSource source)
        {
            var s = FindById(source.Id);

            if (s == null)
            {
                throw new ItemNotFoundException();
            }
            var sql = $"update hostssource set url = '{source.Url}',name='{source.Name}' where id ={source.Id}";

            return(Helper.Execute(sql));
        }
示例#5
0
        /// <summary>
        /// 确认点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                if (Source == null)
                {
                    Source = new HostsSource();
                }

                var r = new Uri(txtUrl.Text);
                Source.Url   = r.ToString();
                Source.Name  = txtName.Text.Trim();
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (UriFormatException)
            {
                Message("Url格式不正确");
            }
            EnableControl(sender);
        }