C#使用checkedListBox1控件鏈接數(shù)據(jù)庫的方法示例
本文實例講述了C#使用checkedListBox1控件鏈接數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:
在數(shù)據(jù)庫中創(chuàng)建三個表: 學生信息表 愛好表 學生愛好表
結果讓學生的信息和愛好同時顯示到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 學生信息表 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#數(shù)據(jù)結構與算法教程》、《C#面向對象程序設計入門教程》及《C#程序設計之線程使用技巧總結》
希望本文所述對大家C#程序設計有所幫助。
欄 目:C#教程
下一篇:C#文件操作類分享
本文標題:C#使用checkedListBox1控件鏈接數(shù)據(jù)庫的方法示例
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5605.html
您可能感興趣的文章
- 01-10C#使用Dispose模式實現(xiàn)手動對資源的釋放
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10C#使用windows服務開啟應用程序的方法
- 01-10c# ArrayList的使用方法小總結
- 01-10C#使用ADO.Net部件來訪問Access數(shù)據(jù)庫的方法
- 01-10C#使用Mutex簡單實現(xiàn)程序單實例運行的方法
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
- 01-10C#實現(xiàn)ComboBox控件顯示出多個數(shù)據(jù)源屬性的方法
- 01-10C#實現(xiàn)讀取DataSet數(shù)據(jù)并顯示在ListView控件中的方法
- 01-10C#中yield用法使用說明


閱讀排行
本欄相關
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關于ASP網頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文