温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在C#中利用checkedListBox1控件连接数据库

发布时间:2021-04-09 18:02:57 来源:亿速云 阅读:249 作者:Leah 栏目:编程语言

这篇文章给大家介绍怎么在C#中利用checkedListBox1控件连接数据库,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

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#中利用checkedListBox1控件连接数据库

关于怎么在C#中利用checkedListBox1控件连接数据库就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI