瀚城老爷子

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 ADO.net

一、定义:编程开发语言与数据库连接的一门语言技术

二、链接:

在vs中操作数据库需在开头进行链接

链接内容:using System.Data.SqlClient

三、引用数据库:

四、应用:

(一)完整查数据库代码

using System.Data.SqlClient;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string sql = "server=.;Database=student;user=sa;pwd=123;";
            SqlConnection conn = new SqlConnection(sql);
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select *from User";
            conn.Open();
            //定义变量,接收读取操作,放到结果集中
            SqlDataReader dr = cmd.ExecuteReader();
            //判断结果集是否存在数据
            if (dr.HasRows)
            {
                int a = 0;
                while (dr.Read())//循环依行读取结果集
                {
                    //定义变量记录打印数
                    a++;
                    //取行中格中的值
                    //索引取
                    string ids = dr[0].ToString();//返回基类,需转换类型
                    string username = dr[1].ToString();
                    //列名取
                    string birthday = dr["Birthday"].ToString();
                    string password = dr["Password"].ToString();
                    string nickname = dr["Nickname"].ToString();
                    string sex = dr["Sex"].ToString();
                    Console.WriteLine();
                }
                Console.WriteLine("共打印"+a+"条数据");
            }
            conn.Close();
            Console.ReadLine();
        }
    }
}

(二)完整删除代码

using System.Data.SqlClient;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string sql = "server=.;Database=student;user=sa;pwd=123;";
            SqlConnection conn = new SqlConnection(sql);
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "delete *from User where username='zhangsan'";
            conn.Open();
            //变量接收删除的次数
            int a = cmd.ExecuteNonQuery();
            //判断是否删除成功
            if (a > 0)
            {
                Console.WriteLine("删除成功,本次共删除" + a + "条数据");
            }
            else {
                Console.WriteLine("删除失败,本次未删除任何数据");
            }
            conn.Close();
            Console.ReadLine();
        }
    }
}

(三)完整修改数据

using System.Data.SqlClient;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string sql = "server=.;Database=student;user=sa;pwd=123;";
            SqlConnection conn = new SqlConnection(sql);
            SqlCommand cmd = conn.CreateCommand();
            //查询判断修改数据是否存在
            Console.Write("请输入用户名:");
            string name = Console.ReadLine();
            cmd.CommandText = "select *from User where Username='"+name+"'";
            //赋值判断不存在
            bool s = false;
            conn.Open();
            SqlDataReader dr =cmd.ExecuteReader();
            if (dr.HasRows)
            {
                s = true;
            }
            //查询后关闭数据库
            conn.Close();
            //判断修改的条件
            if (!s)
            {
                Console.WriteLine("输入用户名错误!");
            }
            else {
                Console.WriteLine("已查到此用户,请输入修改信息");
                Console.Write("请输入修改密码:");
                string pwd= Console.ReadLine();
                Console.Write("请输入修改昵称:");
                string gname = Console.ReadLine();
                cmd.CommandText = "upate user set password='"+pwd +"',nickname='"+gname+"'";
                conn.Open();
                //接受修改执行是否成功
                int aaa = cmd.ExecuteNonQuery();
                conn.Close();
                if (aaa > 0)
                {
                    Console.WriteLine("修改成功");
                }
                else {
                    Console.WriteLine("修改失败");
                }
            }
            Console.ReadLine();
        }
    }
}
View Code

(四)完整增加数据

string sql = "server=.;Database=student;user=sa;pwd=123;";
            SqlConnection conn = new SqlConnection(sql);
            SqlCommand cmd = conn.CreateCommand();
            string name = "";
            for (; ; )
            {
                Console.Write("请输入用户名:");
                name = Console.ReadLine();
                //不能为空
                if (name == "")
                {
                    Console.WriteLine("输入用户名不能为空!");
                }
                else
                {//不能重复
                    cmd.CommandText = "select * from User where username='" + name + "'";
                    conn.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.HasRows)
                    {
                        Console.WriteLine("输入的用户已存在!");
                    }
                    else
                    {
                        conn.Close();
                        Console.WriteLine("输入成功");
                        break;
                    }

                }
            }
            string password;
            for (; ; )//密码
            {
                Console.Write("请输入密码");
                password = Console.ReadLine();
                if (password.Length >= 6)
                {
                    Console.WriteLine("输入成功");
                    break;
                }
                else
                {
                    Console.WriteLine("输入密码长度不正确");
                }
            }
            string nickname;
            for (; ; )
            {
                Console.Write("请输入昵称:");
                nickname = Console.ReadLine();
                if (nickname == "")
                {
                    Console.WriteLine("昵称不能为空!");
                }
                else
                {
                    Console.WriteLine("输入成功");
                    break;
                }
            }
            string sex;
            for (; ; )
            {
                Console.Write("请输入性别:");
                sex = Console.ReadLine();
                if (sex == "")
                {
                    Console.WriteLine("输入成功");
                    sex = "1";
                    break;
                }
                else if (sex == "")
                {
                    Console.WriteLine("输入成功");
                    sex = "0";
                    break;
                }
                else
                {
                    Console.WriteLine("输入性别有误!");
                }
            }
            string birthday;
            for (; ; )
            {
                Console.Write("请输入生日:");
                birthday = Console.ReadLine();
                try
                {
                    DateTime dt = Convert.ToDateTime(birthday);
                    Console.WriteLine("输入成功");
                    break;
                }
                catch
                {
                    Console.WriteLine("输入日期有误!");
                    continue;
                }
            }
            string nation;
            for (; ; )
            {
                Console.Write("请输入民族:");
                nation = Console.ReadLine();
                cmd.CommandText = "select *from Nation where Nationname like '" + nation + "'%";
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    conn.Close();
                    Console.WriteLine("输入成功!");
                    break;
                }
                else
                {
                    Console.WriteLine("暂无此民族");
                }

            }
            //添加
            cmd.CommandText = "insert into User values('" + name + "','" + password + "','" + nickname + "','" + sex + "','" + birthday + "'," + nation + "');";
            conn.Open();
            int a = cmd.ExecuteNonQuery();
            conn.Close();
            if (a > 0)
            {
                Console.WriteLine("添加成功!");
            }
            else
            {
                Console.WriteLine("添加失败!");
            }
            Console.ReadLine()
View Code

 

posted on 2017-04-20 15:57  瀚城老爷子  阅读(183)  评论(0编辑  收藏  举报