创建数据库:
create database duizhan go use duizhan go create table duizhan ( Code varchar(20) not null primary key, Name varchar(20) not null, Sex varchar(20) not null, Blood int, Attack int, Defence int, Mingzhong int, Shanbi int, Speed int, Experience int, Lv int, )
DBconnect.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace 对战
{
    public class DBconnect
    {
        private static string connstring = "server=.;database=duizhan;user=sa;pwd=diushiDEwutong0";
        public static SqlConnection conn
        {
            get
            {
                return new SqlConnection(connstring);
            }
        }
    }
}
duizhan.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 对战
{
    public class duizhan
    {
        private string code;
        public string Code
        {
            get { return code; }
            set { code = value; }
        }
private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
private string sex;
        public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }
private int blood;
        public int Blood
        {
            get { return blood; }
            set { blood = value; }
        }
private int attack;
        public int Attack
        {
            get { return attack; }
            set { attack = value; }
        }
private int defence;
        public int Defence
        {
            get { return defence; }
            set { defence = value; }
        }
private int mingzhong;
        public int Mingzhong
        {
            get { return mingzhong; }
            set { mingzhong = value; }
        }
private int shanbi;
        public int Shanbi
        {
            get { return shanbi; }
            set { shanbi = value; }
        }
private int speed;
        public int Speed
        {
            get { return speed; }
            set { speed = value; }
        }
private int experience;
        public int Experience
        {
            get { return experience; }
            set { experience = value; }
        }
private int lv;
        public int Lv
        {
            get { return lv; }
            set { lv = value; }
        }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace 对战
{
    public class duizhanDA
    {
        private SqlConnection _conn;
        private SqlCommand _cmd;
        private SqlDataReader _dr;
        //用构造函数来初始化连接对象,命令对象
        public duizhanDA()
        {
            _conn = DBconnect.conn;
            _cmd = _conn.CreateCommand();
        }
        //添加数据
        public bool Add(string code, string name, string sex)
        {
            int seed = (int)Convert.ToChar(name.Substring(0, 1)) + (int)Convert.ToChar(name.Substring(1, 1));
            Random rand = new Random(seed);
            //Random rand = new Random();
            int blood = 3000 + rand.Next(3000);
            int attack = 600 + rand.Next(400);
            int defence = 10 + rand.Next(90);
            int minzhong = rand.Next(50) + 50;
            int shanbi = rand.Next(50) + 10;
            int speed = 100 + rand.Next(50);
            int exprience = 0;
            int lv = 1;
            _cmd.CommandText = "insert into duizhan values(@code,@name,@sex,@blood,@attack,@defence,@minzhong,@shanbi,@speed,@experience,@lv)";
            _cmd.Parameters.Clear();
            _cmd.Parameters.AddWithValue("@code", code);
            _cmd.Parameters.AddWithValue("@name", name);
            _cmd.Parameters.AddWithValue("@sex", sex);
            _cmd.Parameters.AddWithValue("@blood", blood);
            _cmd.Parameters.AddWithValue("@attack", attack);
            _cmd.Parameters.AddWithValue("@defence", defence);
            _cmd.Parameters.AddWithValue("@minzhong", minzhong);
            _cmd.Parameters.AddWithValue("@shanbi", shanbi);
            _cmd.Parameters.AddWithValue("@speed", speed);
            _cmd.Parameters.AddWithValue("@experience", exprience);
            _cmd.Parameters.AddWithValue("@lv", lv);
            _conn.Open();
            int n = _cmd.ExecuteNonQuery();
            _conn.Close();
            if (n > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //查询所有数据
        public List<duizhan> Select()
        {
            _cmd.CommandText = "select * from duizhan";
            _conn.Open();
            _dr = _cmd.ExecuteReader();
            //定义一个空的集合
            List<duizhan> list = new List<duizhan>();
            if (_dr.HasRows)
            {
                while (_dr.Read())
                {
                    //造一个duizhan对象
                    duizhan data = new duizhan();
                    data.Code = _dr[0].ToString();
                    data.Name = _dr[1].ToString();
                    data.Sex = _dr[2].ToString();
                    data.Blood = (int)_dr[3];
                    data.Attack = (int)_dr[4];
                    data.Defence = (int)_dr[5];
                    data.Mingzhong = (int)_dr[6];
                    data.Shanbi = (int)_dr[7];
                    data.Speed = (int)_dr[8];
                    data.Experience = (int)_dr[9];
                    data.Lv = (int)_dr[10];
                    //扔到集合里面
                    list.Add(data);
                }
            }
            _conn.Close();
            return list;
        }
        //根据姓名查询
        public List<duizhan> Select(string name)
        {
            _conn.Open();
            _cmd.CommandText = "select * from duizhan where name=@name";
            _cmd.Parameters.Clear();
            _cmd.Parameters.AddWithValue("@name", name);
            _dr = _cmd.ExecuteReader();
            List<duizhan> list = new List<duizhan>();
            if (_dr.HasRows)
            {
                while (_dr.Read())
                {
                    //造一个duizhan对象
                    duizhan data = new duizhan();
                    data.Code = _dr[0].ToString();
                    data.Name = _dr[1].ToString();
                    data.Sex = _dr[2].ToString();
                    data.Blood = (int)_dr[3];
                    data.Attack = (int)_dr[4];
                    data.Defence = (int)_dr[5];
                    data.Mingzhong = (int)_dr[6];
                    data.Shanbi = (int)_dr[7];
                    data.Speed = (int)_dr[8];
                    data.Experience = (int)_dr[9];
                    data.Lv = (int)_dr[10];
                    //扔到集合里面
                    list.Add(data);
                }
            }
            _conn.Close();
            return list;
        }
        //更改经验数据
        public bool update(int experience, string name)
        {
            _cmd.CommandText = "update duizhan set experience =‘" + experience + "‘ where name=@name";
            _cmd.Parameters.Clear();
            _cmd.Parameters.AddWithValue("@name", name);
            _conn.Open();
            int n = _cmd.ExecuteNonQuery();
            _conn.Close();
            if (n > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //升级
        public bool update(string name)
        {
            List<duizhan> list = Select(name);
            
            list[0].Blood += 300;
            list[0].Attack += 50;
            list[0].Defence += 10;
            list[0].Lv += 1;
            _cmd.CommandText = "update duizhan set  Blood=" + list[0].Blood + ",Attack=" + list[0].Attack + ",Defence=" + list[0].Defence  + ",lv=" + list[0].Lv+ " where Name=@name";
            _cmd.Parameters.Clear();
            _cmd.Parameters.AddWithValue("@name", name);
            _conn.Open();
            int n = _cmd.ExecuteNonQuery();
            _conn.Close();
            if (n > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //删除程序
        public bool delete(string name)
        {
            _cmd.CommandText = "delete  from duizhan where name=@name";
            _cmd.Parameters.Clear();
            _cmd.Parameters.AddWithValue("@name", name);
            _conn.Open();
            int n = _cmd.ExecuteNonQuery();
            _conn.Close();
            if (n > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //对战
        public List<duizhan> PK(List<duizhan> list1, List<duizhan> list2)
        {
            int s1 = list1[0].Speed;
            int s2 = list2[0].Speed;
            while (list1[0].Blood > 0 && list2[0].Blood > 0)
            {
                Random mz = new Random();
                while (s1 != 0 && s2 != 0)
                {
                    s1--;
                    s2--;
                   
                }
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine(list1[0].Name + "速度值:" + s1 +"---"+ list2[0].Name + "速度值:" + s2);
                Console.ForegroundColor = ConsoleColor.Black;
                if (s1 == 0)
                {
                    Console.WriteLine(list1[0].Name + "攻击");
                    if (mz.Next(101) < list1[0].Mingzhong)
                    {
                        Random sb = new Random();
                        if (sb.Next(101) < list2[0].Shanbi)
                        {
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine(list1[0].Name + "未命中");
                            Console.ForegroundColor = ConsoleColor.Black;
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            list2[0].Blood = list2[0].Blood - list1[0].Attack * (1-(list2[0].Defence / 300));
                            Console.WriteLine(list1[0].Name + "输出" + (list1[0].Attack * (1 - list2[0].Defence / 300)) + "伤害");
                            Console.ForegroundColor = ConsoleColor.Black;
                        }
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine(list1[0].Name + "未命中");
                        Console.ForegroundColor = ConsoleColor.Black;
                    }
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(list2[0].Name + "血量:" + (list2[0].Blood < 0 ? 0 : list2[0].Blood) + "———" + list1[0].Name + "血量:" + (list1[0].Blood < 0 ? 0 : list1[0].Blood));
                    Console.ForegroundColor = ConsoleColor.Black;
                    s1 = list1[0].Speed;
                }
                else if (s2 == 0)
                {
                    Console.WriteLine(list2[0].Name + "攻击");
                    if (mz.Next(101) < list2[0].Mingzhong)
                    {
                        Random sb = new Random();
                        if (sb.Next(101) < list1[0].Shanbi)
                        {
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine(list2[0].Name + "未命中");
                            Console.ForegroundColor = ConsoleColor.Black;
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            list1[0].Blood = list1[0].Blood - (list2[0].Attack * (1-(list1[0].Defence / 300)));
                            Console.WriteLine(list2[0].Name + "输出" + (list2[0].Attack * (1 - (list1[0].Defence / 300))) + "伤害");
                            Console.ForegroundColor = ConsoleColor.Black;
                        }
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine(list2[0].Name + "未命中");
                        Console.ForegroundColor = ConsoleColor.Black;
                    }
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(list2[0].Name + "血量:" + (list2[0].Blood < 0 ? 0 : list2[0].Blood) + "———" + list1[0].Name + "血量:" + (list1[0].Blood < 0 ? 0 : list1[0].Blood));
                    Console.ForegroundColor = ConsoleColor.Black;
                    s2 = list2[0].Speed;
                }
                System.Threading.Thread.Sleep(1000);
                Console.WriteLine();
            }
            if (list1[0].Blood < 0)
            {
                List<duizhan> fanhui = list2;
                return fanhui;
            }
            else
            {
                List<duizhan> fanhui = list1;
                return fanhui;
            }
        }
    }
}
主函数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace 对战
{
    class Program
    {
        
        static void Main(string[] args)
        {
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;
            string[] AR = new string[] { "code", "姓名", "性别", "血量", "攻击", "防御", "命中", "闪避", "速度", "经验", "等级" };
            Console.WriteLine("添加还是查询?添加输入1,查询输入2,战斗输入3");
            string s = Console.ReadLine();
            duizhanDA da = new duizhanDA();
            string code = "";
            string name = "";
            if (s == "1")
            {
                //添加数据
                while (true)
                {
                    Console.WriteLine("添加数据程序");
                    Console.WriteLine("请输入您要添加的成员代号");
                    code = Console.ReadLine();
                    Console.WriteLine("请输入您要添加的成员姓名");
                    name = Console.ReadLine();
                    Console.WriteLine("请输入您要添加的成员性别");
                    string sex = Console.ReadLine();
                    if (da.Add(code, name, sex))
                    {
                        Console.WriteLine("添加成功");
                    }
                    else
                    {
                        Console.WriteLine("添加失败");
                    }
                    Console.WriteLine("是否继续添加,继续请输入1,跳出输入任意键");
                    string a = Console.ReadLine();
                    if (a == "1")
                    {
                        Console.WriteLine("继续输入");
                    }
                    else
                    {
                        Console.WriteLine("程序跳出");
                        break;
                    }
                }
            }
            else if (s == "2")
            {
                Console.WriteLine("输出属性");
                Console.WriteLine("可查询的人员名单:");
                List<duizhan> List = new List<duizhan>();
                while (true)
                {
                    List = da.Select();
                    if (List.Count() != 0)
                    {
int i = 0;
                        while (i < List.Count())
                        {
                            Console.Write(List[i].Name + "\t");
                            i++;
                        }
                        Console.Write("\n");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("输入错误,请重新输入");
                        code = Console.ReadLine();
                    }
}
                Console.WriteLine("请输入要查询的姓名");
                name = Console.ReadLine();
     
                while (true)
                {
                    List = da.Select(name);
                    if (List.Count() != 0)
                    {
                        int i = 0;
                        while (i < 11)
                        {
                            Console.Write(AR[i] + "\t");
                            i++;
                        }
                        Console.Write("\n");
                        Console.Write(List[0].Code + "\t");
                        Console.Write(List[0].Name + "\t");
                        //Console.Write(((List[0].Sex == "True") ? "男" : "女") + "\t");    
                        Console.Write(List[0].Sex + "\t");
                        Console.Write(List[0].Blood + "\t");
                        Console.Write(List[0].Attack + "\t");
                        Console.Write(List[0].Defence + "\t");
                        Console.Write(List[0].Mingzhong + "\t");
                        Console.Write(List[0].Shanbi + "\t");
                        Console.Write(List[0].Speed + "\t");
                        Console.Write(List[0].Experience + "\t");
                        Console.Write(List[0].Lv + "\t");
                        Console.Write("\n");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("输入错误,请重新输入");
                        code = Console.ReadLine();
                    }
                }
            }
            else if (s == "3")
            {
                Console.WriteLine("战斗序列");
                Console.WriteLine("可以选择的对战人员为:");
                List<duizhan> List = new List<duizhan>();
                while (true)
                {
                    List = da.Select();
                    if (List.Count() != 0)
                    {
int i = 0;
                        while (i < List.Count())
                        {
                            Console.Write(List[i].Name + "\t");
                            Console.Write("等级"+List[i].Lv + "\t");
                            i++;
                        }
                        Console.Write("\n");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("输入错误,请重新输入");
                        code = Console.ReadLine();
                    }
}
                Console.WriteLine("请输入参与战斗的人员");
                Console.WriteLine("请输入第一位参加战斗的人员姓名");
                string name1 = Console.ReadLine();
                Console.WriteLine("请输入第二位参加战斗的人员姓名");
                string name2 = Console.ReadLine();
                List<duizhan> List1 = da.Select(name1);
                List<duizhan> List2 = da.Select(name2);
                List<duizhan> jieguo = da.PK(List1, List2);
                Console.WriteLine(jieguo[0].Name + "胜利");
                int experience = 0;
                if (jieguo[0].Name == List1[0].Name)
                {
                    experience = 50 * List2[0].Lv + List1[0].Experience;
                    da.update(experience, List1[0].Name);
                    Console.WriteLine(jieguo[0].Name+"获得经验"+experience);
                }
                else
                {
                    experience = 50 * List1[0].Lv + List2[0].Experience;
                    da.update(experience, List2[0].Name);
                    Console.WriteLine(jieguo[0].Name + "获得经验" + experience);
                }
                //升级需要经验50,100,200,400,800,1600,3200,6400,12800
                int[] lvexp = new int[] {0, 50,100,200,400,800,1600,3200,6400,12800};
                List<duizhan> uplv = da.Select(jieguo[0].Name);
                int lv = uplv[0].Lv;
                while (true)
                {
                    if (lvexp[lv] <= uplv[0].Experience)
                    {
                        Console.WriteLine("升级了");
                        da.update(uplv[0].Name);
                        Console.WriteLine("属性改变为:");
                        while (true)
                        {
                            List = da.Select(uplv[0].Name);
                            if (List.Count() != 0)
                            {
                                int i = 0;
                                while (i < 11)
                                {
                                    Console.Write(AR[i] + "\t");
                                    i++;
                                }
                                Console.Write("\n");
                                Console.Write(List[0].Code + "\t");
                                Console.Write(List[0].Name + "\t");
                                //Console.Write(((List[0].Sex == "True") ? "男" : "女") + "\t");    
                                Console.Write(List[0].Sex + "\t");
                                Console.Write(List[0].Blood + "\t");
                                Console.Write(List[0].Attack + "\t");
                                Console.Write(List[0].Defence + "\t");
                                Console.Write(List[0].Mingzhong + "\t");
                                Console.Write(List[0].Shanbi + "\t");
                                Console.Write(List[0].Speed + "\t");
                                Console.Write(List[0].Experience + "\t");
                                Console.Write(List[0].Lv + "\t");
                                Console.Write("\n");
                                break;
                            }                           
                        }
                        lv++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            else
            {
                Console.WriteLine("删除数据");
            }
Console.ReadLine();
        }
    }
}
原文:http://www.cnblogs.com/kun-boke/p/5880372.html