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. Promise链式调用 终止或取消

    Promise回调分两种方法,then成功,catch失败 let promise = new Promise(function(resolve, reject){ resolve('第一次成功') ...

  2. hbase 整合ranger

    一.安装hbase插件 1.解压安装插件 从target目录下拷贝ranger-2.1.0-SNAPSHOT-hbase-plugin.tar.gz到hbase集群,你的这个包的版本可能跟我不一致. ...

  3. SpringDataRedis的简单案例使用

    一.SpringDataRedis环境搭建 第一步.导入坐标 <!-- 缓存 --> <dependency> <groupId>redis.clients< ...

  4. OpenFOAM——高空腔内的湍流自然对流

    本算例来自<ANSYS Fluid Dynamics Verification Manual>中的VMFL052: Turbulent Natural Convection Inside ...

  5. Server 2003 操作系统位数

    安装好电脑系统,如何查看windows 2003/xp/win7是64位还是32位? 方法/步骤 第一种方法:桌面上鼠标右键单击“计算机”(我的电脑) 在弹出的快捷菜单中选择“属性”,如果看到64的字 ...

  6. [Web 安全] WASC 和 OWASP两个web安全方面组织机构介绍

    copy from :  http://blog.sina.com.cn/s/blog_70b7aab9010126mn.html WASC 和 OWASP.这两个组织在呼吁企业加强应用安全意识和指导 ...

  7. 纯Python模式

    http://crcmod.sourceforge.net/intro.html https://help.aliyun.com/document_detail/85288.html OSS的CRC数 ...

  8. springboot自动装配redis在pool下偶尔出现连接异常的问题

    jedis pool的配置其实是采用 org.apache.commons.pool2.impl.GenericObjectPoolConfig类的配置项. jedis 2.9版本代码如下: pack ...

  9. Qt布局 tab-widget-layout

    QHBoxLayout *horizontalLayout_6 = new QHBoxLayout(main_ui.tab_5); horizontalLayout_6->setSpacing( ...

  10. Flutter中管理路由栈的方法和应用

    原文地址:https://www.jianshu.com/p/5df089d360e4 本文首先讲的Flutter中的路由,然后主要讲下Flutter中栈管理的几种方法. 了解下Route和Navig ...