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#】上机实验九的更多相关文章

  1. Linux基础入门(新版)(实验九-实验十二)

    实验九 简单文本入门 一.常用的文本处理命令 二.文本处理命令 1.tr 命令 tr 命令可以用来删除一段文本信息中的某些文字.或者将其进行转换. 使用方式: tr [option]...SET1 [ ...

  2. lingo运筹学上机实验指导

    <运筹学上机实验指导>分为两个部分,第一部分12学时,是与运筹学理论课上机同步配套的4个实验(线性规划.灵敏度分析.运输问题与指派问题.最短路问题和背包问题)的Excel.LONGO和LI ...

  3. 算法课上机实验(一个简单的GUI排序算法比较程序)

    (在家里的电脑上Linux Deepin截的图,屏幕大一点的话,deepin用着还挺不错的说) 这个应该是大二的算法课程上机实验时做的一个小程序,也是我的第一个GUI小程序,实现什么的都记不清了,只记 ...

  4. 2017-2018-2 20155228 《网络对抗技术》 实验九:Web安全基础

    2017-2018-2 20155228 <网络对抗技术> 实验九:Web安全基础 1. 实践内容 1.1 标理解常用网络攻击技术的基本原理 1.2 在Webgoat实验环境下实践相关实验 ...

  5. 实验九 ZStack 广播通信实验

    实验九 ZStack 广播通信实验[实验目的]1. 了解 ZigBee 广播通信的原理2. 掌握在 ZigBee 网络中进行广播通信的方法[实验设备]1. 装有 IAR 开发工具的 PC 机一台2.  ...

  6. 2017-2018-2 20155225《网络对抗技术》实验九 Web安全基础

    2017-2018-2 20155225<网络对抗技术>实验九 Web安全基础 WebGoat 1.String SQL Injection 题目是想办法得到数据库所有人的信用卡号,用Sm ...

  7. 20155201 网络攻防技术 实验九 Web安全基础

    20155201 网络攻防技术 实验九 Web安全基础 一.实践内容 本实践的目标理解常用网络攻击技术的基本原理.Webgoat实践下相关实验. 二.报告内容: 1. 基础问题回答 1)SQL注入攻击 ...

  8. Java第一次上机实验源代码

    小学生计算题: package 第一次上机实验_; import java.util.*; public class 小学计算题 { public static void main(String[] ...

  9. oracle上机实验内容

    这是oracle实验的部分代码,我花了一中午做的. 第一次上机内容 实验目的:熟悉ORACLE11G的环境 实验内容: 第二次上机内容 实验目标:掌握oracle体系结构,掌握sqlplus的运行环境 ...

随机推荐

  1. java web项目改装exe安装版

    https://blog.csdn.net/rico_zhou/article/details/79868129java简单程序打包成exe https://blog.csdn.net/rico_zh ...

  2. [RN] React Native 关闭所有黄色警告

    [RN] React Native 关闭所有黄色警告 console.ignoredYellowBox = ['Warning: BackAndroid is deprecated. Please u ...

  3. hadoop jps 出现空指针错误

    在hadoop中安装jdk软件以后出现如下问题: 错误描述 [xxx@localhost jdk1..0_181]$ ./bin/jps Exception in thread "main& ...

  4. fluent将出口温度赋值给入口

    Fluent版本:Fluent18.2 首先我们启动Fluent 然后按照正常的流程进行模型缩放,材料的设置,边界条件的设置等等,然后初始化. 在完成了算例的初始化以后 (define (OutToI ...

  5. devops 运维平台相关知识

    1.https://choerodon.io/zh/community/ (代码 https://github.com/choerodon/choerodon) 猪齿鱼 2.https://www.o ...

  6. java 优秀开源项目

    一.https://github.com/zhangdaiscott/jeecg-boot 简介:一款基于代码生成器的JAVA快速开发平台!全新架构前后端分离:SpringBoot 2.x,Ant D ...

  7. hadoop 综合大作业

    作业要求来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3339 本次作业是在期中大作业的基础上利用hadoop和hive技术进行 ...

  8. python练习:寒冰猴子狐狸,猫狗咬架

    python练习:寒冰猴子狐狸,猫狗咬架 一,寒冰猴子狐狸 class Person: def __init__(self, na, gen, age, fig): self.name = na se ...

  9. 使用肘部法确定k-means均值的k值

    import numpy as np from sklearn.cluster import KMeans from scipy.spatial.distance import cdist impor ...

  10. 【优化技巧】指数移动平均EMA的原理

    前言 在深度学习中,经常会使用EMA(exponential moving average)方法对模型的参数做平滑或者平均,以求提高测试指标,增加模型鲁棒性. 参考 1. [优化技巧]指数移动平均(E ...