ado修改

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

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            //1、让用户输入要修改的学生编号
            Console.Write("请输入修改后的学生编号:");
            string scode = Console.ReadLine();

            Console.Write("请输入修改后的学生姓名:");
            string sname = Console.ReadLine();

            Console.Write("请输入修改后的学生性别:");
            bool ssex = Convert.ToBoolean(Console.ReadLine());

            Console.Write("请输入修改后学生生日:");
            DateTime sbirthday = Convert.ToDateTime(Console.ReadLine());

            Console.Write("请输入修改后学生分数:");
            decimal sscore = Convert.ToDecimal(Console.ReadLine());


            
            //2、执行修改
            //引用数据库命名空间
            //建立数据连接
            SqlConnection conn = new SqlConnection("server=.;database=data0425;user=sa;pwd=123;");
            //建立数据控制,在数据连接基础上
            SqlCommand cmd = conn.CreateCommand();
            //编写sql数据命令
            cmd.CommandText = "update student set name='" + sname + "',sex='" + ssex + "',bithday='" + sbirthday + "',score= " + sscore + " where code='" + scode + "'";
            //cmd.CommandText = "update student set name='" + sname + "',sex='" + ssex + "',birthday='" + sbirthday + "',score=" + sscore + " where code = '" + scode + "'";

            Console.WriteLine(cmd.CommandText);
            try
            {
                //开启数据库
                conn.Open();
                //执行操作命令
                cmd.ExecuteNonQuery();
                //显示操作成功
                Console.WriteLine("修改成功!");

            }
            catch
            {
                Console.WriteLine("数据库连接失败!请重新连接!");
            }
            finally
            {
                //关闭数据库
                conn.Close();
                Console.ReadLine();
            }


           
        }
    }
}

转载于:https://www.cnblogs.com/fengsantianya/p/5604596.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值