当前位置: 源码素材网 » C#教程 » 详情页

C# DSN 数据源管理类 附代码

  •   时间:2018-11-08
  • 概述:数据源

一个C#数据源管理类,如果你的程序需要多个数据源,而让用户去设定需要的数据源,可以参考本程序,它可以加载所有数据源至GridView,自动选中GridView中DataSourceId的行,并结合窗体上的按键事件定义新的数据源:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataFactory;
using DataEntity;
namespace WebDataGather
{
    public partial class DataSourceManage : Form
    {
        #region 变量定义
        /// <summary>
        /// 要编辑的数据源Id
        /// </summary>
        public int DataSourceId = 0;
        /// <summary>
        /// 新增数据源的信息类别所有者Id
        /// </summary>
        public int MgID = 0;
        /// <summary>
        /// 数据操作类
        /// </summary>
        private PageCommon Common = new PageCommon();
        /// <summary>
        /// 数据源列表数据
        /// </summary>
        DataSet ds = new DataSet();
        #endregion
        #region 系统初始化
        public DataSourceManage()
        {
            InitializeComponent();
            //加载所有数据源至GridView
            DataBindGridView();
        }
        private void DataBindGridView()
        {
            ds = Common.GetDataSourceConfigList();
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            this.dataGridView1.ReadOnly = true;


        }
        private void DataSourceManage_Load(object sender, EventArgs e)
        {
            if (this.DataSourceId != 0)
                DataBindForm();
            //自动选中GridView中DataSourceId的行
            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                if (this.dataGridView1.Rows[i].Cells[0].Value.ToString() == DataSourceId.ToString())
                {
                    this.dataGridView1.Rows[i].Selected = true;
                }
                else
                {
                    this.dataGridView1.Rows[i].Selected = false;
                }
            }
        }
        #endregion
        #region 按钮事件
        /// <summary>
        /// 取消按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cancel_Click(object sender, EventArgs e)
        {
            //如何在此处更新主窗体的树型控件?
            this.Close();
        }
        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Save_Click(object sender, EventArgs e)
        {
            //判断有效性
            if (this.DataSourceName.Text.Trim() == "")
            {
                this.errorProvider1.SetError(this.DataSourceName, "数据源名称不可为空!");
                MessageBox.Show(this, "数据源名称不可为空!", "信息提示", MessageBoxButtons.OK);
                return;
            }
            if (this.DataSourceUrl.Text.Trim() == "")
            {
                this.errorProvider1.SetError(this.DataSourceUrl, "数据源Url地址不可为空!");
                MessageBox.Show(this, "数据源Url地址不可为空!", "信息提示", MessageBoxButtons.OK);
                return;
            }
            if (this.DataSourceEncoding.Text.Trim() == "")
            {
                this.errorProvider1.SetError(this.DataSourceEncoding, "字符编码不可为空!");
                MessageBox.Show(this, "字符编码不可为空!", "信息提示", MessageBoxButtons.OK);
                return;
            }

            //if (this.Remark.Text.Trim() == "")
            //{
            //    this.errorProvider1.SetError(this.Remark, "备注不可为空!");
            //    MessageBox.Show(this, "备注不可为空!", "信息提示", MessageBoxButtons.OK);
            //    return;
            //}
            if (this.OrderId.Text.Trim() == "")
            {
                this.errorProvider1.SetError(this.OrderId, "显示顺序不可为空!");
                MessageBox.Show(this, "显示顺序不可为空", "信息提示", MessageBoxButtons.OK);
                return;
            }
            Gather_DataSourceConfig Model = new Gather_DataSourceConfig();
            Model.DataSourceEncoding = this.DataSourceEncoding.Text.Trim();
            Model.DataSourceName = this.DataSourceName.Text.Trim();
            Model.DataSourceUrl = this.DataSourceUrl.Text.Trim();
            Model.OrderId = int.Parse(this.OrderId.Text.Trim());
            Model.Remark = this.Remark.Text.Trim();
            int Num = 0;
            //表示添加新数据源
            if (this.DataSourceId == 0)
            {
                Model.PeriodId = 0;
                //添加数据源,返回数据源的Id
                Num = Common.AddDataSourceConfig(Model);
                if (Num != 0)
                {
                    //将数据源的Id,累加至Message表的DataSourceIds字段
                    Gather_Message Message = new Gather_Message();
                    Message = Common.GetMessageModel(MgID);
                    Message.DataSourceIds = Message.DataSourceIds + Num.ToString() + ",";
                    Num = Common.UpdateMessage(Message);
                }
                else
                {
                    MessageBox.Show(this, "保存失败!", "信息提示", MessageBoxButtons.OK);
                }
            }
            else
            {
                Model.PeriodId = Common.GetDataSourceConfig(this.DataSourceId).PeriodId;
                Model.DataSourceId = this.DataSourceId;
                Num = Common.UpdateDataSourceConfig(Model);
            }
            if (Num == 1)
            {
                //加载所有数据源至GridView
                DataBindGridView();
                MessageBox.Show(this, "保存成功!", "信息提示", MessageBoxButtons.OK);
            }
            else
            {
                MessageBox.Show(this, "保存失败!", "信息提示", MessageBoxButtons.OK);
            }
        }
        #endregion
        #region GridView行单击、双击事件
        private void dataGridView1_MouseUp(object sender, MouseEventArgs e)
        {
            int RowNum = 0;
            RowNum = this.dataGridView1.CurrentRow.Index;
            //绑定编辑区控件值
            this.DataSourceId = int.Parse(ds.Tables[0].Rows[RowNum]["DataSourceId"].ToString());
            DataBindForm();
        }
        private void DataBindForm()
        {
            Gather_DataSourceConfig DataSourceConfig = new Gather_DataSourceConfig();
            DataSourceConfig = Common.GetDataSourceConfig(this.DataSourceId);
            this.DataSourceEncoding.Text = DataSourceConfig.DataSourceEncoding;
            this.DataSourceName.Text = DataSourceConfig.DataSourceName;
            this.DataSourceUrl.Text = DataSourceConfig.DataSourceUrl;
            this.Remark.Text = DataSourceConfig.Remark;
            this.OrderId.Text = DataSourceConfig.OrderId.ToString();
        }
        /// <summary>
        /// 行双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int RowNum = 0;
            RowNum = this.dataGridView1.CurrentRow.Index;
            ParticularManage particularManage = new ParticularManage();
            particularManage.DataSourceId = int.Parse(ds.Tables[0].Rows[RowNum]["DataSourceId"].ToString());
            particularManage.ShowDialog();
        }
        #endregion
    }
}

    相关声明:

      若“C# DSN 数据源管理类 附代码”有损您的权益,请告之我们删除内容。
      部分文章来源于网络,版权归原作者所有。