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. redash oracle 数据源docker 镜像

    redash 官方的docker 镜像是没有包含oracle的,需要我们自己添加,参考了一个docker 镜像进行了简单的修改 Dockerfile FROM redash/redash:7.0.0. ...

  2. Xamarin 自定义OnKeyDown 再按一次退出程序的实现

    private DateTime? lastBackKeyDownTime; public override bool OnKeyDown(Keycode keyCode, KeyEvent e) { ...

  3. 洛谷 P4071 [SDOI2016]排列计数 题解

    P4071 [SDOI2016]排列计数 题目描述 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳 ...

  4. Noip2019暑期训练2

      题目名称 骑士遍历 和谐俱乐部 农场派对 对称二叉树 存盘文件名 knight Beautiful party tree 输入文件名 knight.in Beautiful.in party.in ...

  5. element ui 中的时间选择器怎么设置默认值/el-date-picker区间选择器怎么这是默认值

    template代码 <el-date-picker value-format="yyyy-MM-dd" v-model="search.date" ty ...

  6. c++与matcom混合编程

    #include #include #include "matlib.h" using namespace std; int main() { initM(MATCOM_VERSI ...

  7. CTF PHP反序列化

    目录 php反序列化 一.序列化 二.魔术方法 1.构造函数和析构函数 2.__sleep()和__wakeup() 3.__toString() 4.__set(), __get(), __isse ...

  8. 大数据应用期末总评——Hadoop综合大作业

    作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3339 Hadoop综合大作业 要求: 1.将爬虫大作业产生的csv文件 ...

  9. 循环(for,while,until)与循环控制符(break,continue)

    一.for循环 第一种风格   for ((;;;))(类似C语言风格) do command done 例子:for ((i=0;i<10;i++)) do echo $i done 第二种风 ...

  10. Spark2.x(五十五):在spark structured streaming下sink file(parquet,csv等),正常运行一段时间后:清理掉checkpoint,重新启动app,无法sink记录(file)到hdfs。

    场景: 在spark structured streaming读取kafka上的topic,然后将统计结果写入到hdfs,hdfs保存目录按照month,day,hour进行分区: 1)程序放到spa ...