using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient; namespace ConsoleApplication1
{
class Program4
{
static void Main4(string[] args)
{
//用户输入要删除的数据主键值
Console.WriteLine("请输入要删除的代号:");
string code = Console.ReadLine(); //判断该数据存不存在
SqlConnection conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from Info where Code='"+code+"'";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows)
{
//说明该数据存在
Console.WriteLine("查到该数据,是否要执行删除操作,如果要删除请输入:1");
int sc = Convert.ToInt32(Console.ReadLine()); if (sc == )
{
//删除
dr.Close(); //关闭读取器 cmd.CommandText = "delete from Info where Code='"+code+"'";
cmd.ExecuteNonQuery();
Console.WriteLine("删除成功!"); }
else
{
//不删除
dr.Read(); string sex = Convert.ToBoolean(dr[])?"男":"女";
string nation = MinZu(dr[].ToString()); string str = "代号:"+dr[]+"\t姓名:"+dr[]+"\t性别:"+sex+"\t民族:"+nation+"\t生日:"+dr[]; Console.WriteLine(str); }
}
else
{
//数据不存在
Console.WriteLine("输入的代号错误!");
} conn.Close();
Console.ReadLine();
} static string MinZu(string code)
{
string name="";
SqlConnection conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select Name from Nation where Code = '" + code + "'";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
name = dr[].ToString();
}
conn.Close(); return name;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient; namespace ConsoleApplication1
{
class Program3
{
static void Main3(string[] args)
{
//让用户输入要添加的内容
Console.WriteLine("请输入要添加的代号:");
string code = Console.ReadLine(); Console.WriteLine("请输入姓名:");
string name = Console.ReadLine(); Console.WriteLine("请输入性别:");
bool sex = Console.ReadLine()=="男"?true:false; Console.WriteLine("请输入民族:");
string nation = Console.ReadLine(); Console.WriteLine("请输入生日:");
string birthday = Console.ReadLine(); string nationcode = "n001"; //将民族名称转为名族代号
SqlConnection conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select Code from Nation where Name = '"+nation+"'";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
nationcode = dr[].ToString();
}
conn.Close(); //往Info表添加数据
cmd.CommandText = "insert into Info values('"+code+"','"+name+"','"+sex+"','"+nationcode+"','"+birthday+"')";
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Console.WriteLine("添加成功!"); Console.ReadLine();
}
}
}

ado.net(增删改)的更多相关文章

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

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

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

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

  3. ADO.NET增删改-------跟查不一样

    建立数据库 create database ren go use database go create table user ( code nvarchar(20) primary key,--编号 ...

  4. ADO.NET 增删改、查

    数据访问 对应命名空间:System.Data.SqlClient; SqlConnection:连接对象SqlCommand:命令对象SqlDataReader:读取器对象 CommandText: ...

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

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

  6. ado.net增删改查操作

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

  7. ADO.net 增删改查

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

  8. ado.net增删改查练习

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  9. ADO.net增删改的使用

    添加数据 -------------------------------------------------- //让用户输入要添加的内容 Console.WriteLine("请输入要添加 ...

随机推荐

  1. Apple设备中point,磅(pt),pixel的关系与转换,以及iPhone模拟器与真机的长度关系

    查阅了好多资料都没有发现有相关的详细介绍,包括苹果官方文档,也是草草带过.后来是在一个介绍Macbook打印字体的博客中看到的,受到启发. 首先说明苹果设备绘图的长度单位可以认为是point,不是磅( ...

  2. 什么是 GOF(四人帮,全拼 Gang of Four)?

    在 1994 年,由 Erich Gamma.Richard Helm.Ralph Johnson 和 John Vlissides 四人合著出版了一本名为 Design Patterns - Ele ...

  3. Ubuntu apt-get update错误解决

    用apt-get命令安装开发软件非常方便,但由于各种原因,经常链接不上软件源,于是需要使用sudo apt-get update命令来更新软件源. 而屋漏偏逢连夜雨,这时候更新命令也罢工,出现各种错误 ...

  4. kvm和qemu的关系

    KVM (Kernel Virtual Machine) is a Linux kernel module that allows a user space program to utilize th ...

  5. oracle11g卸载,10g之类版本通用

    鉴于oracle一些比较稀奇的问题,本人没碰到的不能帮忙解决的.而且比较着急赶时间的亲们,我就只能推荐先卸载在安装的办法了,这个方法一般用时也就1个小时到1个半小时之间,切记按步骤删除,别漏删了,不然 ...

  6. Linux文件的打包与压缩

    打包命令: tar tar 的选项与参数非常的多!我们只讲几个常用的选项,更多选项您可以自行 man tar 查询罗! [root@www ~]# tar [-j|-z] [cv] [-f 创建的档名 ...

  7. js节流防抖应用场景,以及在vue中节流防抖的具体实现

    故事背景: 项目有个需求是输入框在输入的时候进行搜索,展示下拉数据,但是没必要输入一个字都进行搜索,所以想到了在输入结束200毫秒后再进行搜索,从而引出来了 js的节流(throttle),防抖(de ...

  8. raw_input() 与 input()对比

    转载来自http://www.cnblogs.com/way_testlife/archive/2011/03/29/1999283.html 这两个均是 python 的内建函数,通过读取控制台的输 ...

  9. C# 各版本的新特性

    http://www.cnblogs.com/JeffreySun/archive/2012/11/14/2770211.html

  10. window 实用操作(结束已打开无法删除进程 内存占用)

    1.win7删除文件,文件夹或文件已在另一程序中打开:https://jingyan.baidu.com/article/e75057f2a41e88ebc91a8985.html 删除文件时,提示“ ...