ADO.NET对战练习

数据连接类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
class dzconnect
{
private static string connstring = System.Configuration.ConfigurationSettings.AppSettings["connstring"];
public static SqlConnection conn
{
get
{
return new SqlConnection(connstring);
}
}
}
}

实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
public class dzb
{
private int ids;

public int Ids
{
get { return ids; }
set { ids = value; }
}
private string name;

public string Name
{
get { return name; }
set { name = value; }
}
private int gongji;

public int Gongji
{
get { return gongji; }
set { gongji = value; }
}
private int fangyu;

public int Fangyu
{
get { return fangyu; }
set { fangyu = 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 shengming;

public int Shengming
{
get { return shengming; }
set { shengming = value; }
}
private int dengji;

public int Dengji
{
get { return dengji; }
set { dengji = value; }
}
}
}

数据访问类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
class dzbDA
{
private SqlConnection _conn;
private SqlCommand _cmd;
private SqlDataReader _dr;

public dzbDA()
{
_conn = dzconnect.conn;
_cmd = _conn.CreateCommand();
}
public bool add(string name)
{

int seed = (int)Convert.ToChar(name.Substring(0, 1)) + (int)Convert.ToChar(name.Substring(1, 1));
Random rand = new Random(seed);
int shengming = 5000 + rand.Next(5000);
int gongji = 500 + rand.Next(500);
int fangyu = 500 + rand.Next(500);
int mingzhong = 50 + rand.Next(50);
int shanbi = 50 + rand.Next(50);

_cmd.CommandText = "insert into dzb values(@name,@gongji,@fangyu,@mingzhong,@shanbi,@shengming)";
_cmd.Parameters.Clear();
_cmd.Parameters.AddWithValue("@name", name);
_cmd.Parameters.AddWithValue("@gongji", gongji);
_cmd.Parameters.AddWithValue("@fangyu", fangyu);
_cmd.Parameters.AddWithValue("@mingzhong", mingzhong);
_cmd.Parameters.AddWithValue("@shanbi", shanbi);
_cmd.Parameters.AddWithValue("@shengming", shengming);
_conn.Open();
int n = _cmd.ExecuteNonQuery();

if (n > 0)
{
return true;
}
else
{
return false;
}

}
public List<dzb> select(string name)
{
_cmd.CommandText = "select * from dzb where [email protected]";
_cmd.Parameters.Clear();
_cmd.Parameters.AddWithValue("@name", name);

_conn.Open();
_dr = _cmd.ExecuteReader();

List<dzb> list = new List<dzb>();
if (_dr.HasRows)
{
while (_dr.Read())
{

dzb data = new dzb();

data.Name = _dr[1].ToString();
data.Gongji =Convert.ToInt32 (_dr[2]);
data.Fangyu = Convert.ToInt32(_dr[3]);
data.Shanbi = Convert.ToInt32(_dr[4]);
data.Mingzhong = Convert.ToInt32(_dr[5]);
data.Shengming = Convert.ToInt32(_dr[6]);
list.Add(data);
}
}
_conn.Close();
return list;

}
}
}

具体操作表

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{

class Program
{
static dzb pk(dzb a1, dzb a2)
{
Console.WriteLine(a1.Name + "攻击" + a2.Name);
System.Threading.Thread.Sleep(3000);
Random rand = new Random();
int gongji1 = a1.Gongji + rand.Next(500);

int mingzhong1 = a1.Mingzhong + rand.Next(50);

int fangyu2 = a2.Fangyu + rand.Next(100);

int shanbi2 = a2.Shanbi + rand.Next(10);

if (mingzhong1 - shanbi2 > 0)
{
if (gongji1 - fangyu2 > 0)
{
a2.Shengming = a2.Shengming - (gongji1 - fangyu2)*5;
Console.WriteLine(a1.Name + "打掉" +a2.Name + (gongji1 - fangyu2)*5 + "点血");
}
if (gongji1 - fangyu2 < 0)
{
Console.WriteLine("未破防");
}

}
else
{
Console.WriteLine("未命中");
}
return a2;
}

static void Main(string[] args)
{
Console.WriteLine("输入1添加,输入2战斗");
string a = Console.ReadLine();
if (a == "1")
{
Console.WriteLine("请输入玩家:");
string name = Console.ReadLine();
dzbDA da = new dzbDA();
if (da.add(name))
{
Console.WriteLine("添加成功!");
}
else
{
Console.WriteLine("添加失败!");
}
}
if (a == "2")
{
dzbDA da = new dzbDA();
Console.WriteLine("请输入玩家1:");
string name1 = Console.ReadLine();
Console.WriteLine("请输入玩家2:");
string name2 = Console.ReadLine();
List<dzb> list1 = da.select(name1);
foreach (dzb data in list1)
{
Console.WriteLine(data.Name + "__" + data.Gongji + "__" + data.Fangyu + "__" + data.Shanbi + "__" + data.Mingzhong + "__" + data.Shengming);
}
List<dzb> list2 = da.select(name2);
foreach (dzb data in list2)
{
Console.WriteLine(data.Name + "__" + data.Gongji + "__" + data.Fangyu + "__" + data.Shanbi + "__" + data.Mingzhong + "__" + data.Shengming);
}

while (list1[0].Shengming> 0 &&list2[0].Shengming> 0)
{
dzb pk2 = pk(list1[0], list2[0]);
list2[0] = pk2;
dzb pk1 = pk(list2[0], list1[0]);
list1[0] = pk1;
Console.WriteLine(list2[0].Name + ":" + (list2[0].Shengming < 0 ? 0 : list2[0].Shengming) + "----------" + list1[0].Name + ":" + (list1[0].Shengming < 0 ? 0 : list1[0].Shengming));
Console.WriteLine();
}
Console.WriteLine("战斗结束");
if (list1[0].Shengming > 0 && list2[0].Shengming <= 0)
{
Console.WriteLine(list1[0].Name+"赢了");
}
else if (list1[0].Shengming <= 0 && list2[0].Shengming > 0)
{
Console.WriteLine(list2[0].Name + "赢了");
}
else if (list1[0].Shengming <= 0 && list2[0].Shengming <= 0)
{
Console.WriteLine("同归于尽");
}
}

Console.ReadLine();
}
}

时间: 2024-10-26 02:38:53

ADO.NET对战练习的相关文章

ADO.NET 扩展属性、配置文件 和 对战游戏

扩展属性 有外键关系时将信息处理成用户可看懂的 利用扩展属性 如:Info表中的民族列显示的是民族代号处理成Nation表中的民族名称 需要在Info类里面扩展一个显示nation名称的属性 例:先前在NationDA里面已经下好了一个代号换成名称的方法 public class Info { //扩展的名族名称属性 public string NationName { get { NationDA da = new NationDA(); return da.NationName(this.n

ODBC、ADO

一.VC访问数据库的技术 1.ODBC --Open Database Connectivity微软开放式数据互联,一组用于访问和操作数据库的API,可以访问不同的数据库产品,但只能访问关系型数据库.MFC将这组API函数封装成ODBC类,使用前需要将不同的数据库设置为ODBC数据源. 2.DAO --基于ODBC的,目前已经被淘汰 3.OLE DB  --基于COM技术的,提供了一组用于访问和操作数据库的接口.既可以访问关系型又可以访问非关系型数据,性能也有了很大提高.缺点是学习难度大,对程序

一种利用ADO连接池操作MySQL的解决方案(VC++)

VC++连接MySQL数据库 常用的方式有三种:ADO.mysql++,mysql API ; 本文只讲述ADO的连接方式. 为什么要使用连接池? 对于简单的数据库应用,完全可以先创建一个常连接(此连接永远不关闭,直接数进程退出),但是这样做至少会引起两个问题:(1)资源竞争,多个数据库请求操作不能同时进行,后一请求必须要等到前一请求完成后才能进行:(2)多线程情况下容易出现混乱,甚至出现资源异常释放.还有一种方法,就是使用数据库时创建连接,使用完后关闭连接回收资源.这种方式在数据库操作频繁的情

巩固ADO.NET

回想一年半前学数据库的时候是找的培训视频看的,大概10天视频(有一半多时间关于数据库操作的winform)的内容看完后数据库这块就没有了,还以为自己学的挺好,现在想想真是傻的可爱.虽然以ADO.NET为基础的EF挺好用的,但抽时间好好巩固下基础我觉得很重要.不废话了,ADO.NET提供对SQL.Oracle和MySql等数据库的访问操作,它有2个重要的组成部分:.NET数据提供程序和数据集DataSet. 1.什么是ADO.NET .NET数据提供程序就是一个类集,本质的功能就是用于连接数据库.

CRUD Operations In ASP.NET MVC 5 Using ADO.NET

Background After awesome response of an published by me in the year 2013: Insert, Update, Delete In GridView Using ASP.Net C#. It now has more than 140 K views, therefore to help beginners I decided to rewrite the article i with stepbystep approach u

二人对战游戏 结构体+函数

class 对战713 { struct Player { public string Name; public int Blood; public int Attact; public int Defence; public int DuoBi; public ArrayList JiNeng; public ArrayList ShangHai; } static void Main (string[] args) { #region 定义技能库 string[] JN; int[] SH;

【UOJ】【UR #2】猪猪侠再战括号序列(splay/贪心)

http://uoj.ac/problem/31 纪念伟大的没有调出来的splay... 竟然那个find那里写错了!!!!!!!!!!!!! 以后要记住:一定要好好想过! (正解的话我就不写了,太简单了.. #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #

异常:error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name &#39;System.Data.SqlClient&#39;

error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient' error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the pro

ADO.NET

1.vb.net: vb.net自身并不具备对数据库进行操作的功能,它对数据库的处理通过.NET Framwork SDK中面向数据库编程的类库和微软的MDAC 来实现的,其中ADO.NET 是.NET Framwork SDK重要组成部分.它是.NET Framwork SDK中用以操作数控的类库的总称. 2.ado.net     1.命名空间:在使用前先引用 imports system.data imports system.sqlclient 2.ado.net的核心内容 Connec