c#五个小程序_第1页
c#五个小程序_第2页
c#五个小程序_第3页
c#五个小程序_第4页
c#五个小程序_第5页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

界面 一 界面 一 1 练习目的 熟悉 lable butten textbox radiobutten 等控件的使用 2 程序代码 private void button1 Click object sender EventArgs e if DX Checked true this textBox4 Text 姓名 this textBox1 Text r n 性别 this textBox2 Text r n 学号 this textBox3 Text r n 爱好 this DX Text if ZH Checked true this textBox4 Text 姓名 this textBox1 Text r n 性别 this textBox2 Text r n 学号 this textBox3 Text r n 爱好 this ZH Text if KC Checked true this textBox4 Text 姓名 this textBox1 Text r n 性别 this textBox2 Text r n 学号 this textBox3 Text r n 爱好 this KC Text 3 程序界面 4 运行结果 界面 二 1 练习目的 熟悉 toolStripMenuItem panel 等窗体的功能及使用方法 学会演出新窗体的 代码 以及退出窗体的代码 2 代码 private void button1 Click object sender EventArgs e Form2 form new Form2 form Show 第一个窗体不隐藏 this close 第一个窗口隐藏 private void button2 Click object sender EventArgs e this Close 3 窗体设计 4 实验结果 窗口传值 1 练习目的 学会在两个窗口之间进行传值的代码 并熟练运用 2 代码 Form1 private void button1 Click object sender EventArgs e if DX Checked true this textBox4 Text 姓名 this textBox1 Text r n 性别 this textBox2 Text r n 学号 this textBox3 Text r n 爱好 this DX Text if ZH Checked true this textBox4 Text 姓名 this textBox1 Text r n 性别 this textBox2 Text r n 学号 this textBox3 Text r n 爱好 this ZH Text if KC Checked true this textBox4 Text 姓名 this textBox1 Text r n 性别 this textBox2 Text r n 学号 this textBox3 Text r n 爱好 this KC Text private void textBox1 TextChanged object sender EventArgs e private void button2 Click object sender EventArgs e Form2 f2 new Form2 f2 ShowDialog this Form2 public partial class Form2 Form public Form2 InitializeComponent private void button1 Click object sender EventArgs e Form1 f1 Form1 this Owner this textBox1 Text f1 textBox4 Text 3 窗体设计 4 运行结果 数据库 一 一一 实验目的 学习使用 c 进行可视化数据库保存 查询 二二 实验步骤 1 建立项目 DataBase 2 在解决方案资源管理器中 Database 上点右键 选择添加 新建项 3 在弹出的窗口中选择数据 基于服务的数据库 点击添加 然后点击完成 4 在菜单栏视图中打开服务器资源管理器 在 表 上点击右键 选择添加新 表 5 按下图新建一个表并在行前点击右键来设置主键 点击保存 并输入一个表 名 6 在刚才新建的表上点击右键 选择显示表数据 7 在表中输入如下的数据并保存 8 点击菜单数据 显示数据源 在 database1dataset 上点击右键选择使用向导 配置数据集 选中表 点击完成 9 点击菜单生成 生成解决方案 会在工具箱中生成新的控件 在解决方案资 源管理器中生成链接字符串 10 在界面上添加一个 datagridview 控件和两个 button 一个是保存 一个是 查询 11 添加命名空间引用 using System Data SqlClient 12 给 form1 类添加两个变量 SqlDataAdapter adapter DataTable table 13 给 Form1 添加一个 Form1 Load 事件 事件中添加代码如下 string connStr Properties Settings Default MyDatabaseConnectionString SqlConnection conn new SqlConnection connStr adapter new SqlDataAdapter select from MyTable conn SqlCommandBuilder builder new SqlCommandBuilder adapter adapter InsertCommand builder GetInsertCommand adapter DeleteCommand builder GetDeleteCommand adapter UpdateCommand builder GetUpdateCommand table new DataTable adapter Fill table dataGridView1 DataSource table 14 给按钮查询添加一个click事件响应函数 添加如下代码 string varNumber textBoxNumber Text Trim if varNumber MessageBox Show 请输入你要查询的学号 提示信息 MessageBoxButtons OK MessageBoxIcon Warning try string connStr Properties Settings Default MyDatabaseConnectionString SqlConnection conn new SqlConnection connStr adapter new SqlDataAdapter select from MyTable where学号 varNumber conn SqlCommandBuilder builder new SqlCommandBuilder adapter adapter InsertCommand builder GetInsertCommand adapter DeleteCommand builder GetDeleteCommand adapter UpdateCommand builder GetUpdateCommand table new DataTable adapter Fill table dataGridView1 DataSource table conn Close catch Exception ee MessageBox Show ee Message 提示信息 MessageBoxButtons OK MessageBoxIcon Warning 15 给保存按钮添加一个click事件响应函数 添加如下代码 textBoxNumber Text dataGridView1 EndEdit try adapter Update table MessageBox Show 保存成功 catch Exception ee MessageBox Show ee Message 保存失败 MyLoad 三 实验结果 1 可输入信息状态 2 保存 3 3 查询保存结果查询保存结果 数据库 二 数据库 二 1 实训目的 1 了解 ADO NET 结构 2 了解 ADO NET 的组件 3 使用 Command 对象和 Connection 对象 2 功能要求 使用 Visual Studio 实现简单注册和登录功能 SQL Server 作为后台数据库 程序运行界面如图 1 注册 和图 2 登录 所示 图 1 程序运行效果 注册 图 2 程序运行效果 登录 程序的功能要求如下 1 在 SQL Server 中创建数据库 表结构 并输入测试数据 2 按照图 1 设计程序窗体界面 并完成以下功能 1 新建两个窗体 在窗体界面上添加并设置控件 如图 1 和图 2 所示 2 点击 注册 即可添加一条数据并显示在列表中 3 点击 登录 如果登录成功则使用 MessageBox 中的方法显示成功 的登录用户名 3 数据库设计 数据库名称 RegistAndLogin 数据表名 Users 字段显示字段名数据类型字段大小备注和说明 编号 userIdint 主键 自增 1 姓名 userNamevarchar30 不允许空 密码 userPwdvarchar20 邮箱 userEmailvarchar50 电话 userPhoneint 4 实现注册 1 使用 SqlCommand 对象将数据添加到数据库中 当点击注册按钮后需要 给予用户提示 注册成功 或 注册失败 如图 3 用户注册 2 密码输入框内输入密码要为 图 3 用户注册 5 实现登录 1 使用 DataAdapter 对象将数据填充到 DataSet 中并返回 DataSet 判断 DataSet 是否存在数据 2 当用户单击登录时 如果输入的用户名和密码错误则弹出提示 用户登 录失败 用户输入用户名和密码正确则将记录集中的用户名读出来 弹 出如图 3 用户登录界面提示 3 密码输入框内输入密码要为 图 3 用户登录 6 主要代码与程序截图 登录登录 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 namespace LoginAndRegister public partial class login Form public login InitializeComponent private void button1 Click object sender EventArgs e if this username Text this password Text MessageBox Show 请输入用户名和密码 else SqlConnection con new SqlConnection 创建连接 con ConnectionString server local database RegistAndLogin uid sa pwd 123456 con Open SqlCommand cmd con CreateCommand cmd CommandText select from Users SqlDataReader dr cmd ExecuteReader if dr Read string power dr 0 ToString index main new index main Tag this FindForm this Visible false main ShowDialog this Close else MessageBox Show 用户名或密码错误 con Close private void button2 Click object sender EventArgs e register register new register register Show 注册注册 using System using System Collections Generic using System ComponentModel using System Data using System Drawing using System Linq using S

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论