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();
            }


           
        }
    }
}

posted @ 2016-06-21 18:24  枫炎  阅读(213)  评论(0编辑  收藏  举报