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

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

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

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

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

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

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

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();
} }
}

C#端加载数据库,Combobox与Node控件绑定数据源demo示例的更多相关文章

  1. ComboBox控件绑定数据源后,添加'请选择'或'全部'

    ComboBox控件绑定数据源后,添加'请选择'或'全部' 当使用ComboBox控件绑定数据源之后,通过Items 属性添加的数据是无效的,此时如果要在所有选项前添加 选项 ,则需要考虑从数据源下手 ...

  2. ComboBox控件绑定数据源

    最近在研究机房收费系统的组合查询的方法时,看到了ComboBox控件可以进行数据绑定,我觉得这个功能真的很不错,可以给我省去很多的麻烦. 下面是我组合查询窗体界面 一.数据转换方法 现在我们开看一下我 ...

  3. Winform开发之ComboBox和ComboBoxEdit控件绑定key/value数据

    使用 ComboBox 控件绑定key/value值: 因为 ComboBox 是有 DataSource 属性的,所以它可以直接绑定数据源,如 DataTable.ListItem 等. 使用 Da ...

  4. [MFC] MFC 打开HTML资源(用ID版,也可加载到自己的web控件上)

    @ ^ @:如果是加载到web控件上,就把注释掉的解除注释(改为web控件点后面的函数),把下一句注释 BOOL Button::LoadFromResource(UINT nRes){//打开网页加 ...

  5. ASP.NET中页面加载时文本框(texbox控件)内有文字获得焦点时文字消失

    代码如下: <asp:TextBox ID="TextBox1" runat="server" Height="26px" MaxLe ...

  6. BitmapImage处理网络图片,例如阿里云获取的图片。异步加载到需要显示的控件上。提升速度非常明显。

    想直接把网络图片赋给控件,又要下载又要缓存,速度非常慢.不流畅. 需要进行处理,异步加载会显著提升速度.方法如下: public static BitmapImage ByteArrayToBitma ...

  7. 【WPF学习笔记】之如何点击“新建”按钮,在面板中加载一条条的“用户控件”的信息:动画系列之(四)

    ...... 承接上一系列动画三. 在主界面后台代码设置嵌套第二个用户控件. using System; using System.Collections.Generic; using System. ...

  8. 页面加载通过javascript来修改控件属性

    function changeFormElementStatus(tagNames) {            var tagNameArr = tagNames.split("," ...

  9. ajax验证表单元素规范正确与否 ajax展示加载数据库数据 ajax三级联动

    一.ajax验证表单元素规范正确与否 以用ajax来验证用户名是否被占用为例 1创建表单元素<input type="text" id="t"> 2 ...

随机推荐

  1. sass中的循环判断条件语句

    @if $lte7:true !default;//是否兼容ie6,7 //inline-block //ie6-7 *display: inline;*zoom:1; @mixin inline-b ...

  2. Kattis之旅——Number Sets

    You start with a sequence of consecutive integers. You want to group them into sets. You are given t ...

  3. suse日常操作(含suse/rhel内核与发行版对应关系)

    最近有家客户要求只能使用suse系统,是suse 12 sp3的,而且版本都不同意换,一直以来,都是使用rhel的客户,还没遇到过suse的,可偏偏不巧,我们的系统和suse 12 sp3自带的gli ...

  4. amqp 抓包

    1. wireshark 2. tcpick -yR -r  file.name

  5. intel FPGA使用

    https://www.altera.com/documentation/swn1503506366945.html https://files.cnblogs.com/files/shaohef/o ...

  6. yolo3(目标检测)实测

    yolo是继faster-r-cnn后,原作者在目标检测领域进行的新研究.到了v3版本以后,虽然已经换人支持,但是更注重工程实践,在实际使用过程中突出感受就是 “非常快”,GPU加速以后能够达到实时多 ...

  7. javaweb三大框架和MVC设计模式

    javaweb三大框架和MVC设计模式 转载,原文请见https://blog.csdn.net/sunpeng19960715/article/details/50890705 一.MVC设计模式 ...

  8. Java基础——javaMail:使用心得

    想要做一个java发送邮件小功能. 使用的maven搭建的项目. <!-- https://mvnrepository.com/artifact/javax.mail/mail -->&l ...

  9. css的再深入4(更新中···)

    两种居中的方式: Margin:0 auto;和text-align:center; Margin的居中是对自身,text-align对元素内部的文本来说. 隐藏的两种方式: visibility:h ...

  10. 再谈 tp的 实例化 类 的自动加载

    表示一个域名下的所有/任何主机 使用 的格式是: [*.] example.com 其中 , example.com叫着 裸域名. (这个example.com/net/org不能被注册, 被保留) ...