删除,修改,添加

删除

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

namespace delete4
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入要删除的编号");
            string str = Console.ReadLine();//将用户输入的删除的编号读取并返回到str里
            //编写连接数据库的字符串 
            string sql = "server=.;database=Date0504;user=sa;pwd=y920106k;";

            //创建数据库连接对象
            SqlConnection conn = new SqlConnection(sql);

            //创建数据库操作对象
            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandText = "delete from users where ucode='" + str + "'";//执行删除条件
            conn.Open();//打开数据库
            int count = cmd.ExecuteNonQuery();

            conn.Close();
            if (count >= 1)
            {
                Console.Write("删除成功");
            }
            else
            {
                Console.WriteLine("删除失败");
            }
        }
    }
}

修改

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

namespace cha2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入要修改学生编号");
            string str = Console.ReadLine();
            Console.Write("请输入要修改的列名");
            string liename = Console.ReadLine();
            Console.Write("请输入要修改的值");
            string zhi = Console.ReadLine();
            SqlConnection conn = new SqlConnection("server=.;database=date0504;user=sa;pwd=y920106k");
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "update  users set " + liename + " ='" + zhi + "' where ucode='" + str + "'";
            conn.Open();
            int count = cmd.ExecuteNonQuery();
            conn.Close();
            if (count >= 1)
            {
                Console.WriteLine("修改成功");
            }
            else
            {
                Console.WriteLine("修改失败");
            }
            Console.ReadLine();
        }
    }
}

添加

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace inert5
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入学生编号");
            string ucode = Console.ReadLine();
            Console.Write("请输入学生用户名");
            string username = Console.ReadLine();
            Console.Write("请输入学生密码");
            string password = Console.ReadLine();
            Console.Write("请输入学生昵称");
            string nickname = Console.ReadLine();
            Console.Write("请输入学生性别");
            string sex = Console.ReadLine();
            Console.Write("请输入学生生日");
            string birthday = Console.ReadLine();
            Console.Write("请输入学生民族:");
            string nation = Console.ReadLine();
            string sql = "server=.;database=date0504;user=sa; pwd=y920106k";
            SqlConnection conn = new SqlConnection(sql);
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText= "insert into users values('" + ucode + "','" + username + "','" + password + "','" + nickname + "','" + sex + "','" + birthday + "','" + nation + "');";
            conn.Open();
            int count = cmd.ExecuteNonQuery();

            conn.Close();
            if (count >= 1)
            {
                Console.WriteLine("添加成功");
            }
            else
            {
                Console.WriteLine("添加失败");
                return;

            }
            Console.ReadLine();
        }
    }
}

 

posted @ 2017-07-19 14:10  纡ゾ少︶ㄣ  阅读(130)  评论(0编辑  收藏  举报