ADO.NET添加

using System.Data.SqlClient;
            string ucode = "";
           //开始 生成编号
            SqlConnection conn = new SqlConnection("server=.;database=Data0504;user=sa;pwd=lyb0629");
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from Users order by Ucode desc";
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            ucode = dr["Ucode"].ToString();
            int a = Convert.ToInt32(ucode.Substring(1));
            a++;
            ucode = "U" + a.ToString("000");
            conn.Close();
            //结束 生成编号
            Console.WriteLine("您的编号是:"+ucode);
            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();
                   
            cmd.CommandText = "insert into Users value('@b','@c','@d','@e','@f','@g')";
            cmd.Parameters.Clear();          
            cmd.Parameters.AddWithValue(@b, username);
            cmd.Parameters.AddWithValue(@c, password);
            cmd.Parameters.AddWithValue(@d, nickname);
            cmd.Parameters.AddWithValue(@e, sex);
            cmd.Parameters.AddWithValue(@f, birthday);
            cmd.Parameters.AddWithValue(@g, nation);
            conn.Open();
            int count = cmd.ExecuteNonQuery();
            conn.Close();
            if (count >= 1)
            {
                Console.WriteLine("添加成功!");
            }
            else
            {
                Console.WriteLine("添加失败!");
            }
         Console.ReadKey();

 

posted @ 2017-07-05 20:35  小程序员//  阅读(196)  评论(0编辑  收藏  举报