C#使用checkedListBox1控件鏈接數(shù)據(jù)庫的方法示例
本文實(shí)例講述了C#使用checkedListBox1控件鏈接數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:
在數(shù)據(jù)庫中創(chuàng)建三個(gè)表: 學(xué)生信息表 愛好表 學(xué)生愛好表
結(jié)果讓學(xué)生的信息和愛好同時(shí)顯示到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為數(shù)據(jù)庫連接字段 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 學(xué)生信息表 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 學(xué)生信息表 INNER JOIN 學(xué)生愛好表 ON 學(xué)生信息表.c# = 學(xué)生愛好表.c# INNER JOIN 愛好表 ON 學(xué)生愛好表.s# = 愛好表.s# WHERE (學(xué)生信息表.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 學(xué)生信息表( 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 學(xué)生愛好表( s# char(5) references 愛好表(s#), c# char(5) references 學(xué)生信息表(c#), code char(10) not null, primary key(s#,c#) ) ---------------------------------------------- insert into 學(xué)生信息表 values('1','111111','zhang',1) insert into 學(xué)生信息表 values('2','222222','wang',0) insert into 學(xué)生信息表 values('3','333333','li',1) ---------------------------------------------- insert into 愛好表 values('11','學(xué)習(xí)') insert into 愛好表 values('22','籃球') insert into 愛好表 values('33','時(shí)尚') insert into 愛好表 values('44','游戲') insert into 愛好表 values('55','購物') insert into 愛好表 values('66','其他') ---------------------------------------------- insert into 學(xué)生愛好表 values('11','1','code') insert into 學(xué)生愛好表 values('22','1','code') insert into 學(xué)生愛好表 values('33','1','code') insert into 學(xué)生愛好表 values('44','1','code') insert into 學(xué)生愛好表 values('55','1','code') insert into 學(xué)生愛好表 values('66','1','code') insert into 學(xué)生愛好表 values('11','2','code') insert into 學(xué)生愛好表 values('33','2','code') insert into 學(xué)生愛好表 values('11','3','code') insert into 學(xué)生愛好表 values('44','3','code') ---------------------------------------------- select love from 學(xué)生信息表,愛好表,學(xué)生愛好表 where 學(xué)生愛好表.c# = 學(xué)生信息表.c# and 學(xué)生愛好表.s# = 愛好表.s# and 學(xué)生信息表.c#='3'
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#常見控件用法教程》、《C#窗體操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》
希望本文所述對大家C#程序設(shè)計(jì)有所幫助。
- C#中CheckedListBox控件的用法實(shí)例
- C# Winfom 中ListBox的簡單用法詳解
- C# ListBox中的Item拖拽代碼分享
- C# CheckedListBox控件的用法總結(jié)
- C#實(shí)現(xiàn)讓ListBox適應(yīng)最大Item寬度的方法
- C#讀取文本文件到listbox組件的方法
- C#保存listbox中數(shù)據(jù)到文本文件的方法
- C#(WinForm) ComboBox和ListBox添加項(xiàng)及設(shè)置默認(rèn)選擇項(xiàng)
- C#入門教程之ListBox控件使用方法
- c#使用listbox的詳細(xì)方法和常見問題解決
相關(guān)文章
C# MVC 使用LayUI實(shí)現(xiàn)下拉框二級聯(lián)動(dòng)的功能
這篇文章主要介紹了C# MVC 如何使用LayUI實(shí)現(xiàn)下拉框二級聯(lián)動(dòng),文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06C#中一個(gè)高性能異步socket封裝庫的實(shí)現(xiàn)思路分享
下面小編就為大家分享一篇C#中一個(gè)高性能異步socket封裝庫的實(shí)現(xiàn)思路,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11winform實(shí)現(xiàn)拖動(dòng)文件到窗體上的方法
這篇文章主要介紹了winform實(shí)現(xiàn)拖動(dòng)文件到窗體上的方法,以實(shí)例分析了C#中WinForm操作窗體及文件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09WPF ProgressBar實(shí)現(xiàn)實(shí)時(shí)進(jìn)度效果
這篇文章主要介紹了WPF ProgressBar實(shí)現(xiàn)實(shí)時(shí)進(jìn)度效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12C#程序中創(chuàng)建、復(fù)制、移動(dòng)、刪除文件或文件夾的示例
這篇文章主要介紹了C#程序中創(chuàng)建、復(fù)制、移動(dòng)、刪除文件或文件夾的示例,即對System.IO命名空間中類的運(yùn)用,需要的朋友可以參考下2016-02-02C# 利用Aspose.Words.dll將 Word 轉(zhuǎn)成PDF
關(guān)于word轉(zhuǎn)成pdf的方法網(wǎng)上有很多。大部分需要借助office 2007及以上版本的組件。安裝配置起來比較麻煩。今天偶然得之“Aspose.Words.dll”可以實(shí)現(xiàn)2013-08-08