zoukankan      html  css  js  c++  java
  • ado显示,删除后刷新重新显示

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.SqlClient;
    
    namespace ConsoleApplication6
    {
        class Program
        {
            static void Main(string[] args)
            {
                //1.显示表格内容
                //引用数据库命名空间
                //建立数据库连接
                SqlConnection conn = new SqlConnection("server=.;database=data0425;user=sa;pwd=123;");
                //在数据库连接基础上建立数据库操作
                SqlCommand cmd = conn.CreateCommand();
                //编写sql操作命令
                cmd.CommandText = "select code,name,score from student";
                //执行操作
                //开启数据库
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if(dr.HasRows)
                {
                    while(dr.Read())
                    {
                        Console.WriteLine(dr["code"]+"  "+dr["name"]+"   "+dr["score"]);
                    }
                }
    
                //关闭数据库
                conn.Close();
    
    
                //2.删除s107
               
                Console.Write("请输入需要删除的学号:");
                string scode=Console.ReadLine();
                //编写删除指令
                cmd.CommandText = "delete from student where code='"+scode+"'";
                //进行操作
                try
                {
                    //开启数据库
                    conn.Open();
                    //执行操作
                    cmd.ExecuteNonQuery();
                    Console.Clear();
                    Console.WriteLine("删除成功!");
    
                }
                catch
                {
                    Console.WriteLine("连接数据库失败!请重新连接");
                }
                finally
                {
                    //关闭数据库
                    conn.Close();
                }
    
                //3.刷新并显示删除后的表格
    
                //编写sql操作命令
                cmd.CommandText = "select code,name,score from student";
                //执行操作
                //开启数据库
                conn.Open();
                SqlDataReader dr1 = cmd.ExecuteReader();
                if (dr1.HasRows)
                {
                    while (dr1.Read())
                    {
                        Console.WriteLine(dr1["code"] + "  " + dr1["name"] + "   " + dr1["score"]);
                    }
                }
    
                //关闭数据库
                conn.Close();
    
    
    
    
    
                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    [通信] C# TCP实现多个客户端与服务端 数据 与 文件的传输
    [压缩]C#下使用SevenZipSharp压缩解压文本
    [通信] C#多线程Socket-文件传输
    [算法] N 皇后
    【算法】N Queens Problem
    [Eclipse]
    [C/C++] String Reverse 字符串 反转
    [SQL] 获取 Microsoft SQL Server 2008 的数据表结构
    [WIFI] WIFI 破解(初级)
    Unable to extract 64-bitimage. Run Process Explorer from a writeable directory
  • 原文地址:https://www.cnblogs.com/fengsantianya/p/5604606.html
Copyright © 2011-2022 走看看