ado测试

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

namespace adoceshi.dataopration
{
    public class studentdata
    {
        SqlConnection conn = null;
        SqlCommand cmd = null;
        public studentdata()
        {
            conn = new SqlConnection("server=.;database=adoceshi;user=sa;pwd=123;");
            cmd = conn.CreateCommand();
        }

        public List<Student> selectall()
        {
            List<Student> list = new List<Student>();

            cmd.CommandText = "select * from Student";
            try
            {
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        Student s = new Student();
                        s.Code = dr["Code"].ToString();
                        s.Name = dr["Name"].ToString();
                        s.Sex = Convert.ToBoolean(dr["Sex"]);
                        s.Birthday = Convert.ToDateTime(dr["Birthday"]);
                        s.SsubjectCode = dr["SsubjectCode"].ToString();

                        list.Add(s);
                    }
                }
                conn.Close();
            }
            catch
            {
                conn.Close();
                Console.WriteLine("数据库连接失败!请重新连接!");
            }
            return list;

        }

        //用户操作
        //1新增
        public void xinzeng()
        {
            string code, name, zhuanye,zhuanyecode,s;
            bool sex;
            DateTime shengri;
            while (true)
            {
                Console.Write("请输入学生编号:");
                string c = Console.ReadLine();
                cmd.CommandText = "select * from student where  [email protected]";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@c",c);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {

                    conn.Close();
                    Console.WriteLine("您输入的学生编号已拥有,请重新输入!");
                }
                else
                {

                    conn.Close();
                    code = c;
                    break;
                }
            }
            while (true)
            {
                Console.Write("请输入学生姓名(不能为空):");
                string n = Console.ReadLine();
                if (n == "")
                {
                    Console.WriteLine("用户名不能为空!请重新输入!");
                }
                else
                {
                    name = n;
                    break;
                }
            }
            while (true)
            {
                Console.Write("请输入学生性别(男/女):");
               s = Console.ReadLine();
                if (s == "男")
                {
                    sex = true;
                    break;
                }
                else if (s == "女")
                {
                    sex = false;
                    break;
                }
                else
                {
                    Console.WriteLine("您的输入有误!只能输入男或女,请重新输入!");
                }
            }
            while (true)
            {
                Console.Write("请输入学生生日(如2000/12/12):");
                try
                {
                    DateTime ri = Convert.ToDateTime(Console.ReadLine());
                    shengri = ri;
                    break;
                }
                catch
                {
                    Console.WriteLine("您输入的生日格式不正确!请重新输入!");
                }
            }
            while (true)
            {
                Console.Write("请输入学生专业:");
                string z = Console.ReadLine();
                cmd.CommandText = "select * from Ssubject where SsubjectName like @n";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@n", "%"+z+"%");

                    conn.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.HasRows)
                    {
                        dr.Read();
                        zhuanye = dr["SsubjectName"].ToString();
                            zhuanyecode = dr["SsubjectCode"].ToString();
                        conn.Close();
                        break;
                    }
                    else
                    {
                        conn.Close();
                        Console.WriteLine("没有该专业!请重新输入!");
                    }
            }
            Console.WriteLine(code+"\t"+name+"\t"+s+"\t"+shengri+"\t"+zhuanye);
            while(true)
            {
            Console.Write("以上为新增学生的信息,是否确定添加?(Y/N):");
            string queding = Console.ReadLine();
            if(queding=="y"||queding=="Y")
            {
                cmd.CommandText="insert into student values(@c,@x,@s,@b,@sub)";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@c",code);
                 cmd.Parameters.Add("@x",name);
                 cmd.Parameters.Add("@s",sex);
                 cmd.Parameters.Add("@b",shengri);
                 cmd.Parameters.Add("@sub",zhuanyecode);
                try
                {
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                Console.Clear();
                Console.WriteLine("添加成功!");
                    break;
                }
                catch
                {
                    Console.WriteLine("连接数据库失败!请重新连接!");
                }
            }
            else if(queding=="n"||queding=="N")
            {
                Console.Clear();
                break;
            }
            else
            {
                Console.WriteLine("输入不正确!请重新输入!");
            }
        }

        }
        //修改
        public void xiugai(string bianhao)
        {
            string code, name, zhuanye,zhuanyecode,ss;
            bool sex;
            DateTime shengri;
            cmd.CommandText = "select * from Student where [email protected]";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@c",bianhao);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    dr.Read();
                    Student s = new Student();
                        s.Name = dr["Name"].ToString();
                        s.Sex = Convert.ToBoolean(dr["Sex"]);
                        s.Birthday = Convert.ToDateTime(dr["Birthday"]);
                        s.SsubjectCode = dr["SsubjectCode"].ToString();

                        conn.Close();
                        Console.WriteLine("已查询到"+bianhao+"的学生信息,请进行以下修改:");
                        Console.WriteLine(bianhao+"\t"+s.Name+"\t"+s.Sex+"\t"+s.Birthday+"\t"+s.SsubjectCode);

                        Console.WriteLine(""+bianhao+"学生当前姓名为:"+s.Name+"");
                        while (true)
                        {
                            Console.Write("请输入修改后学生姓名(不能为空):");
                            string n = Console.ReadLine();
                            if (n == "")
                            {
                                Console.WriteLine("用户名不能为空!请重新输入!");
                            }
                            else
                            {
                                name = n;
                                break;
                            }
                        }
                        Console.WriteLine("" + bianhao + "学生当前性别为:" + s.Sex + "");
                        while (true)
                        {
                            Console.Write("请输入您的修改学生性别(男/女):");
                            ss = Console.ReadLine();
                            if (ss == "男")
                            {
                                sex = true;
                                break;
                            }
                            else if (ss == "女")
                            {
                                sex = false;
                                break;
                            }
                            else
                            {
                                Console.WriteLine("您的输入有误!只能输入男或女,请重新输入!");
                            }
                        }
                        Console.WriteLine("" + bianhao + "学生当前生日为:" + s.strbirthday + "");
                        while (true)
                        {
                            Console.Write("请输入修改学生生日(如2000/12/12):");
                            try
                            {
                                DateTime ri = Convert.ToDateTime(Console.ReadLine());
                                shengri = ri;
                                break;
                            }
                            catch
                            {
                                Console.WriteLine("您输入的生日格式不正确!请重新输入!");
                            }
                        }
                        Console.WriteLine("" + bianhao + "学生当前专业编号为:" + s.SsubjectCode + "");
                        while (true)
                        {
                            Console.Write("请输入修改后的学生专业:");
                            string z = Console.ReadLine();
                            cmd.CommandText = "select * from Ssubject where SsubjectName like @n";
                            cmd.Parameters.Clear();
                            cmd.Parameters.Add("@n", "%" + z + "%");
                            try
                            {
                                conn.Open();
                                SqlDataReader dr1 = cmd.ExecuteReader();
                                if (dr1.HasRows)
                                {
                                    dr1.Read();
                                    zhuanye = dr1["SsubjectName"].ToString();
                                    zhuanyecode = dr1["SsubjectCode"].ToString();
                                    conn.Close();
                                    break;
                                }
                                else
                                {
                                    conn.Close();
                                    Console.WriteLine("没有该专业!请重新输入!");
                                }
                            }
                            catch
                            {
                                conn.Close();
                                Console.WriteLine("连接数据库失败!请重新连接!");
                            }
                        }
                        Console.WriteLine(bianhao+ name + "\t" + sex + "\t" + shengri + "\t" + zhuanye);
                        while (true)
                        {
                            Console.Write("以上为修改学生的信息,是否确定添加?(Y/N):");
                            string queding = Console.ReadLine();
                            if (queding == "y" || queding == "Y")
                            {
                                cmd.CommandText = " update Student set [email protected],[email protected],[email protected],[email protected] where Code=‘"+bianhao +"‘";
                                cmd.Parameters.Clear();
                                cmd.Parameters.Add("@x", name);
                                cmd.Parameters.Add("@s", sex);
                                cmd.Parameters.Add("@b", shengri);
                                cmd.Parameters.Add("@sub", zhuanyecode);
                               conn.Open();
                                    cmd.ExecuteNonQuery();
                                    conn.Close();
                                    Console.Clear();
                                    Console.WriteLine("修改成功!");
                                    break;

                            }
                            else if (queding == "n" || queding == "N")
                            {
                                Console.Clear();
                                break;
                            }
                            else
                            {
                                Console.WriteLine("输入不正确!请重新输入!");
                            }
                        }

                }
                else
                {
                    Console.WriteLine("没有该学生的信息!请重新输入");
                }
        }
        //删除
        public void shanchu(string bianhao)
        {
            cmd.CommandText = "select * from Student where [email protected]";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@c",bianhao);
            conn.Open();
            SqlDataReader dr=cmd.ExecuteReader();
            if (dr.HasRows)
            {
                dr.Read();
                Student s = new Student();
                s.Code = dr["Code"].ToString();
                s.Name = dr["Name"].ToString();
                s.Sex = Convert.ToBoolean(dr["Sex"]);
                s.Birthday = Convert.ToDateTime(dr["Birthday"]);
                s.SsubjectCode = dr["SsubjectCode"].ToString();
                Console.WriteLine(bianhao + "\t" + s.Name + "\t" + s.Sex + "\t" + s.Birthday + "\t" + s.SsubjectCode);
                conn.Close();
                cmd.CommandText = "delete from student where [email protected]";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@cc", bianhao);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                Console.Clear();
                Console.WriteLine("删除成功!");
            }
            else
            {
                conn.Close();
                Console.WriteLine("没有该学生信息请重新输入");
            }

            conn.Close();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace adoceshi.model
{
    public class Student
    {
        SqlConnection conn = null;
        SqlCommand cmd = null;
        public Student()
        {
           conn = new SqlConnection("server=.;database=adoceshi;user=sa;pwd=123;");
           cmd = conn.CreateCommand();
        }

        private string _Code;
        /// <summary>
        /// 学生编号
        /// </summary>
        public string Code
        {
            get { return _Code; }
            set { _Code = value; }
        }
        private string _Name;
        /// <summary>
        /// 学生姓名
        /// </summary>
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        private bool _Sex;
        /// <summary>
        /// 学生性别
        /// </summary>
        public bool Sex
        {
            get { return _Sex; }
            set { _Sex = value; }
        }

        public string strsex
        {
            get
            {
                if (_Sex == true)
                {
                    return "男";
                }
                else
                {
                    return "女";
                }
            }
        }

        private DateTime _Birthday;
        /// <summary>
        /// 学生生日
        /// </summary>
        public DateTime Birthday
        {
            get { return _Birthday; }
            set { _Birthday = value; }
        }

        public string strbirthday
        {
            get
            {
                return _Birthday.ToString("yyyy年MM月dd日");
            }
        }
        public int nianling
        {
            get
            {
                return
                (DateTime.Now.Year - _Birthday.Year);
            }
        }

        private string _SsubjectCode;
        /// <summary>
        /// 学生专业
        /// </summary>
        public string SsubjectCode
        {
            get { return _SsubjectCode; }
            set { _SsubjectCode = value; }
        }

        /// <summary>
        /// 专业名称
        /// </summary>
        public string SsubjectName
        {
            get
            {
                string name = "";
                cmd.CommandText = "select * from Ssubject where [email protected]";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@c", _SsubjectCode);
                try
                {
                    conn.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.HasRows)
                    {
                        dr.Read();
                        name = dr["SsubjectName"].ToString();
                    }
                    conn.Close();
                }
                catch
                {
                    conn.Close();
                    Console.WriteLine("连接数据库失败!请重新输入!");
                }

                return name;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using adoceshi.model;
using adoceshi.dataopration;

namespace adoceshi
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                //打印学生表
                List<Student> data = new studentdata().selectall();
                Console.WriteLine("编号" + "\t" + "姓名" + "\t" + "性别" + "\t" + "年龄" + "\t" + "生日" + "\t" + "\t" + "专业");
                foreach (Student s in data)
                {
                    Console.WriteLine(s.Code + "\t" + s.Name + "\t" + s.strsex + "\t" + s.nianling + "\t" + s.strbirthday + "\t" + s.SsubjectName);
                }
                Console.WriteLine("--------------------------------------------------------------------------");
                //用户操作
                Console.Write("请输入您的操作数字(1=新增,2=修改,3=删除,4=退出):");
                string caozuo = Console.ReadLine();
                if(caozuo=="1")
                {
                    studentdata z = new studentdata();
                    z.xinzeng();
                }
                else if (caozuo == "2")
                {
                    Console.Write("请输入您要修改的学生的编号:");
                    string bianhao = Console.ReadLine();
                    studentdata g = new studentdata();
                    g.xiugai(bianhao);
                }
                else if (caozuo == "3")
                {
                    Console.Write("请输入您要删除的学生的编号:");
                    string bianhao = Console.ReadLine();
                    studentdata g = new studentdata();
                    g.shanchu(bianhao);
                }
                else if (caozuo == "4")
                {
                    break;
                }
                else
                {
                    Console.WriteLine("您的输入有误!请重新输入!");
                }

            }

            Console.ReadKey();
        }
    }
}

时间: 2024-10-15 04:01:34

ado测试的相关文章

ADO.net 综合练习题

题目: 第一部分: 新建一个数据库:ADO测试,包含下面两个数据表,使用代码创建,并保留创建的代码文本. 专业表Subject: 专业编号(SubjectCode):nvarchar类型,不能为空,主键 专业名称(SubjectName):nvarchar类型,不能为空 学生表Student: 编号(Code):nvarchar类型,不能为空,主键 姓名(Name):nvarchar类型,不能为空 性别(Sex):bit类型,不能为空,true为男,false为女 生日(Birthday):da

ado.not--综合练习

题目: 第一部分: 新建一个数据库:ADO测试,包含下面两个数据表,使用代码创建,并保留创建的代码文本. 专业表Subject: 专业编号(SubjectCode):nvarchar类型,不能为空,主键 专业名称(SubjectName):nvarchar类型,不能为空 学生表Student: 编号(Code):nvarchar类型,不能为空,主键 姓名(Name):nvarchar类型,不能为空 性别(Sex):bit类型,不能为空,true为男,false为女 生日(Birthday):da

测试Entity Framework 6比传统Ado.net性能差多少

测试环境: 硬件: Intel I5 4核 +8G内存. 软件: Windows 7 + vs2013 SP2 + EF6.1 + MVC5.1 数据库: vs2013自带的sql express 2012. 测试过程: 1. 新建一默认模板的MVC项目,在Models中添加如下代码并建立对应的库表: [Table("Person")] public class Person { public int Id { get; set; } public string UserName {

【ADO.NET-中级】百万级数据的批量插入的两种方法测试

在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍SQL Server支持的两种批量数据插入方法:Bulk和表值参数(Table-Valued Parameters). 运行下面的脚本,建立测试数据库和表值参数. --Create DataBase create database BulkTestDB; Go use BulkTestDB; go --Create Table Cr

ado 小测试

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using adoceshi.model; using adoceshi.dataopration; namespace adoceshi { class Program { static void Main(string[] args) { while (true)

ODBC、ADO

一.VC访问数据库的技术 1.ODBC --Open Database Connectivity微软开放式数据互联,一组用于访问和操作数据库的API,可以访问不同的数据库产品,但只能访问关系型数据库.MFC将这组API函数封装成ODBC类,使用前需要将不同的数据库设置为ODBC数据源. 2.DAO --基于ODBC的,目前已经被淘汰 3.OLE DB  --基于COM技术的,提供了一组用于访问和操作数据库的接口.既可以访问关系型又可以访问非关系型数据,性能也有了很大提高.缺点是学习难度大,对程序

VC++下封装ADO类以及使用方法

操作系统:windows 7软件环境:visual studio 2008 .Microsoft SQL 2005本次目的:介绍一个已经封装的ADO类,简单说明怎么导入使用 首先声明一下,这个封装的ADO类是在[vc知识库 ]下载的,因为最近在数据库课程设计,所以对vc++下使用ADO连接数据库不是很了解,故若是本文有错误的地方的,请不吝指出.具体的ADO类各个封装功能请进入[原文地址 ]查看.在此仅介绍使用此ADO封装类的入门,如连接数据库,显示记录等.一个测试例子如下: 使用步骤:1.先从[

EF添加ADO.NET实体模型处直接选择Oracle数据源

上一文介绍了如何下载Mysql for vs Tools来进行Mysql的ADO.NET实体模型数据源选择,今天将Oracle的测试了下.步骤如下: 1.在你项目Model层中nuget安装选中项 2.下载oracle,这个有点麻烦~就不具体说咯,这里就说下已经下好oracle的情况下,还要下一个oracle for vs2015 Tools.就是我们的驱动. 到官网: 下载之后,直接安装即可(注意,安装前最好将VS先退出): 完事儿后,就可以在我们新建项目——ADO.NET实体模型——新建链接

Delphi多线程数据库查询(ADO)

ADO多线程数据库查询通常会出现3个问题: 1.CoInitialize 没有调用(CoInitialize was not called):所以,在使用任何dbGo对象前,必须手 调用CoInitialize和CoUninitialize.调用CoInitialize失败会产生"CoInitialize was not called"例外. 2.画布不允许绘画(Canvas does not allow drawing):所以,必须通过Synchronize过程来通知主线程访问主窗体