using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient; namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//创建数据库连接对象,并编写连接字符串,注意连接字符串不要写错
SqlConnection conn = new SqlConnection("server=.;database=Data0928;user=sa;pwd=123");
//创建数据库操作对象,创建过程是与刚创建的连接对象匹配起来
SqlCommand cmd = conn.CreateCommand();
for (; ; )
{
Console.Write("请输入你想要的操作序号(1、删除 2、添加 3、更改 4、查看)");
try
{ int code = int.Parse(Console.ReadLine());
if (code == )
{
//删除
Console.Write("请输入想要删除的用户名:");
string user = Console.ReadLine();
//编写操作语句 TSQL语句
cmd.CommandText = "select ids,UserName,PassWord,NickName,Sex,Birthday,(select NationName from Nation where NationCode=Users.Nation) as 'Nation' ,ClassName as 'Class' from Users join Class on Class.ClassCode=Users.Class where UserName='" + user + "'";
//数据库连接打开,准备执行操作
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();//使用SqlDataReader类接受查询的数据
if (dr.HasRows)//判断dr里是否有数据 返回布尔类型
{
while (dr.Read())//循环每一行 当超出时返回false
{
Console.WriteLine(dr["ids"] + "\t" + dr["UserName"] + "\t" + dr["PassWord"] + "\t" + dr["NickName"] + "\t" + (Convert.ToBoolean(dr["Sex"]) ? "男" : "女") + "\t" + Convert.ToDateTime(dr["Birthday"]).ToString("yyyy年MM月dd日") + "\t" + dr["Nation"] + "\t" + dr["Class"]);
}
//关闭数据库连接
conn.Close();
Console.Write("是否确定删除此条数据?(Y/N)");
string yn = Console.ReadLine();
if (yn == "y")
{
cmd.CommandText = "delete from Users where UserName='" + user + "'";
conn.Open();
int i = cmd.ExecuteNonQuery();//执行操作,并记录受影响的行数
if (i > )
Console.WriteLine("删除成功。");
else
{
Console.WriteLine("删除失败");
}
conn.Close();
}
else if (yn == "n")
{
Console.WriteLine("取消删除。");
}
else
{
Console.WriteLine("输入有误!");
}
}
else
{
Console.WriteLine("查无此条数据!");
}
conn.Close();
}
else if (code == )
{
//添加
Console.Write("请输入想要添加的用户名:");
string uname = Console.ReadLine();
cmd.CommandText = "select * from Users";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
int biao = ;
//查询用户名是否重复
if (dr.HasRows)
{
while (dr.Read())
{
if (dr["UserName"].ToString() == uname)
{
biao++;
break;
}
}
}
conn.Close();
if (biao == )//用户名无重复
{
string pwd;
for (; ; )
{
Console.Write("请输入密码(6到18位):");
string pwd1 = Console.ReadLine();
if (pwd1.Length >= && pwd1.Length <= )
{
pwd = pwd1;
break;
}
else
{
Console.WriteLine("密码长度不正确,请重新输入!");
continue;
}
}
Console.Write("请输入昵称:");
string nick = Console.ReadLine();
string sex;
for (; ; )
{
Console.Write("请输入性别(请输入男女或者0、1):");
string sex1 = Console.ReadLine();
if(sex1==""||sex1=="")
{
sex = sex1;
break;
}
else if (sex1 == "男" || sex1 == "女")
{
if (sex1 == "男")
{
sex = "";
break;
}
else
{
sex = "";
break;
}
}
else
{
Console.WriteLine("性别输入有误,请重新输入!");
continue;
}
}
string bir;
for (; ; )
{
Console.Write("请输入生日:");
try
{
DateTime dt = DateTime.Parse(Console.ReadLine());
bir = dt.ToString();
break;
}
catch
{
Console.WriteLine("生日日期输入有误,请重新输入!");
continue;
}
}
string nation;
for (; ; )
{
Console.Write("请输入民族:");
string nation1 = Console.ReadLine();
if(nation1=="汉族"||nation1=="汉")
{
nation = "N001";
break;
}
else if (nation1 == "满族" || nation1 == "满")
{
nation = "N002";
break;
}
else if (nation1 == "藏族" || nation1 == "藏")
{
nation = "N003";
break;
}
else if (nation1 == "彝族" || nation1 == "彝")
{
nation = "N004";
break;
}
else
{
Console.WriteLine("输入民族有误,请重新输入!");
continue;
}
}
string cla;
for (; ; )
{
Console.Write("请输入班级:");
string cla1 = Console.ReadLine();
if(cla1=="一班"||cla1=="一")
{
cla = "C001";
break;
}
else if (cla1 == "二班" || cla1 == "二")
{
cla = "C002";
break;
}
else if (cla1 == "三班" || cla1 == "三")
{
cla = "C003";
break;
}
else if (cla1 == "四班" || cla1 == "四")
{
cla = "C004";
break;
}
else
{
Console.WriteLine("输入班级有误,请重新输入!");
continue;
}
}
cmd.CommandText = "insert into Users values('" + uname + "','" + pwd + "','" + nick + "','" + sex + "','" + bir + "','" + nation + "','" + cla + "')";
conn.Open();
int s = cmd.ExecuteNonQuery();
if (s > )
{
Console.WriteLine("添加成功!");
}
else
{
Console.WriteLine("添加失败!");
}
conn.Close();
}
else//用户名重复
{
Console.WriteLine("该用户名已存在!");
}
}
else if(code==)
{
//更改
Console.Write("请输入想要更改的用户名:");
string user = Console.ReadLine();
cmd.CommandText = "select ids,UserName,PassWord,NickName,Sex,Birthday,(select NationName from Nation where NationCode=Users.Nation) as 'Nation' ,ClassName as 'Class' from Users join Class on Class.ClassCode=Users.Class where UserName='" + user + "'";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Console.WriteLine(dr["ids"] + "\t" + dr["UserName"] + "\t" + dr["PassWord"] + "\t" + dr["NickName"] + "\t" + (Convert.ToBoolean(dr["Sex"]) ? "男" : "女") + "\t" + Convert.ToDateTime(dr["Birthday"]).ToString("yyyy年MM月dd日") + "\t" + dr["Nation"] + "\t" + dr["Class"]);
}
conn.Close();
Console.Write("是否更改此条数据?(Y/N)");
string yn = Console.ReadLine();
if (yn == "y")
{
string pwd;
for (; ; )
{
Console.Write("请输入密码(6到18位):");
string pwd1 = Console.ReadLine();
if (pwd1.Length >= && pwd1.Length <= )
{
pwd = pwd1;
break;
}
else
{
Console.WriteLine("密码长度不正确,请重新输入!");
continue;
}
}
Console.Write("请输入昵称:");
string nick = Console.ReadLine();
string sex;
for (; ; )
{
Console.Write("请输入性别(请输入男女或者0、1):");
string sex1 = Console.ReadLine();
if (sex1 == "" || sex1 == "")
{
sex = sex1;
break;
}
else if (sex1 == "男" || sex1 == "女")
{
if (sex1 == "男")
{
sex = "";
break;
}
else
{
sex = "";
break;
}
}
else
{
Console.WriteLine("性别输入有误,请重新输入!");
continue;
}
}
string bir;
for (; ; )
{
Console.Write("请输入生日:");
try
{
DateTime dt = DateTime.Parse(Console.ReadLine());
bir = dt.ToString();
break;
}
catch
{
Console.WriteLine("生日日期输入有误,请重新输入!");
continue;
}
}
string nation;
for (; ; )
{
Console.Write("请输入民族:");
string nation1 = Console.ReadLine();
if (nation1 == "汉族" || nation1 == "汉")
{
nation = "N001";
break;
}
else if (nation1 == "满族" || nation1 == "满")
{
nation = "N002";
break;
}
else if (nation1 == "藏族" || nation1 == "藏")
{
nation = "N003";
break;
}
else if (nation1 == "彝族" || nation1 == "彝")
{
nation = "N004";
break;
}
else
{
Console.WriteLine("输入民族有误,请重新输入!");
continue;
}
}
string cla;
for (; ; )
{
Console.Write("请输入班级:");
string cla1 = Console.ReadLine();
if (cla1 == "一班" || cla1 == "一")
{
cla = "C001";
break;
}
else if (cla1 == "二班" || cla1 == "二")
{
cla = "C002";
break;
}
else if (cla1 == "三班" || cla1 == "三")
{
cla = "C003";
break;
}
else if (cla1 == "四班" || cla1 == "四")
{
cla = "C004";
break;
}
else
{
Console.WriteLine("输入班级有误,请重新输入!");
continue;
}
}
cmd.CommandText = "update User set Password='"+pwd+"',NickName='"+nick+"',Sex="+sex+",Birthday='"+bir+"',Nation='"+nation+"',Class='"+cla+"' where UserName='" + user + "'";
conn.Open();
int i = cmd.ExecuteNonQuery();//执行操作,并记录受影响的行数
if (i > )
Console.WriteLine("更改成功。");
else
{
Console.WriteLine("更改失败");
}
conn.Close();
}
else if (yn == "n")
{
Console.WriteLine("取消更改。");
}
else
{
Console.WriteLine("输入有误!");
}
}
else
{
Console.WriteLine("查无此条数据!");
}
conn.Close();
}
else if(code==)
{
//查看
Console.Write("请输入想要查看的用户名(输入*代表查看全部):");
string user = Console.ReadLine();
if (user != "*")
{
cmd.CommandText = "select ids,UserName,PassWord,NickName,Sex,Birthday,(select NationName from Nation where NationCode=Users.Nation) as 'Nation' ,ClassName as 'Class' from Users join Class on Class.ClassCode=Users.Class where UserName='" + user + "'";
}
else
{
cmd.CommandText = "select ids,UserName,PassWord,NickName,Sex,Birthday,(select NationName from Nation where NationCode=Users.Nation) as 'Nation' ,ClassName as 'Class' from Users join Class on Class.ClassCode=Users.Class";
}
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Console.WriteLine(dr["ids"] + "\t" + dr["UserName"] + "\t" + dr["PassWord"] + "\t" + dr["NickName"] + "\t" + (Convert.ToBoolean(dr["Sex"]) ? "男" : "女") + "\t" + Convert.ToDateTime(dr["Birthday"]).ToString("yyyy年MM月dd日") + "\t" + dr["Nation"] + "\t" + dr["Class"]);
}
conn.Close();
}
else
{
Console.WriteLine("查无此条数据!");
}
conn.Close();
}
else
{
Console.WriteLine("无此操作序号有误,请重新输入!");
continue;
}
}
catch
{
Console.WriteLine("输入有误,请重新输入!");
continue;
}
}
}
}
}

ado.net增删改查练习的更多相关文章

  1. Ado.net[增删改查,GET传值]

    1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.c ...

  2. ADO.NET 增删改查的基本用法

    ADO.NET:数据访问技术 就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中也可以将数据库中的数据提取到内存中供程序调用 所有数据访问技术的基础 连接 ...

  3. LinQ和ADO.Net增删改查 备忘

    是否些倦了 SqlConnection conn=new SqlConnection();一系列繁冗的代码? 来试试Linq吧 查: using System.Data.SqlClient; name ...

  4. ADO.net 增删改查

    ADO.net 一.定义:编程开发语言与数据库连接的一门语言技术 二.链接: 在vs中操作数据库需在开头进行链接 链接内容:using System.Data.SqlClient 三.引用数据库: 四 ...

  5. ado.net增删改查操作

    ado.net是数据库访问技术将数据库中的数据,提取到内存中,展示给用户看还可以将内存中的数据写入数据库中去 并不是唯一的数据库访问技术,但是它是最底层最基础的数据库访问技术 使用ado.net对数据 ...

  6. ADO.net 增删改查封装DBhelper

    using System; using System.Collections.Generic; using System.Data.SqlClient;//引用数据库客户端 using System. ...

  7. Ado.net[登录,增删改查,Get传值,全选,不选,批量删除,批量更新]

    [虽然说,开发的时候,我们可以使用各种框架,ado.net作为底层的东西,作为一个合格的程序员,在出问题的时候我们还是要知道如何调试] 一.增删改查 cmd.ExecuteReader();执行查询, ...

  8. ado.net C#如何实现数据库增删改查

    ado.net数据库访问技术将数据库中的数据,提取到内存中,展示给用户看还可以将内存中的数据写入数据库中去 并不是唯一的数据库访问技术,但是它是最底层的数据库访问技术也就是说是最麻烦但是是最不可缺少的 ...

  9. ado.net的简单数据库操作(三)——简单增删改查的实际应用

    果然,在犯困的时候就该写写博客,写博客就不困了,哈哈! 上篇我记录了自己的SqlHelper的开发过程,今天记录一下如何使用这个sqlhelper书写一个具有简单增删改查的小实例啦. 实例描述:在数据 ...

随机推荐

  1. selenium WebDriver 操作高德地图

    String URL="http://www.amap.com/"; WebDriver driver = new FirefoxDriver(profile); driver.g ...

  2. Django~static files (e.g. images, JavaScript, CSS)

    django.contrib.staticfiles settings.py django.contrib.staticfiles is included in your INSTALLED_APPS ...

  3. Spring配置数据源

    Spring在第三方依赖包中包含了两个数据源的实现类包,其一是Apache的DBCP,其二是 C3P0.可以在Spring配置文件中利用这两者中任何一个配置数据源. DBCP数据源 DBCP类包位于 ...

  4. JS 保留两位小数问题收集

    1.使用四舍五入的方法,保留小数点后的两位小数: toFixed里面的参数表示保留的小数的位数,范围是0-20,超过20位就会报错了 <script> var num=22.127456; ...

  5. mysql探究之null与not null

    相信很多用了mysql很久的人,对这两个字段属性的概念还不是很清楚,一般会有以下疑问: 1.我字段类型是not null,为什么我可以插入空值 2.为毛not null的效率比null高 3.判断字段 ...

  6. CSS伪类

    CSS伪类:控制元素的某种状态 语法:元素名称:伪类名称{属性:值} CSS伪类控制链接状态 状态 语法 未访问的链接 a:link{color:#ff00ff} 已访问的链接 a:visited{c ...

  7. October 8th 2016 Week 41st Saturday

    When ambition ends, happiness begins. 野心消亡之日,正是快乐破茧之时. If I don't have the wish to be a useful man, ...

  8. iphone删除自动更新的系统

    1.利用 etc/host 文件屏蔽 Apple 更新服务器用电脑 iTools 或者手机 iFile 打开 etc/host 文件,添加:127.0.0.1 mesu.apple.com到文件中.2 ...

  9. SQLSERVER查询连接数

    SELECT * FROM [Master].[dbo].[SYSPROCESSES] WHERE [DBID] IN (SELECT [DBID]FROM [Master].[dbo].[SYSDA ...

  10. XAML语言介绍

    <Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winf ...