选择题

1.下列ASP.NET语句(B)正确地创建了一个与mySQL数据库和服务器的连接。

A.SqlConnection con1 = new Connection(“Data Source = localhost; Integrated Security = SSPI; Initial Catalog = myDB”);

B. MySqlConnection con1 = new MySqlConnection (“Server=localhost;Database = myDB;Uid=root;Pwd=;”);

C.SqlConnection con1 = new SqlConnection(Data Source = localhost; Integrated Security = SSPI; Initial Catalog = myDB);

D.SqlConnection con1 = new OleDbConnection(“Data Source = localhost; Integrated Security = SSPI; Initial Catalog = myDB”);

2.在ADO.NET中,对于MySqlCommand对象的ExecuteNonQuery()方法和MySqlDataAdapter对象的Fill ()方法,下面叙述错误的是(C

A.insert、update、delete等操作的mySql语句主要用ExecuteNonQuery()方法来执行;

B.ExecuteNonQuery()方法返回执行mySql语句所影响的行数。

C.Select操作的mySql语句只能由Fill ()方法来执行;

D.Fill ()方法返回一个DataSet对象;

简述并举例说明

什么是ADO.NET?

在NET编程环境中优先使用的数据访问接口

什么是连接字符串?

连接字符串,是一组被格式化的键值对:

它告诉ADO.NET数据源在哪里

需要什么样的数据格式

提供什么样的访问信任级别

以及其他任何包括连接的相关信息。

connection对象的作用?

Connection(连接对象):与数据源建立连接。

commmand对象的作用?

Command(命令对象):对数据源执行SQL命令并返回结果。

dataAdapter对象的作用?

DataAdapter(适配器对象):对数据源执行操作并返回结果,在DataSet与数据源之间建立通信,将数据源中的数据写入

DataReader对象的作用?

DataReader(数据流对象):取数据源的数据,只允许对将数据源以只读、顺向的方式查看其中所存储的数据。其常用于检索大量数据,DataReader对象还是一种非常节省资源的数据对象

DataSet对象的作用?

DataSet(数据集对象):内存中的数据库,是数据表的集合,它可以包含任意多个数据表。

程序设计

//字段
private string _account;
private string _name;
private string _usertype;
private string _password;
private string _vip;
private string _garde;
private string _amount;
private string _age;
private string _hobby;
private string _department;
private const string CONSTR = "Server=127.0.0.1;Database=studentsinfo;UserId=root;Password=qq2686485465;";
//属性
public string Account { get => _account; set => _account = value; }
public string Name { get => _name; set => _name = value; }
public string Usertype { get => _usertype; set => _usertype = value; }
public string Password { get => _password; set => _password = value; }
public string Vip { get => _vip; set => _vip = value; }
public string Garde { get => _garde; set => _garde = value; }
public string Amount { get => _amount; set => _amount = value; }
public string Age { get => _age; set => _age = value; }
public string Hobby { get => _hobby; set => _hobby = value; }
public string Department { get => _department; set => _department = value; }
  1. 已知本机上的MySql数据库studentsinfo中有学生表.请给HandleStudent类添加成员函数:public bool HandleLogin((string studentAccount, string studentPwd) 该方法可检验学生登录时的信息是否有效。

    提示:学生表中字段类型自定,但需满足数据的合理性。

    //验证登录
    /// <summary>
    /// 登录验证
    /// </summary>
    /// <param name="studentAccount">账号</param>
    /// <param name="studentPwd">密码</param>
    /// <returns>Boolean 验证成功返回真,否则返回假</returns>
    public static bool HandleLogin(string studentAccount, string studentPwd)
    {
    //对参数进行处理验证
    if (Equals(studentAccount.Trim(), "") || Equals(studentPwd.Trim(), "")) return false;
    //连接数据库
    MySqlConnection con = HandleConnection();
    //打开数据库
    con.Open();
    //参数化拼接字符串
    string sql = "Select*from students where `account` = @Account AND `password` = @Password";
    //创建命令对象
    MySqlCommand cmd = new MySqlCommand(sql, con);
    //指定参数
    cmd.Parameters.Add(new MySqlParameter("@Account",studentAccount));
    cmd.Parameters.Add(new MySqlParameter("@Password", studentPwd));
    //执行
    MySqlDataReader dr = cmd.ExecuteReader();
    //判断
    if (dr.Read())
    {
    con.Close();
    return true;
    }
    else
    {
    con.Close();
    return false;
    } }
  2. 已知本机上的MySql数据库studentsinfo中有学生表students,请给HandleStudent类添加成员函数public static int HandleAddStudents<T>(HandleStudent student) 该方法可添加一位学生的信息到数据表中。

    提示:学生表中字段类型自定,但需满足数据的合理性。

  /// <summary>
/// 根据主键判断 是否存在学生
/// </summary>
/// <param name="key"></param>
/// <returns>boolea 存在返回真,不存在返回假</returns>
public static bool HandleStudentIsIn(string key)
{
//对参数进行处理验证
if (Equals(key.Trim(), "")) return false;
//连接数据库
MySqlConnection con = HandleConnection();
//打开数据库
con.Open();
//参数化拼接字符串
string sql = "Select*from students where `account` = @Account";
//创建命令对象
MySqlCommand cmd = new MySqlCommand(sql, con);
//指定参数
cmd.Parameters.Add(new MySqlParameter("@Account", key));
//执行
MySqlDataReader dr = cmd.ExecuteReader();
//判断
if (dr.Read())
{
con.Close();
return true;
}
else
{
con.Close();
return false;
}
}
 /// <summary>
/// 添加学生
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="student"></param>
/// <returns>int型 操作影响的行数</returns>
public static int HandleAddStudents<T>(HandleStudent student)
{
MySqlConnection con = HandleConnection();
con.Open();
//判断学生是否存在,信息是否完善
if (HandleStudentIsIn(student.Account) || Equals(student.Account,"") || Equals(student.Name, "" ) || Equals(student.Password, "") || Equals(student.Usertype, "")) return 0;
//添加学生信息
string sql = "INSERT INTO students(`account`,`name`,`usertype`,`password`,`vip`,`garde`,`amount`,`age`,`hobby`,`department`)" +
"VALUES" +
"(@Account,@Name,@UserType,@Password,1,0,0,0,'IT','OMTPC')";
MySqlCommand cmd = new MySqlCommand(sql, con);
cmd.Parameters.Add(new MySqlParameter("@Account",student.Account));
cmd.Parameters.Add(new MySqlParameter("@Name", student.Name));
cmd.Parameters.Add(new MySqlParameter("@UserType", student.Usertype));
cmd.Parameters.Add(new MySqlParameter("@Password", student.Password));
int rows = cmd.ExecuteNonQuery();
con.Close();
return rows;
}

项目完整代码

Form1.cs 点击查看代码
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; namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (HandleStudent.HandleLogin("202171031022", "1234566789"))
{
MessageBox.Show("登录成功!");
}
else
{
MessageBox.Show("登录失败!");
}
} private void button2_Click(object sender, EventArgs e)
{
if (HandleStudent.HandleLogin("000000000", "000000000"))
{
MessageBox.Show("登录成功!");
}
else
{
MessageBox.Show("登录失败!");
}
} private void button3_Click(object sender, EventArgs e)
{
Random seed = new Random();
string xuehao = seed.Next(100, 999).ToString()+ seed.Next(1000, 9999).ToString() + seed.Next(100, 999).ToString() + seed.Next(10, 99).ToString();
string mima = seed.Next(100, 999).ToString() + seed.Next(1000, 9999).ToString() + seed.Next(100, 999).ToString() + seed.Next(10, 99).ToString();
string dianhua = seed.Next(100, 999).ToString() + seed.Next(100, 999).ToString() + seed.Next(100, 999).ToString() + seed.Next(10, 99).ToString();
HandleStudent pep = new HandleStudent(xuehao, mima,"张三",dianhua);
MessageBox.Show("该操作影响行数为:"+HandleStudent.HandleAddStudents<HandleStudent>(pep).ToString());
} private void button4_Click(object sender, EventArgs e)
{
Random seed = new Random();
string xuehao = seed.Next(100, 999).ToString() + seed.Next(1000, 9999).ToString() + seed.Next(100, 999).ToString() + seed.Next(10, 99).ToString();
string mima = seed.Next(100, 999).ToString() + seed.Next(1000, 9999).ToString() + seed.Next(100, 999).ToString() + seed.Next(10, 99).ToString();
string dianhua = seed.Next(100, 999).ToString() + seed.Next(100, 999).ToString() + seed.Next(100, 999).ToString() + seed.Next(10, 99).ToString();
HandleStudent pep = new HandleStudent("", "", "", "");
MessageBox.Show("该操作影响行数为:" + HandleStudent.HandleAddStudents<HandleStudent>(pep).ToString());
}
}
}
HandleStudent.cs - 点击查看代码
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace WindowsFormsApp1
{
class HandleStudent
{
//字段
private string _account;
private string _name;
private string _usertype;
private string _password;
private string _vip;
private string _garde;
private string _amount;
private string _age;
private string _hobby;
private string _department;
private const string CONSTR = "Server=127.0.0.1;Database=studentsinfo;UserId=root;Password=qq2686485465;";
//属性
public string Account { get => _account; set => _account = value; }
public string Name { get => _name; set => _name = value; }
public string Usertype { get => _usertype; set => _usertype = value; }
public string Password { get => _password; set => _password = value; }
public string Vip { get => _vip; set => _vip = value; }
public string Garde { get => _garde; set => _garde = value; }
public string Amount { get => _amount; set => _amount = value; }
public string Age { get => _age; set => _age = value; }
public string Hobby { get => _hobby; set => _hobby = value; }
public string Department { get => _department; set => _department = value; } //构造函数
public HandleStudent() { }
public HandleStudent(string studentAccount, string studentPasswd)
{
_account = studentAccount;
_password = studentPasswd;
}
public HandleStudent(string studentAccount, string studentPasswd, string stdentName, string studentTel)
{
_account = studentAccount;
_password = studentPasswd;
_name = stdentName;
_usertype = studentTel;
}
//操作方法
/// <summary>
/// 连接数据库
/// </summary>
/// <returns>MySqlConnection 数据库连接对象</returns>
public static MySqlConnection HandleConnection() {
MySqlConnection con = new MySqlConnection(CONSTR);
return con;
}
/// <summary>
/// 根据主键判断 是否存在学生
/// </summary>
/// <param name="key"></param>
/// <returns>boolea 存在返回真,不存在返回假</returns>
public static bool HandleStudentIsIn(string key)
{
//对参数进行处理验证
if (Equals(key.Trim(), "")) return false;
//连接数据库
MySqlConnection con = HandleConnection();
//打开数据库
con.Open();
//参数化拼接字符串
string sql = "Select*from students where `account` = @Account";
//创建命令对象
MySqlCommand cmd = new MySqlCommand(sql, con);
//指定参数
cmd.Parameters.Add(new MySqlParameter("@Account", key));
//执行
MySqlDataReader dr = cmd.ExecuteReader();
//判断
if (dr.Read())
{
con.Close();
return true;
}
else
{
con.Close();
return false;
}
}
/// <summary>
/// 登录验证
/// </summary>
/// <param name="studentAccount">账号</param>
/// <param name="studentPwd">密码</param>
/// <returns>Boolean 验证成功返回真,否则返回假</returns>
public static bool HandleLogin(string studentAccount, string studentPwd)
{
//对参数进行处理验证
if (Equals(studentAccount.Trim(), "") || Equals(studentPwd.Trim(), "")) return false;
//连接数据库
MySqlConnection con = HandleConnection();
//打开数据库
con.Open();
//参数化拼接字符串
string sql = "Select*from students where `account` = @Account AND `password` = @Password";
//创建命令对象
MySqlCommand cmd = new MySqlCommand(sql, con);
//指定参数
cmd.Parameters.Add(new MySqlParameter("@Account",studentAccount));
cmd.Parameters.Add(new MySqlParameter("@Password", studentPwd));
//执行
MySqlDataReader dr = cmd.ExecuteReader();
//判断
if (dr.Read())
{
con.Close();
return true;
}
else
{
con.Close();
return false;
} }
/// <summary>
/// 添加学生
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="student"></param>
/// <returns>int型 操作影响的行数</returns>
public static int HandleAddStudents<T>(HandleStudent student)
{
MySqlConnection con = HandleConnection();
con.Open();
//判断学生是否存在,信息是否完善
if (HandleStudentIsIn(student.Account) || Equals(student.Account,"") || Equals(student.Name, "" ) || Equals(student.Password, "") || Equals(student.Usertype, "")) return 0;
//添加学生信息
string sql = "INSERT INTO students(`account`,`name`,`usertype`,`password`,`vip`,`garde`,`amount`,`age`,`hobby`,`department`)" +
"VALUES" +
"(@Account,@Name,@UserType,@Password,1,0,0,0,'IT','OMTPC')";
MySqlCommand cmd = new MySqlCommand(sql, con);
cmd.Parameters.Add(new MySqlParameter("@Account",student.Account));
cmd.Parameters.Add(new MySqlParameter("@Name", student.Name));
cmd.Parameters.Add(new MySqlParameter("@UserType", student.Usertype));
cmd.Parameters.Add(new MySqlParameter("@Password", student.Password));
int rows = cmd.ExecuteNonQuery();
con.Close();
return rows;
} }
}

【C#】【平时作业】习题-11-ADO.NET的更多相关文章

  1. Linux系统管理----目录与文件管理作业习题

    chapter02 - 03 作业 1.  分别用cat \tac\nl三个命令查看文件/etc/ssh/sshd_config文件中的内容,并用自己的话总计出这三个文档操作命令的不同之处? cat ...

  2. 【笨办法学Python】习题11:打印出改变了的输入

    print "How old are you?", age = raw_input() print "How tall are you?", height = ...

  3. Linux系统管理----账号与权限管理作业习题

    1.创建/guanli 目录,在/guanli下创建zonghe 和 jishu 两个目录(一条命令) 创建目录:mkdir +目录 [root@localhost chen]#mkdir /guan ...

  4. Linux系统管理----LVM逻辑卷和磁盘配额作业习题

    1.为主机增加80G SCSI 接口硬盘 2.划分三个各20G的主分区 [root@localhost chen]# fdisk /dev/sdb 命令(输入 m 获取帮助):n Partition ...

  5. python作业习题集锦

    1. 登录作业: 写一个登录程序,登录成功之后,提示XXX欢迎登录,登录失败次数是3次,要校验一下输入为空的情况 for i in range(3): username=input('username ...

  6. 平时作业七 Java

    以下是几本计算机书籍的基本信息编号 书名 价格 出版社1 JAVA基础 32 清华大学出版社2 JAVA WEB开发 40 电子工业出版社3 面向对象程序设计 28 清华大学出版社4 Struts开发 ...

  7. 平时作业六 java

    编写一个Java应用程序,使用Java的输入输出流技术将Input.txt的内容(Input.txt为文本文件)逐行读出,每读出一行就顺序为其添加行号(从1开始,逐行递增),并写入到另一个文本文件Ou ...

  8. 平时作业五 Java

    使用I/O流和文件对象实现目录备份功能.用户指定源目录.目标目录以及备份文件类型(如果是任意文件使用通配符*号),通过此程序可将源目录及其所有子目录下的指定类型文件保存到目标目录. package c ...

  9. Java 平时作业四

    编写一个Java程序实现返回指定目录及其子目录下扩展名为*.pdf的所有文件名. 扩展: isFile public boolean isFile() 测试此抽象路径名表示的文件是否为普通文件. 如果 ...

  10. [物理学与PDEs]第2章习题11 Lagrange 形式的一维理想流体力学方程组在强间断线上的间断连接条件

    对由第 10 题给出的 Lagrange 形式的一维理想流体力学方程组, 给出解在强间断线上应满足的间断连接条件 (假设体积力 $F\equiv 0$). 解答: $$\beex \bea \sez{ ...

随机推荐

  1. git 设置代理和取消代理

    1.设置代理 git config --global http.proxy "127.0.0.1:1080" 2.取消代理 git config --global --unset ...

  2. 2024年9月中国数据库排行榜:openGauss系多点开花,根社区优势明显

    在墨天轮发布的9月中国数据库流行度排行榜中,中国数据库产业格局进一步聚集刷新,呈现出3大显著特征: 开源势力力争上游显优势领先潮流: openGauss 开源根社区优势明显: 阿里华为两极鼎立云上云下 ...

  3. 2024年1月中国数据库排行榜: OPOT 组合续写贺新年,达梦、腾讯发力迎升势

    2024年开局,墨天轮中国数据库流行度排行火热出炉,292个国产数据库齐聚榜单.整体来看,榜单前十整体变化不大,"O-P-O"格局稳固,前五位名次未发生变动.但新年伊始,各家数据库 ...

  4. Notepad--特色功能:拷贝另存为

    Notepad--特色功能:拷贝另存为 你是否纠结如下的使用场景: 正在编辑的文件,还没有想好,保存担心把原文件给覆盖了. 使用"另存为"后当前编辑界面的文档又变成新的文件了,可是 ...

  5. 云原生周刊:Argo Rollouts 支持 Kubernetes Gateway API 1.0 | 2024.7.1

    开源项目 Kubetools Recommender System Kubetools Recommender System (Krs) 是一个基于 GenAI 的工具,用于帮助管理和优化 Kuber ...

  6. k8s 中的 Gateway API 的背景和简介【k8s 系列之四】

    〇.Gateway API 的背景 第一阶段:Service 初始的 Kubernetes 内部服务向外暴露,使用的是自身的 LoadBlancer 和 NodePort 类型的 Service. 在 ...

  7. 5道大厂的JAVA经典面试题

    前言 本来想着给自己放松一下,刷刷博客,慕然回首,Java的四种引用,强弱软虚?泛型常用特点?Java创建对象有几种方式? 有没有可能两个不相等的对象有相同的hashcode?深拷贝和浅拷贝的区别是什 ...

  8. 初识GO语言--基本数据类型

  9. 2024/9/16 CSP-S模拟赛试题

    A 这题是很有意思的一个题,思路就是你考虑kt的位置只可能在四个角,因为这种情况下,他的距离才会最远对吧,所以你就暴力找另一个人fengwu的点的位置,然后计算他们之间的距离然后你求一个\(\max\ ...

  10. php在大并发下redis锁实现

    在现如今电商盛行的时期,会出现很多促销活动,最为常见的就是秒杀.在秒杀系统中最为常见的问题就是会出现超卖的情况,那么如何来杜绝超卖的情形了,在业务逻辑层面可以使用缓存以及加锁的手法来避免超卖的情形. ...