c#连接数据库作业

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace ConsoleApp7
{

    class Program
    {
        static void Main(string[] args)
        {

            for (int i = 0; i < 10; i++)
            {
                MySql my = new MySql();
                //
                Console.WriteLine("欢迎来到河南牧业经济学院");
                Console.WriteLine("是否要添加信息?");
                Console.WriteLine("是请按:Y              否请按:N");
                string st1 = Console.ReadLine();
                char ch1 = Convert.ToChar(st1);
                if(ch1=='Y'||ch1=='y')
                {
                    my.Add();
                    my.Show();
                    Console.WriteLine("继续操作请按回车");
                }
                else if(ch1=='N'||ch1=='n')
                {
                    //删除
                    Console.WriteLine("是否要删除");
                    Console.WriteLine("是请按:Y              否请按:N");
                    string st2 = Console.ReadLine();
                    char ch2 = Convert.ToChar(st2);
                    if (ch2 == 'Y' || ch2 == 'y')
                    {
                        my.Drop();
                        my.Show();
                        Console.WriteLine("继续操作请按回车");
                    }
                    else if (ch2 == 'N' || ch2 == 'n')
                    {
                        //查询
                        Console.WriteLine("是否要查询");
                        Console.WriteLine("是请按:Y              否请按:N");
                        string st3 = Console.ReadLine();
                        char ch3 = Convert.ToChar(st3);
                        if (ch2 == 'Y' || ch2 == 'y')
                        {
                            my.Select();
                            my.Show();
                            Console.WriteLine("继续操作请按回车");
                        }
                        else if (ch2 == 'N' || ch2 == 'n')

                            Console.WriteLine("欢迎再次使用");
                        else
                            Console.WriteLine("请输入正确操作");
                        
                    }
                    else
                        Console.WriteLine("请输入正确操作");        
                }    
                else
                    Console.WriteLine("请输入正确操作");
               
                Console.ReadKey();
            }


        }
    }
    public class MySql
    {
        
           public static string connstr = "data source=localhost;database=students;user id=root;password=password;pooling=false;charset=utf8";//pooling代表是否使用连接池
           public static  MySqlConnection conn = new MySqlConnection(connstr);
           public static string sql = "select * from stu";
           MySqlCommand cmd = new MySqlCommand(sql, conn);
            
        
        
         //删除
         public void Drop()
        {

            Console.WriteLine("请输入要删除的编号:");
            string std = Console.ReadLine();
            int i = Convert.ToInt32(std);
            conn.Open();
            string sql4 = "delete from stu where id="+i;
            MySqlCommand cmd3 = new MySqlCommand(sql4, conn);

            int s = cmd3.ExecuteNonQuery();
            if (s == 0)
                Console.WriteLine("操作失败!");
            else
                Console.WriteLine("操作成功!");
            conn.Close();
        }
        //添加
         public void Add()
        {
            Console.WriteLine("请输入姓名,学号:");
           
            string str;
            str = Console.ReadLine();
            string num = Console.ReadLine();          
            conn.Open();
            string sql3 = "insert into stu(id,name,number) values (null,'"+str+"','"+num+"')";
            MySqlCommand cmd3 = new MySqlCommand(sql3, conn);
            
            int s = cmd3.ExecuteNonQuery();
            if (s == 0)
                Console.WriteLine("操作失败!");
            else
                Console.WriteLine("操作成功!");
            conn.Close();
        
         }
        //查询
        public void Select()
        {
            Console.WriteLine("请输要入查询的编号:");
            string std = Console.ReadLine();
            int i = Convert.ToInt32(std);
            conn.Open();
            string sql5 = "select * from stu where id="+i;
            MySqlCommand cmd5 = new MySqlCommand(sql5, conn);

            int s = cmd5.ExecuteNonQuery();
            if (s == 0)
                Console.WriteLine("操作失败!");
            else
                Console.WriteLine("操作成功!");
            conn.Close();
        }
        //展示
        public void Show()
        {
            conn.Open();
            MySqlDataReader reader = cmd.ExecuteReader();
            Console.WriteLine("id\t姓名\t学号");
            while (reader.Read())
            {
                Console.Write(reader.GetInt32("id") + "\t");
                if (reader.IsDBNull(1))
                {
                    Console.Write("空\t");
                }
                else
                {
                    Console.Write(reader.GetString("name") + "\t");
                }
                if (reader.IsDBNull(2))
                {
                    Console.Write("空\n");
                }
                else
                {
                    Console.Write(reader.GetString("number") + "\n");
                }
            }
            conn.Close();
        }
       
    }

}

发布了55 篇原创文章 · 获赞 47 · 访问量 3519

猜你喜欢

转载自blog.csdn.net/qq_42577542/article/details/104526766