码迷,mamicode.com
首页 > Web开发 > 详细

ado.net

时间:2016-12-04 00:07:04      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:blog   and   ima   链接   pre   update   stat   用户名   ndt   

使用前先添加

using System.Data.SqlClient;

delete删除:

技术分享
namespace adonet_接受_删除_添加
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入要删除的用户名:");
            string uname = Console.ReadLine();
            //1、创建数据库链接类
            string sql = "server=.;database=data0928;user=sa;pwd=123;";
            SqlConnection conn = new SqlConnection(sql);
            //2、创建数据库操作类
            SqlCommand cmd = conn.CreateCommand();
            //3、编写操作语句
            cmd.CommandText = "delete from users where username=‘" + uname + "‘";
            //4、打开数据库连接
            conn.Open();
            //5、执行操作,返回受影响行数
            int a= cmd.ExecuteNonQuery();
            //6、关闭数据库连接
            conn.Close();
            //7、提示操作是否成功
            if (a > 0)
            { Console.WriteLine("删除成功"); }
            else
            { Console.WriteLine("删除失败"); }

            Console.ReadLine();
        }
    }
}
技术分享

insert添加:

技术分享
namespace insert
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入要添加的用户名:");
            string uname = Console.ReadLine();
            Console.WriteLine("请输入要添加的密码:");
            string pwd = Console.ReadLine();
            Console.WriteLine("请输入要添加的昵称:");
            string nick = Console.ReadLine();
            Console.WriteLine("请输入要添加的性别:");
            string sex = Console.ReadLine();
            Console.WriteLine("请输入要添加的生日:");
            string bir = Console.ReadLine();
            Console.WriteLine("请输入要添加的民族:");
            string nation = Console.ReadLine();
            Console.WriteLine("请输入要添加的班级:");
            string cla = Console.ReadLine();

            //创建数据库连接类
            SqlConnection con = new SqlConnection("server=.;database=data0928;user=sa;pwd=123");
            //创建数据库操作类
            SqlCommand cm = con.CreateCommand();
            //编写操作语句
            cm.CommandText = "insert into users values(‘" + uname + "‘,‘" + pwd + "‘,‘" + nick + "‘,‘" + sex + "‘,‘" + bir + "‘,‘" + nation + "‘,‘" + cla + "‘)";
            //打开数据库连接
            con.Open();
            //执行操作
            cm.ExecuteNonQuery();
            //关闭数据库
            con.Close();

            Console.ReadLine();
        }
    }
}
技术分享

select选择:

技术分享
namespace select
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建连接
            SqlConnection conn = new SqlConnection("server=.;database=data0928;user=sa;pwd=123");
            //创建数据库操作
            SqlCommand cmd = conn.CreateCommand();
            //编写操作语句
            cmd.CommandText = "select * from users";

            conn.Open();
            SqlDataReader dr= cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    Console.WriteLine(dr["ids"] + "\t" + dr["username"] + "\t" + dr["password"] + "\t" + dr["nickname"] + "\t" + dr["sex"] + "\t" + dr["birthday"] + "\t" + dr["nation"] + "\t" + dr["class"]);
                }            
            }
            conn.Close();
            Console.ReadLine();
        }
    }
}
技术分享

update更改:

技术分享
namespace Update
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入要修改的用户名:");
            string uname = Console.ReadLine();
            Console.Write("请输入密码:");
            string pwd = Console.ReadLine();
            Console.Write("请输入昵称:");
            string nick = Console.ReadLine();
            Console.Write("请输入性别:");
            string sex = Console.ReadLine();
            Console.Write("请输入生日:");
            string bir = Console.ReadLine();
            Console.Write("请输入民族:");
            string nation = Console.ReadLine();
            Console.Write("请输入班级:");
            string cla = Console.ReadLine();

            SqlConnection conn = new SqlConnection("server=.;database=Data0928;user=sa;pwd=123");

            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandText = "update Users set PassWord = ‘" + pwd + "‘,NickName = ‘" + nick + "‘,Sex=‘" + sex + "‘,Birthday=‘" + bir + "‘,Nation=‘" + nation + "‘,Class=‘" + cla + "‘ where UserName = ‘" + uname + "‘";

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

        }
    }
}
技术分享

ado.net

标签:blog   and   ima   链接   pre   update   stat   用户名   ndt   

原文地址:http://www.cnblogs.com/baimangguo/p/6129750.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!