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

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

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

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

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

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

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

连接数据库之后,在开头引用using System.Data.SqlClient;

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

      //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"] });//邦定数据
}
dtr.Close();
comboBox1.SelectedIndex = ;
conn.Close();
}

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

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

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

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

 //对应法律名称的法律编号
int rawid = laws[name];
int id = ;
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() )
{
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显示不同的法律条文:

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

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

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 / ;
panel1.Top = ;
panel1.Left = ;
panel1.Width = width; //设置panel2的属性
panel2.Height = height - height / ;
panel2.Top = panel1.Height;
panel2.Left = ;
panel2.Width = width / ; //设置panel3的属性
panel3.Height = height - height / ;
panel3.Top = panel1.Height;
panel3.Left = width / ;
panel3.Width = width - width / ; //设置treeView1属性
treeView1.Top = ;
treeView1.Left = ;
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 = ;
conn.Close();
} //显示具体的法律条款
private void ShowLaw(string name)
{
//对应法律名称的法律编号
int rawid = laws[name];
int id = ;
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() )
{
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(, );
switch (schar)
{
case "":
res = "一";
break;
case "":
res = "二";
break;
case "":
res = "三";
break;
case "":
res = "四";
break;
case "":
res = "五";
break;
case "":
res = "六";
break;
case "":
res = "七";
break;
case "":
res = "八";
break;
case "":
res = "九";
break;
default:
res = "零";
break;
}
if (str.Length > )
{
switch (str.Length)
{
case :
case :
res += "十";
break;
case :
case :
res += "百";
break;
case :
res += "千";
break;
case :
res += "万";
break;
default:
res += "";
break;
}
res += NumberToChinese(int.Parse(str.Substring(, str.Length - )));
} res = res.Replace("十零", "十").Replace("一十", "十");
return res;
} private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
label1.Text = e.Node.Tag.ToString();
} }
}
上一篇:gstreamer 编译实例


下一篇:Coding Interviews 20 包含min函数的栈