6w学习笔记
vc控件的输入输出
单选按钮
当单击 RadioButton 控件时,其 Checked 属性设置为 true,并且调用 Click 事件处理程序。当 Checked 属性的值更改时,将引发 CheckedChanged 事件。如果 AutoCheck 属性设置为 true(默认值),则当选择单选按钮时,将自动清除该组中的所有其他单选按钮
输入
this.rdb_in.Checked = (bool)sqlDataReader["inorout"];
this.rdb_out.Checked = !(bool)sqlDataReader["inorout"];
输出
sqlConnection.Open();
if(this.rdb_Male.Checked==true)
string str = "update tb_book set inorout=1 where No=09187756";
else
string str = "update tb_book set inorout=0 where No=09187756";
日历控件
输入 this.dtp_BirthDate.Value(DateTime)sqlDataReader["publishDate"];
输出
sqlCommand1.Parameters.AddWithValue("@date", this.data.tostring);
文本框
输入this.txb_No.Text = sqlDataReader["No"].ToString();
输出sqlCommand1.Parameters.AddWithValue("@No", client.No);
网格视图控件 DataGridView
下拉列表控件ComboBox
输入 this.cmb_stock.SelectedValue = (int)sqlDataReader["stockNo"];
this.cmb_stock.DataSource = stockTable;
this.cmb_stock.DisplayMember = "Name"; this.cmb_stock.ValueMember = "No";输出sqlCommand1.Parameters.AddWithValue("@stockNo",this.cmb_stock.SelectedValue.toString());
示例
建库语句
USE master;IF DB_ID('EduBaseDemo') IS NOT NULLBEGINALTER DATABASE EduBaseDemoSET SINGLE_USERWITH ROLLBACK IMMEDIATE;DROP DATABASE EduBaseDemo;ENDCREATE DATABASE EduBaseDemoON(NAME='Datafile',FILENAME='C:\DataFile.mdf')LOG ON(NAME='Logfile',FILENAME='C:\Logfile.ldf');GOUSE EduBaseDemo;--创建表;----班级表CREATE TABLE tb_stock(NoINTNOT NULLPRIMARY KEY,NameVARCHAR(20)NOT NULL)INSERT tb_stock(No,Name)VALUES(1,'A馆'),(2,'B馆'),(3,'C馆'),(4,'D馆');----学生表;CREATE TABLE tb_book(NoCHAR(10)NOT NULLPRIMARY KEY,NameVARCHAR(20)NOT NULL,inoroutBITNOT NULL,publishDateDATENOT NULL,stockNoINTNOT NULL,SpecialityVARCHAR(100)NULL);INSERT tb_book(No,Name,inorout,publishDate,stockNo,Speciality)VALUES('09187756','内科学',1,'1991-10-15',2,'收藏'); select * from tb_bookselect * from tb_stock
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.Data.SqlClient; //包含访问SQL Server所需的各类对象;namespace lizi6w{ public partial class frm_booktInfo : Form { ////// 公有方法:构造函数; /// public frm_bookInfo() { InitializeComponent(); this.StartPosition = FormStartPosition.CenterScreen; //本窗体启动位置设为屏幕中央; } ////// 私有方法:点击载入按钮; /// /// /// private void btn_Load_Click(object sender, EventArgs e) { SqlConnection sqlConnection = new SqlConnection(); sqlConnection.ConnectionString = "Server=(local);Database=EduBaseDemo;Integrated Security=sspi"; SqlCommand sqlCommand = new SqlCommand(); SqlCommand sqlCommand2 = new SqlCommand(); sqlCommand.Connection = sqlConnection; sqlCommand2.Connection = sqlConnection; sqlCommand.CommandText = "SELECT * FROM tb_stock;"; sqlCommand2.CommandText = "SELECT * FROM tb_book WHERE No=@No;"; sqlCommand2.Parameters.AddWithValue("@No", "09187756"); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); sqlDataAdapter.SelectCommand = sqlCommand; DataTable classTable = new DataTable(); sqlConnection.Open(); sqlDataAdapter.Fill(classTable); this.cmb_stock.DataSource = classTable; this.cmb_stock.DisplayMember = "Name"; this.cmb_stock.ValueMember = "No"; SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader(); if (sqlDataReader.Read()) { this.txb_No.Text = sqlDataReader["No"].ToString(); this.txb_Name.Text = sqlDataReader["Name"].ToString(); this.rdb_inorout.Checked = (bool)sqlDataReader["inorout"]; this.rdb_inorout.Checked = !(bool)sqlDataReader["inorout"]; this.dtp_publishDate.Value = (DateTime)sqlDataReader["publishDate"]; this.cmb_stock.SelectedValue = (int)sqlDataReader["stockNo"]; this.txb_Speciality.Text = sqlDataReader["Speciality"].ToString(); } sqlDataReader.Close(); //关闭数据阅读器(同时关闭连接); } } }
例子2DataGridView
namespace 药品信息管理系统{ public partial class frm_ClientTable : Form { public frm_ClientTable() { InitializeComponent(); this.StartPosition = FormStartPosition.CenterScreen; } private void frm_ClientTable_Load(object sender, EventArgs e) { } private void btn_Load_Click(object sender, EventArgs e) { using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString)) { SqlCommand sqlCommand = new SqlCommand(); //声明并实例化SQL命令; sqlCommand.Connection = sqlConnection; //将SQL命令的连接属性指向SQL连接; sqlCommand.CommandText = "SELECT * FROM tb_Client;"; //指定SQL命令的命令文本;该命令查询所有学生; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); //声明并实例化SQL数据适配器; sqlDataAdapter.SelectCommand = sqlCommand; //将SQL数据适配器的查询命令属性指向SQL命令; DataTable ClientTable = new DataTable(); //声明并实例化数据表,用于保存所有学生,以用作数据网格视图的数据源; sqlConnection.Open(); //打开SQL连接; sqlDataAdapter.Fill(ClientTable); //SQL数据适配器读取数据,并填充学生数据表; sqlConnection.Close(); //关闭SQL连接; this.dgv_Client.Columns.Clear(); //数据网格视图的列集合清空; this.dgv_Client.DataSource = ClientTable; //将数据网格视图的数据源设为学生数据表; this.dgv_Client.Columns["CNo"].HeaderText = "客户编号"; //将数据网格视图的指定列的表头文本设为中文; this.dgv_Client.Columns["CName"].HeaderText = "客户名称"; this.dgv_Client.Columns["CAddress"].HeaderText = "地址"; this.dgv_Client.Columns["CTel"].HeaderText = "联系电话"; this.dgv_Client.Columns["CName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; this.dgv_Client.Columns[this.dgv_Client.Columns.Count - 1].AutoSizeMode = //数据网格视图的最后一列的自动调整列宽模式设为填充(至数据网格视图右侧边缘); DataGridViewAutoSizeColumnMode.Fill; } } private void btn_Update_Click(object sender, EventArgs e) { using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString)) { SqlCommand sqlCommand = new SqlCommand(); //声明并实例化SQL命令; sqlCommand.Connection = sqlConnection; //将SQL命令的连接属性指向SQL连接; sqlCommand.CommandText = //指定SQL命令的命令文本; "UPDATE tb_Client" + " SET CName=@CName,CAddress=@CAddress,CTel=@CTel" + " WHERE CNo=@CNo;"; sqlCommand.Parameters.Add("@CName", SqlDbType.VarChar, 0, "CName"); //向SQL命令的参数集合添加参数的名称、SQL Server数据类型、长度(仅用于定长类型)、所绑定的数据表中的列名; sqlCommand.Parameters.Add("@CAddress", SqlDbType.VarChar, 0, "CAddress"); sqlCommand.Parameters.Add("@CTel", SqlDbType.VarChar, 0, "CTel"); sqlCommand.Parameters.Add("@CNo", SqlDbType.Char, 10, "CNo"); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令; sqlDataAdapter.UpdateCommand = sqlCommand; //将SQL数据适配器的更新命令属性指向SQL命令; DataTable ClientTable = (DataTable)this.dgv_Client.DataSource; //声明数据表,并指向数据网格视图的数据源;数据源默认类型为object,还需强制转换类型; sqlConnection.Open(); //打开SQL连接; int rowAffected = sqlDataAdapter.Update(ClientTable); sqlConnection.Close(); if (rowAffected > 0) { MessageBox.Show("更新" + rowAffected.ToString() + "行。"); } else { MessageBox.Show("not ok"); } //SQL数据适配器根据学生数据表提交更新,并返回受影响行数; } } private void btn_Add_Click(object sender, EventArgs e) { //未完成 using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString)) { SqlCommand sqlCommand = new SqlCommand(); //声明并实例化SQL命令; sqlCommand.Connection = sqlConnection; //将SQL命令的连接属性指向SQL连接; sqlCommand.CommandText = //指定SQL命令的命令文本; "insert tb_Client (CNo,CName,CTel,CAddress)values" + "(@CNo,@CName,@CTel,@CAddress)"; sqlCommand.Parameters.Add("@CName", SqlDbType.VarChar, 0, "CName"); //向SQL命令的参数集合添加参数的名称、SQL Server数据类型、长度(仅用于定长类型)、所绑定的数据表中的列名; sqlCommand.Parameters.Add("@CAddress", SqlDbType.VarChar, 0, "CAddress"); sqlCommand.Parameters.Add("@CTel", SqlDbType.VarChar, 0, "CTel"); sqlCommand.Parameters.Add("@CNo", SqlDbType.Char, 10, "CNo"); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令; sqlDataAdapter.UpdateCommand = sqlCommand; //将SQL数据适配器的更新命令属性指向SQL命令; DataTable ClientTable = (DataTable)this.dgv_Client.DataSource; //声明数据表,并指向数据网格视图的数据源;数据源默认类型为object,还需强制转换类型; sqlConnection.Open(); //打开SQL连接; int i = sqlCommand.ExecuteNonQuery(); if (i == 1) { MessageBox.Show("OK"); } else MessageBox.Show("not OK"); //int rowAffected = sqlDataAdapter.Update(ClientTable); sqlConnection.Close(); MessageBox.Show("添加" + i.ToString() + "行。"); //SQL数据适配器根据学生数据表提交更新,并返回受影响行数; } } private void btn_Delete_Click(object sender, EventArgs e) { //未完成 using (SqlConnection sqlConnection = new SqlConnection(DBHelper.connString)) { SqlCommand sqlCommand = new SqlCommand(); //声明并实例化SQL命令; sqlCommand.Connection = sqlConnection; //将SQL命令的连接属性指向SQL连接; sqlCommand.CommandText = //指定SQL命令的命令文本; "delete from tb_Client" + " WHERE CName='"+this.dgv_Client .CurrentRow .Cells [1].ToString ()+"'";; //sqlCommand.Parameters.Add("@CName", SqlDbType.VarChar, 0, "CName"); //向SQL命令的参数集合添加参数的名称、SQL Server数据类型、长度(仅用于定长类型)、所绑定的数据表中的列名; //sqlCommand.Parameters.Add("@CAddress", SqlDbType.VarChar, 0, "CAddress"); //sqlCommand.Parameters.Add("@CTel", SqlDbType.VarChar, 0, "CTel"); //sqlCommand.Parameters.Add("@CNo", SqlDbType.Char, 10, "CNo"); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令; sqlDataAdapter.UpdateCommand = sqlCommand; //将SQL数据适配器的更新命令属性指向SQL命令; DataTable ClientTable = (DataTable)this.dgv_Client.DataSource; //声明数据表,并指向数据网格视图的数据源;数据源默认类型为object,还需强制转换类型; sqlConnection.Open(); //打开SQL连接; int i = sqlCommand.ExecuteNonQuery(); if (i >0) { MessageBox.Show("OK"); } else MessageBox.Show("not OK"); //int rowAffected = sqlDataAdapter.Update(ClientTable); sqlConnection.Close(); MessageBox.Show("删除" + i.ToString() + "行。"); //SQL数据适配器根据学生数据表提交更新,并返回受影响行数; } } }}