菜单

C#端加载数据库,Combobox与Node控件绑定数据源demo示例

2019年4月14日 - mg娱乐场www4355com

mg娱乐场www4355com,如今直接在做网页。用的js相比多,近期亟需做1个C#相关的demo,1开端还有些不适于,写了几句有点感觉了

本篇博客的机要内容是C#怎么读取数据库文件里的数量以及有关控件怎样绑定数据源,所做的德姆o如图所示:

mg娱乐场www4355com 1

第叁在from中校需求控件拖放好,设置属性,连接数据库:

工具–连接到数据库–选取数据源,填写有关数据:

mg娱乐场www4355com 2

总是数据库之后,在起来引用using System.Data.SqlClient;

编纂二个函数,命名称为SetCombobox,从数据库内位Combobox绑定数据源,具体的函数如下

 1      //Combobox绑定数据源
 2         private void SetCombobox()
 3         {
 4             //输入想关联的数据库的基本信息
 5             SqlConnection conn = new SqlConnection(@" server = DESKTOP-5SDB4D4;Initial Catalog= test;User ID=sa;Password=lmz123LMZ");
 6             //打开数据库连接
 7             conn.Open();
 8             //sql语句
 9             string str = " select * from [test].[dbo].[TRaw] where c1 = 0";
10             //创建命令对象
11             SqlCommand lo_cmd = conn.CreateCommand();
12             lo_cmd.CommandText = str;
13             SqlDataReader dtr = lo_cmd.ExecuteReader();
14             while (dtr.Read())
15             {
16                 //邦定数据
17                 comboBox1.Items.AddRange(new object[] { dtr["c3"] });//邦定数据
18             }
19             dtr.Close();
20             comboBox1.SelectedIndex = 0;
21             conn.Close(); 
22         }

数据库内不一致的法律法规有两样的代表编号,那里写3个dictionary函数,当选中国和法国律条款时重返对应的数目编号

  //法律法规与编号相对应
        Dictionary<string, int> laws = new Dictionary<string, int>();

当读取数据库时编辑dictionary函数:

  //法律名称
     string Lawsname = (string)dtr["c3"];
     //法律代码
     int number = (int)dtr["c0"];

     //向dictionary函数赋值
      if (!laws.ContainsKey(Lawsname))
         laws.Add(Lawsname, number)
       else
          Console.WriteLine(Lawsname);

点击运转,实现,法律法规已经在列表中显得,且分裂的法律法规对应着差别的数列:

mg娱乐场www4355com 3

在控件左侧添加treeview控件,具体绑定数据库代码如下:

 //对应法律名称的法律编号
            int rawid = laws[name];
            int id = 1;
            treeView1.Nodes.Clear();

            //输入想关联的数据库的基本信息
            SqlConnection conn = new SqlConnection(@"server = DESKTOP-5SDB4D4;Initial Catalog= test;User ID=sa;Password=lmz123LMZ");
            conn.Open();
            //sql语句
            string str = "select * from [test].[dbo].[TRaw] where c4 = 1 and c0 = " + rawid;
            //创建命令对象
            SqlCommand lo_cmd = conn.CreateCommand();
            lo_cmd.CommandText = str;
            SqlDataReader dtr = lo_cmd.ExecuteReader();


            while (dtr.Read())
            {
                //父节点的内容
                TreeNode node = new TreeNode("第" + NumberToChinese(id) + "条");
                treeView1.Nodes.Add(node);
                //自动展开
                treeView1.ExpandAll();
                string Laws1;

                //法律名称
                if ( !dtr.IsDBNull(6) )
                {
                    Laws1 = (string)dtr["c3"];
                }
                else
                   Laws1 = "子条款"; 
                node.Tag = Laws1;
                id++;

            }
            dtr.Close();
            conn.Close();

编辑nodeMouseClick事件:

 private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            label1.Text = e.Node.Tag.ToString();
        }

成效如图所示,分歧法律名称对应不相同的数据量,单机node呈现不相同的法律条文:

mg娱乐场www4355com 4

mg娱乐场www4355com 5

 德姆o完整代码如下:

mg娱乐场www4355com 6mg娱乐场www4355com 7

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.SqlClient;

namespace WindowsFormsApp1
{
    public partial class Form1 :Form
    {
        //法律法规与编号相对应
        Dictionary<string, int> laws = new Dictionary<string, int>();

        public Form1()
        {
            InitializeComponent();
            SetInfo();
            SetCombobox();         
        }

        //设置属性信息
        private void SetInfo()
        {
            //获取窗体长度
            int width = this.Width;
            //获取窗体高度
            int height = this.Height;

            //设置panel1的属性
            panel1.Height = height / 5;
            panel1.Top = 0;
            panel1.Left = 0;
            panel1.Width = width;

            //设置panel2的属性
            panel2.Height = height - height / 5;
            panel2.Top = panel1.Height;
            panel2.Left = 0;
            panel2.Width = width / 5;

            //设置panel3的属性
            panel3.Height = height - height / 5;
            panel3.Top = panel1.Height;
            panel3.Left = width / 5;
            panel3.Width = width - width / 5;

            //设置treeView1属性
            treeView1.Top = 0;
            treeView1.Left = 0;
            treeView1.Width = panel2.Width;
            treeView1.Height = panel2.Height;
        }

        //Combobox绑定数据源
        private void SetCombobox()
        {
            //输入想关联的数据库的基本信息
            SqlConnection conn = new SqlConnection(@"server = DESKTOP-5SDB4D4;Initial Catalog= test;User ID=sa;Password=lmz123LMZ");
            //打开数据库连接
            conn.Open();
            //sql语句
            string str = " select * from [test].[dbo].[TRaw] where c1 = 0";
            //创建命令对象
            SqlCommand lo_cmd = conn.CreateCommand();
            lo_cmd.CommandText = str;
            SqlDataReader dtr = lo_cmd.ExecuteReader();

            while (dtr.Read())
            {
                //邦定数据
                comboBox1.Items.AddRange(new object[] { dtr["c3"] });//邦定数据
                //法律名称
                string Lawsname = (string)dtr["c3"];
                //法律代码
                int number = (int)dtr["c0"];

                //向dictionary函数赋值
                if (!laws.ContainsKey(Lawsname))
                    laws.Add(Lawsname, number);
                else
                    Console.WriteLine(Lawsname);
            }
            dtr.Close();
            comboBox1.SelectedIndex = 0;
            conn.Close();
        }

        //显示具体的法律条款
        private void ShowLaw(string name)
        {
            //对应法律名称的法律编号
            int rawid = laws[name];
            int id = 1;
            treeView1.Nodes.Clear();

            //输入想关联的数据库的基本信息
            SqlConnection conn = new SqlConnection(@"server = DESKTOP-5SDB4D4;Initial Catalog= test;User ID=sa;Password=lmz123LMZ");
            conn.Open();
            //sql语句
            string str = "select * from [test].[dbo].[TRaw] where c4 = 1 and c0 = " + rawid;
            //创建命令对象
            SqlCommand lo_cmd = conn.CreateCommand();
            lo_cmd.CommandText = str;
            SqlDataReader dtr = lo_cmd.ExecuteReader();


            while (dtr.Read())
            {
                //父节点的内容
                TreeNode node = new TreeNode("第" + NumberToChinese(id) + "条");
                treeView1.Nodes.Add(node);
                //自动展开
                treeView1.ExpandAll();
                string Laws1;

                //法律名称
                if ( !dtr.IsDBNull(6) )
                {
                    Laws1 = (string)dtr["c3"];
                }
                else
                   Laws1 = "子条款"; 
                node.Tag = Laws1;
                id++;

            }
            dtr.Close();
            conn.Close();
        }

        //当选择框发生变化时
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name = (string)comboBox1.SelectedItem;
            ShowLaw(name);
        }


        //数字转中文
        public string NumberToChinese(int number)
        {
            string res;
            string str = number.ToString();
            string schar = str.Substring(0, 1);
            switch (schar)
            {
                case "1":
                    res = "一";
                    break;
                case "2":
                    res = "二";
                    break;
                case "3":
                    res = "三";
                    break;
                case "4":
                    res = "四";
                    break;
                case "5":
                    res = "五";
                    break;
                case "6":
                    res = "六";
                    break;
                case "7":
                    res = "七";
                    break;
                case "8":
                    res = "八";
                    break;
                case "9":
                    res = "九";
                    break;
                default:
                    res = "零";
                    break;
            }
            if (str.Length > 1)
            {
                switch (str.Length)
                {
                    case 2:
                    case 6:
                        res += "十";
                        break;
                    case 3:
                    case 7:
                        res += "百";
                        break;
                    case 4:
                        res += "千";
                        break;
                    case 5:
                        res += "万";
                        break;
                    default:
                        res += "";
                        break;
                }
                res += NumberToChinese(int.Parse(str.Substring(1, str.Length - 1)));
            }

            res = res.Replace("十零", "十").Replace("一十", "十");
            return res;
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            label1.Text = e.Node.Tag.ToString();
        }


    }
}

View Code

 

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图