【C#】上机实验九
1. 设计一个Windows登陆窗体应用程序,能够实现根据现有表中数据模拟登陆,并设置相关属性,具体界面如下。
可能使用到的类:
(1)SqlConnection
(2)SqlCommand
(3)SqlDataReader
(4)MessageBox
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 Myform
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void label1_Click(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{ string connString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C#09J20172202899何海钊\Myform\Myform\Properties\Database1.mdf;Integrated Security=True";
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataReader reader = null;
try
{
conn = new SqlConnection(connString);
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = "Select username , password From Table1 where username = @username and password = @password ";
cmd.Parameters.AddWithValue("@username", textBox1.Text.Trim());
cmd.Parameters.AddWithValue("@password", textBox2.Text.Trim());
reader = cmd.ExecuteReader();
if (reader.Read())
{
MessageBox.Show("登录成功", "OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("用户名密码不正确", "NO", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Open Error", MessageBoxButtons.OK);
}
finally
{
if (reader != null) reader.Close();
if (conn.State == ConnectionState.Open) conn.Close();
}
}
}
}
Code1
2、设计一个Windows登陆窗体应用程序,根据下图设置相关属性,该程序能够读写现有表中数据,具体界面如下。
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 Myform2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataReader reader = null;
private SqlDataAdapter adapter = null;
private DataSet List = null; private string connString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C#09J20172202899何海钊\db\Database1.mdf;Integrated Security=True;Connect Timeout=30"; private void button1_Click(object sender, EventArgs e)
{
try
{
conn = new SqlConnection(connString);
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = "Insert into Table1 ( username , password ) values (@username , @password ) ";
cmd.Parameters.AddWithValue("@username", textBox1.Text.Trim());
cmd.Parameters.AddWithValue("@password", textBox2.Text.Trim());
int n = cmd.ExecuteNonQuery();
if ( n > )
{
MessageBox.Show("成功插入", "Insert OK ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("插入失败", "Insert error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Open Error", MessageBoxButtons.OK);
}
finally
{
if (reader != null) reader.Close();
if (conn.State == ConnectionState.Open) conn.Close();
}
} private void button2_Click(object sender, EventArgs e)
{
try
{
conn = new SqlConnection(connString);
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = "Select * From Table1 "; adapter = new SqlDataAdapter(cmd); List = new DataSet(); adapter.Fill(List);
DataTable table = List.Tables[]; DataRowCollection Rows = table.Rows; for (int i = ; i < Rows.Count; i++)
{
DataRow row = Rows[i];
string username = (string)row["username"];
string password = (string)row["password"]; this.richTextBox1.Text += username + " " + password + "\r\n";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Open Error", MessageBoxButtons.OK);
}
finally
{
if (reader != null) reader.Close();
if (conn.State == ConnectionState.Open) conn.Close();
}
}
}
}
Code2
3、设计一个Windows窗体应用程序,根据下图设置相关属性,该程序能够读取AdventureWorksDW_Data.mdf数据库中DimReseller表中数据,具体界面如下。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient; namespace FormPage
{
public partial class Form1 : Form
{ private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataAdapter adapter = null;
private SqlDataReader reader = null;
private DataSet ds = null; private int pageSize = ;
private int rowCount = ;
private int pageNum = ;
private string connectString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C#09J20172202899何海钊\MyForm3\AdventureWorksDW2008\AdventureWorksDW_Data.mdf;Integrated Security=True;Connect Timeout=30"; public Form1()
{
InitializeComponent();
Count_Row();
Fill_Data();
} private void btn_FirstPage_Click(object sender, EventArgs e)
{
pageNum = ;
Fill_Data();
} private void btn_LastPage_Click(object sender, EventArgs e)
{
pageNum = rowCount/pageSize;
Fill_Data();
} private void btn_PrePage_Click(object sender, EventArgs e)
{
if( pageNum > )
{
pageNum--;
Fill_Data();
}
} private void btn_NextPage_Click(object sender, EventArgs e)
{
if (pageNum < rowCount / pageSize)
{
pageNum++;
Fill_Data();
} }
private void btn_Fill_Click(object sender, EventArgs e)
{
Fill_Data();
}
void Fill_Data()
{
if (pageNum == )
{
btn_FirstPage.Enabled = false;
btn_PrePage.Enabled = false;
}
else
{
btn_FirstPage.Enabled = true;
btn_PrePage.Enabled = true;
} if( pageNum == rowCount / pageSize)
{
btn_LastPage.Enabled = false;
btn_NextPage.Enabled = false;
}
else
{
btn_LastPage.Enabled = true;
btn_NextPage.Enabled = true;
}
try
{
conn = new SqlConnection(connectString);
conn.Open(); cmd = conn.CreateCommand();
cmd.CommandText = "Select top ("+pageSize+ ") Employeekey , FirstName , LastName , EmailAddress , Phone from DimEmployee where EmployeeKey not in( Select top (" + pageSize *pageNum+ ") EmployeeKey from DimEmployee order by EmployeeKey ) order by EmployeeKey"; ds = new DataSet();
adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds , "DimEmployee" ); dataGridView1.DataSource = ds;
dataGridView1.DataMember = "DimEmployee"; }
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Fill Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
} void Count_Row()
{
try
{
conn = new SqlConnection(connectString);
conn.Open(); cmd = conn.CreateCommand();
cmd.CommandText = "Select count(*) from DimEmployee"; reader = cmd.ExecuteReader();
if( reader.Read())
{
rowCount = reader.GetInt32();
}
else
{
MessageBox.Show("Search Error", "0 row", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (reader.IsClosed)
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Search Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
if( reader.IsClosed)
reader.Close();
}
}
}
}
Code3
【C#】上机实验九的更多相关文章
- Linux基础入门(新版)(实验九-实验十二)
实验九 简单文本入门 一.常用的文本处理命令 二.文本处理命令 1.tr 命令 tr 命令可以用来删除一段文本信息中的某些文字.或者将其进行转换. 使用方式: tr [option]...SET1 [ ...
- lingo运筹学上机实验指导
<运筹学上机实验指导>分为两个部分,第一部分12学时,是与运筹学理论课上机同步配套的4个实验(线性规划.灵敏度分析.运输问题与指派问题.最短路问题和背包问题)的Excel.LONGO和LI ...
- 算法课上机实验(一个简单的GUI排序算法比较程序)
(在家里的电脑上Linux Deepin截的图,屏幕大一点的话,deepin用着还挺不错的说) 这个应该是大二的算法课程上机实验时做的一个小程序,也是我的第一个GUI小程序,实现什么的都记不清了,只记 ...
- 2017-2018-2 20155228 《网络对抗技术》 实验九:Web安全基础
2017-2018-2 20155228 <网络对抗技术> 实验九:Web安全基础 1. 实践内容 1.1 标理解常用网络攻击技术的基本原理 1.2 在Webgoat实验环境下实践相关实验 ...
- 实验九 ZStack 广播通信实验
实验九 ZStack 广播通信实验[实验目的]1. 了解 ZigBee 广播通信的原理2. 掌握在 ZigBee 网络中进行广播通信的方法[实验设备]1. 装有 IAR 开发工具的 PC 机一台2. ...
- 2017-2018-2 20155225《网络对抗技术》实验九 Web安全基础
2017-2018-2 20155225<网络对抗技术>实验九 Web安全基础 WebGoat 1.String SQL Injection 题目是想办法得到数据库所有人的信用卡号,用Sm ...
- 20155201 网络攻防技术 实验九 Web安全基础
20155201 网络攻防技术 实验九 Web安全基础 一.实践内容 本实践的目标理解常用网络攻击技术的基本原理.Webgoat实践下相关实验. 二.报告内容: 1. 基础问题回答 1)SQL注入攻击 ...
- Java第一次上机实验源代码
小学生计算题: package 第一次上机实验_; import java.util.*; public class 小学计算题 { public static void main(String[] ...
- oracle上机实验内容
这是oracle实验的部分代码,我花了一中午做的. 第一次上机内容 实验目的:熟悉ORACLE11G的环境 实验内容: 第二次上机内容 实验目标:掌握oracle体系结构,掌握sqlplus的运行环境 ...
随机推荐
- 防火墙firewalld
增加外部可访问的端口 启动: systemctl start firewalld 查看状态: systemctl status firewalld 停止: systemctl stop firewal ...
- Android入门教程(五)
关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 欢迎大家关注我的微信公众号:「醉翁猫咪」 字面量: 1.整数字面量为整型(int) 2.小数字面量为双精度浮点型(double) ...
- 【洛谷】P5024 保卫王国 (倍增)
前言 传送门 很多人写了题解了,我就懒得写了,推荐一篇博客 那就分享一下我的理解吧(说得好像有人看一样 对于每个点都只有选与不选两种情况,所以直接用倍增预处理出来两种情况的子树之内,子树之外的最值,最 ...
- SDN初体验(软件定义网络实验一)
作业说明 本次实验步骤2.3是在机房环境下完成的,步骤1.4是在自己笔记本上重新配置完成的,所以环境.用户名什么的会略有差别. 1. 安装轻量级网络仿真工具Mininet 为了节约课程时间,实验室机房 ...
- mvn pom文件引用顺序关系
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 第10组 Alpha冲刺(6/6)
链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 恩泽(组长) 过去两天完成了哪些任务 描述 tomcat的学习与实现 服务器后端部署,API接口的beta版实现 后端代码 ...
- Spark2.x(六十):在Structured Streaming流处理中是如何查找kafka的DataSourceProvider?
本章节根据源代码分析Spark Structured Streaming(Spark2.4)在进行DataSourceProvider查找的流程,首先,我们看下读取流数据源kafka的代码: Spar ...
- mysql死锁(锁与事务)
线上某服务时不时报出如下异常(大约一天二十多次):“Deadlock found when trying to get lock;”. Oh, My God! 是死锁问题.尽管报错不多,对性能目前看来 ...
- vue中使用极验验证码(附demo)
前言: vue中使用极验验证码,最好是在页面渲染的时候(mounted)进行验证码的初始化,然后在初始化回调中绑定触发弹出验证码的事件.这样在点击按钮或者进行特定操作时能够快速的弹出验证码. 关键代码 ...
- Python3基础 函数 __name__ 得到引用所指向的真正名字
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...