溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在C#中利用checkedListBox1控件連接數據庫

發布時間:2021-04-09 18:02:57 來源:億速云 閱讀:287 作者: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

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女