小白在线

爱生活 爱小白

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

本文实例讲述了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'

2017630114721727.jpg

«    2023年3月    »
12345
6789101112
13141516171819
20212223242526
2728293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
    文章归档
    网站收藏
    友情链接
    • Z-BlogPHP
    • 订阅本站的 RSS 2.0 新闻聚合

    Powered By Z-BlogPHP 1.7.3

    Copyright iXiaoBai.net Rights Reserved. 吉ICP备18001369号-1 吉公网安备22010202001107