50100506 数据访问(3)

输入向数据库中添加或删除数据、、检查主外键

一、向数据库中添加数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication8
{
class Class1
{
public const string CONNECTIONSTRING = "server=MA-PC;database=mydb;uid=sa;pwd=111111";
public static bool Check(string columnName, string value)
{
if (columnName =="sex")
{
if (value =="0"||value =="1")
{
return true;
}
else
{
Console.WriteLine("输入的性别错误");
return false;
}
}
else if (columnName =="birthday")
{
try
{
Convert.ToDateTime(value);
return true;
}
catch
{
Console.WriteLine("生日格式不正确!");
return false;
}
}
else
{
return false;
}
} //判断格式
public static bool CheckPK(string pk) //检查主键
{
bool notHasPK = true;
SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
conn.Open();

SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select*from info where code=‘"+pk+"‘";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows )
{
notHasPK = false;
Console.WriteLine("主键存在");
}
else
{
notHasPK = true;
}

conn.Close();
return notHasPK;
}
public static bool HasNation(string nationCode) //检查民族代号是否输入正确
{
bool hasNation = true;
SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
conn.Open();

SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from nation where code=‘" + nationCode + "‘";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
hasNation = true;
}
else
{
hasNation = false;
Console.WriteLine("民族代号输入不正确");
}
conn.Close();
return hasNation;
}
public static void AddInfo(string code,string name, string sex, string nation,string birthday) //添加数据
{
SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
conn.Open();

SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into info value(‘"+code +"‘,‘"+name +"‘,‘"+sex+"‘,‘"+nation +"‘,‘"+birthday+"‘)";
cmd.ExecuteNonQuery();

conn.Close();
}
public static void Main01(string[] args)
{
//输入
Console.Write("代号:");
string code= Console.ReadLine();
Console.Write("姓名:");
string name = Console.ReadLine();
Console.Write("性别(0,1):");
string sex = Console.ReadLine();
Console.Write("民族:");
string nation = Console.ReadLine();
Console.Write("生日:");
string birthday = Console.ReadLine();
Console.ReadLine();

//检查
bool isok = CheckInput(code,sex,nation,birthday);
//插入
if (isok==true )
{
AddInfo(code,name,sex ,nation ,birthday);
}
Console.ReadLine();
}
private static bool CheckInput(string code,string sex,string nation,string birthday)
{
//1.对输入的数据格式进行检查
bool isOK = true;
bool isOK1 = Check("sex", sex);
bool isOK2 = Check("birthday", birthday);

//2.对主外键进行检查
bool isOK3 = CheckPK(code);
bool isOK4 = HasNation(nation);

isOK = isOK && isOK1 && isOK2 && isOK3 && isOK4;
return isOK;
} ///检查
}
}

二、从数据库中删除数据  并显示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Class2
{
public const string CONNECTIONSTRING = "server=.;database=mydb;uid=sa;pwd=123";
static string GetNationName(string code)
{
string str = "";

//根据民族代号查询民族名称
SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
conn.Open();

SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from nation where code=‘" + code + "‘ ";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
str = dr["Name"].ToString();
}
else
{
str = "未知";
}

conn.Close();

return str;
}
//显示所有人员信息
public static void Show()
{
//显示
SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
conn.Open();

SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from info";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string code = dr["code"].ToString();
string name = dr["name"].ToString();
string sex = ((bool)dr["Sex"]) ? "男" : "女";
string nation = GetNationName(dr["nation"].ToString());
string birthday = ((DateTime)dr["birthday"]).ToString("yyyy年MM月dd日");

Console.WriteLine(code + "\t" + name + "\t" + sex + "\t" + nation + "\t" + birthday);
}

conn.Close();
}
//检查主键是否存在
public static bool CheckPK(string pk)
{
bool hasPK = true;
SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
conn.Open();

SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from info where code=‘" + pk + "‘";
SqlDataReader dr = cmd.ExecuteReader();
hasPK = dr.HasRows;
conn.Close();

return hasPK;
}
public static void DeleteInfo(string pk)
{
SqlConnection conn = new SqlConnection(CONNECTIONSTRING);
conn.Open();

SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "delete from family where InfoCode=‘" + pk + "‘";
cmd.ExecuteNonQuery();
cmd.CommandText = "delete from work where InfoCode=‘" + pk + "‘";
cmd.ExecuteNonQuery();
cmd.CommandText = "delete from info where code=‘" + pk + "‘";
cmd.ExecuteNonQuery();

conn.Close();
}
public static void Main00(string[] args)
{
//显示
Show();
//输入要删除的人员代号
Console.Write("输入要删除的人员:");
string code = Console.ReadLine();
//删除
//看看有没有这个代号的人员,如果没有就提示代号输入错误。
bool pkIsOK = CheckPK(code);
if (pkIsOK == true)
{
//执行删除
DeleteInfo(code);
Console.WriteLine("删除成功");
}
else
{
Console.WriteLine("找不到要删除的人员代号,删除失败。请重新检查要删除的人员代号。");
}

//显示
Show();
}
}
}

时间: 2024-08-30 11:07:27

50100506 数据访问(3)的相关文章

重要!!!实体类、数据访问类

创建两个类: users类: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 实体类_数据访问类.App_Code { public class Users { private int _Ids; /// <summary> /// ids /// </summary> public int Ids { get { return _Ids;

.NET Framework 2.0中的数据访问新特性

1. 异步数据访问 ? 支持异步数据编程 ? SqlConnection – BeginOpen – EndOpen ? SqlCommand – BeginExecuteNonQuery – BeginExecuteReader – BeginExecuteXmlReader – EndExecuteNonQuery – EndExecuteReader – EndExecuteXmlReader 2. 多活动结果集(MARS) ? 在 SQL Server 2005 中支持多活动结果集 ?

ADO.NET数据访问技术概览

1. 以数据为中心的应用程序设计 1.1. ADO.NET 支持下列的存储类型? 无结构? 有结构, 无层次的数据 – 以逗号分离的数据(CSV) 文件.Microsoft Excel 电子表格.Microsoft Exchange 文件.Active Directory 文件等? 有层次的数据 – XML 文档等? 关系型数据库 – SQL Server.Oracle.Access 等. 1.2. 连接环境? 连接环境是指用户在这种环境下始终保持与数据源的连接? 优点 – 环境易于实施安全控制

实体类、数据访问类中的属性拓展

类中: using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; namespace 实体类_数据访问类.App_Code { public class Users { SqlConnection conn = null; SqlCommand cmd = null; public Users() { conn = new S

Spring数据访问之JdbcTemplate

Spring数据访问之JdbcTemplate 使用JdbcTemplate的基本操作步骤 1.引jar包 项目的基本架构 这里重点看实现类的内容 1 package cn.hmy.dao.impl; 2 3 4 import java.util.List; 5 6 import org.springframework.jdbc.core.support.JdbcDaoSupport; 7 8 import cn.hmy.beans.Emp; 9 import cn.hmy.dao.IEmpDa

Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问

本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这部分内容需要以下Jar包支持 mysql-connector:MySQL数据库连接驱动,架起服务端与数据库沟通的桥梁: MyBatis:一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架: log4j:Apache的开源项目,一个功能强大的日志组件,提供方便的日志记录: 修改后的pom.xm

PHP-------PDO:数据访问抽象层

PDO:数据访问抽象层 它是用来做数据访问的,和数据库之间连接,执行一些SQL语句 这种方式比之前的,Mysqli的方式功能更大一些 用一张图来说明: 人为写了一条SQL语句,是通过Mysqli的对象(封装好的类),通过Mysql驱动,然后在操作Mysql数据库.这是以前的方式. 如果这条SQL语句,想访问另外一个数据库,不是Mysql了,想用一下Oracle Call数据库或者SQL Server数据库,根据逻辑,应该还要有一个类是专门操作Oracle Call 数据库的类 同一张图来表示:

TP数据访问

重点学习了: 1,ThinkPHP查询数据 2.ThinkPHP添加数据 LianXiController.class.php 1 <?php 2 namespace Home\Controller; 3 use Think\Controller; 4 class LianXiController extends Controller 5 { 6 //添加数据 7 function tj() 8 { 9 10 /* 11 //方法1:(数组) 12 //添加的数组必须为关联数组,key必须为字段

php数据访问数据层

数据访问pdo抽象层 方法一:比较简单点: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta htt