当前位置:主页 > 软件编程 > .NET代码 >

C#使用checkedListBox1控件链接数据库的方法示例

时间:2020-10-17 23:34:57 | 栏目:.NET代码 | 点击:

本文实例讲述了C#使用checkedListBox1控件链接数据库的方法。分享给大家供大家参考,具体如下:

在数据库中创建三个表: 学生信息表  爱好表   学生爱好表
结果让学生的信息和爱好同时显示到C#窗体上面

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.Sql;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
  class DataDase
  {
    public string Con = "";
    public DataTable DATA_SQL(string SQL)
    {
      SqlConnection myconn = new SqlConnection(Con);
      //Con为数据库连接字段
      SqlDataAdapter myadapter = new SqlDataAdapter(SQL, myconn);
      DataSet mydataset = new DataSet();
      myadapter.Fill(mydataset);
      return mydataset.Tables[0];
    }
    public void RUN_SQL(string SQL)
    {
      SqlConnection myconn = new SqlConnection(Con);
      SqlDataAdapter myadapter = new SqlDataAdapter(SQL, myconn);
      DataSet mydataset = new DataSet();
      myadapter.Fill(mydataset);
    }
  }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    }
    private void button1_Click(object sender, EventArgs e)
    {
      checkedListBox1.Items.Clear();
      DataDase database = new DataDase();
      database.Con = @"Data Source = 192.168.1.3; Initial Catalog = zxl; User Id = sa; Password = zxl; ";
      DataTable dt,t;
      t = database.DATA_SQL("select * from 学生信息表 where c#='" + textBox1.Text + "'");
      textBox2.Text= t.Rows[0][1].ToString();
      if (t.Rows[0][3].ToString().Equals("1"))
      {
        radioButton1.Checked=true;
      }
      else {
        radioButton2.Checked = true;
      }
      dt = database.DATA_SQL("SELECT 爱好表.love FROM 学生信息表 INNER JOIN 学生爱好表 ON 学生信息表.c# = 学生爱好表.c# INNER JOIN 爱好表 ON 学生爱好表.s# = 爱好表.s# WHERE (学生信息表.c# = '"+textBox1.Text+"')");
      for (int i = 0; i < dt.Rows.Count; i++)
      {
        checkedListBox1.Items.Add(dt.Rows[i][0]);
      }
    }
  }
}

create database zxl
use zxl
----------------------------------------------
create table 学生信息表(
c# char(5) primary key,
num char(11) not null,
name char(6) not null,
sex int
)
create table 爱好表(
s# char(5) primary key,
love char(4) not null
)
create table 学生爱好表(
s# char(5) references 爱好表(s#),
c# char(5) references 学生信息表(c#),
code char(10) not null,
primary key(s#,c#)
)
----------------------------------------------
insert into 学生信息表 values('1','111111','zhang',1)
insert into 学生信息表 values('2','222222','wang',0)
insert into 学生信息表 values('3','333333','li',1)
----------------------------------------------
insert into 爱好表 values('11','学习')
insert into 爱好表 values('22','篮球')
insert into 爱好表 values('33','时尚')
insert into 爱好表 values('44','游戏')
insert into 爱好表 values('55','购物')
insert into 爱好表 values('66','其他')
----------------------------------------------
insert into 学生爱好表 values('11','1','code')
insert into 学生爱好表 values('22','1','code')
insert into 学生爱好表 values('33','1','code')
insert into 学生爱好表 values('44','1','code')
insert into 学生爱好表 values('55','1','code')
insert into 学生爱好表 values('66','1','code')
insert into 学生爱好表 values('11','2','code')
insert into 学生爱好表 values('33','2','code')
insert into 学生爱好表 values('11','3','code')
insert into 学生爱好表 values('44','3','code')
----------------------------------------------
select love
from 学生信息表,爱好表,学生爱好表
where 学生爱好表.c# = 学生信息表.c# and 学生爱好表.s# = 爱好表.s# and 学生信息表.c#='3'

更多关于C#相关内容感兴趣的读者可查看本站专题:《C#常见控件用法教程》、《C#窗体操作技巧汇总》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结

希望本文所述对大家C#程序设计有所帮助。

您可能感兴趣的文章:

相关文章