码道至简

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

一、连接数据库

新建一个MVC应用程序(基本)

建一个HomeController

为Index添加视图

 

@{

    ViewBag.Title = "Index";

}

 

<h2>Index</h2>

@using(Html.BeginForm("Indexa","Home")) {

 

  <b>id:</b>  @Html.TextBox("id",1);

  <b>name:</b> @Html.TextBox("name");

 

<button type="submit">提交</button>

}

 

在HomeController里新建一个Action 写入连接数据库的代码

public ActionResult Indexa()

        {

            SqlConnection conn = new SqlConnection();

            conn.ConnectionString = @"Data Source=.;Initial Catalog=paiqiu;Integrated Security=True";

            SqlCommand comm = new SqlCommand();

            comm.Connection = conn;

            string aa=Request["id"].ToString();

            int c = int.Parse(aa);

            string aaa = Request["name"].ToString();

         

            comm.CommandText = "insert into yifu (id,name) values ("+c+",'"+aaa+"')";

            conn.Open();

            int a = comm.ExecuteNonQuery();

            conn.Close();

            if (a > 0)

            {

                ViewBag.S = "插入成功";

                ViewBag.J = "插入结果为";

                comm.CommandText = "select *from yifu";

                conn.Open();

                SqlDataReader dr = comm.ExecuteReader();

                if (dr.HasRows)

                {

                    ViewBag.a = "<table><tr><td>编号</td><td>名字</td></tr>";

                    while (dr.Read())

                    {

                        ViewBag.a += "<tr><td>" + dr["id"] + "</td><td>" + dr["name"] + "</td></tr>";

                    }

                }

                ViewBag.a += "</table>";

 

 

            }

            else

            {

                ViewBag.a += "插入失败";

                ViewBag.S = "";

                ViewBag.J = "";

            }

            return View();

        }

二 、 基本得分的实现方法

新建一个Action

public ActionResult Indexb()
        {
            
            return View();
        }

为它添加视图

@{
    ViewBag.Title = "Indexb";
}

<h2>Indexb</h2>
@using (Html.BeginForm("Indexc", "Home"))
{ 
    string str=ViewBag.ccc;
    if (string.IsNullOrEmpty(str))
    {
        str = "0";
    }
    @Html.TextBox("number",str);
    <button type="submit">得分</button>
}

再添加一个Action

   

   public ActionResult Indexc()

        {

           int a=int.Parse( Request["number"]);



           if (a < 5)

           {

               a++;

           }

            ViewBag.ccc = a.ToString();

            return View("Indexb");

        }

这是这个排球计分程序的基本得分的技术实现

 

经过一系列判断就会是一个比较复杂的程序

@{

    ViewBag.Title = "Indexb";

}

 

<h2>Indexb</h2>

@using (Html.BeginForm("Indexc", "Home"))

{

    string str=ViewBag.ccc;

    if (string.IsNullOrEmpty(str))

    {

        str = "0";

    }

    @Html.TextBox("number",str);

    <buttontype="submit">得分</button>

}

再添加一个Action

      publicActionResult Indexc()

        {

           int a=int.Parse( Request["number"]);

 

           if (a < 5)

           {

               a++;

           }

            ViewBag.ccc = a.ToString();

            return View("Indexb");

        }

posted on 2017-06-24 22:04  码道至简  阅读(199)  评论(0编辑  收藏  举报