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

最近一直在做网页。用的js比较多,最近需要做一个C#相关的demo,一开始还有点不适应,写了几句有点感觉了

本篇博客的主要内容是C#怎么读取数据库文件里的数据以及相关控件如何绑定数据源,所做的Demo如图所示:

首先在from上将需要控件拖放好,设置属性,连接数据库:

工具--连接到数据库--选择数据源,填写相关数据:

连接数据库之后,在开头引用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         }

数据库内不同的法律法规有不同的指代编号,这里写一个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);

点击启动,完成,法律法规已经在列表中显示,且不同的法律法规对应着不同的数列:

在控件右边添加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显示不同的法律条文:

 Demo完整代码如下:

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

 

posted @ 2017-06-15 21:12  StephenCurry  阅读(782)  评论(0编辑  收藏  举报