Table of Contents
AddInfo
Home Database Mysql Tutorial mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)

mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)

Jun 07, 2016 pm 03:11 PM
operate data database

1:添加控制器类文件HomeController.cs其代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MvcTestData.Models; namespace MvcTestData.Controllers{ public class HomeContr

1:添加控制器类文件HomeController.cs其代码如下:

mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)

<span>using</span><span> System;
</span><span>using</span><span> System.Collections.Generic;
</span><span>using</span><span> System.Linq;
</span><span>using</span><span> System.Web;
</span><span>using</span><span> System.Web.Mvc;
</span><span>using</span><span> MvcTestData.Models;
</span><span>namespace</span><span> MvcTestData.Controllers
{
    </span><span>public</span> <span>class</span><span> HomeController : Controller
    {
        </span><span>//</span>
        <span>//</span><span> GET: /Home/</span>

        <span>public</span><span> ActionResult Index()
        {
            TestDataContext txtData </span>= <span>new</span><span> TestDataContext();
            </span><span>var</span> result=<span>from</span> info <span>in</span><span> txtData.StuTable
                       </span><span>select</span><span> info;
            ViewData[</span><span>"</span><span>data</span><span>"</span>] =<span> result;
            </span><span>return</span><span> View(result);
        }

        </span><span>public</span><span> ActionResult Add(FormCollection form)
        {
            </span><span>string</span> id =form[<span>"</span><span>StuId</span><span>"</span><span>];
            </span><span>string</span> name=form[<span>"</span><span>StuName</span><span>"</span><span>];
            </span><span>string</span> sex = form[<span>"</span><span>StuSex</span><span>"</span><span>];
            </span><span>int</span> age = <span>int</span>.Parse(form[<span>"</span><span>StuAge</span><span>"</span><span>]);
            </span><span>string</span> address = form[<span>"</span><span>StuAddress</span><span>"</span><span>];

            StuTable stu </span>= <span>new</span><span> StuTable();
            stu.StuId </span>=<span> id;
            stu.StuName </span>=<span> name;
            stu.StuSex </span>=<span> sex;
            stu.StuAge </span>=<span> age;
            stu.StuAddress </span>=<span> address;

            </span><span>try</span><span>
            {
                </span><span>using</span> (<span>var</span> db = <span>new</span><span> TestDataContext())
                {
                    db.StuTable.InsertOnSubmit(stu);
                    db.SubmitChanges();
                    ViewData[</span><span>"</span><span>result</span><span>"</span>] = <span>"</span><span>ok</span><span>"</span><span>;
                }
            }
            </span><span>catch</span><span> 
            {
                ViewData[</span><span>"</span><span>result</span><span>"</span>] = <span>"</span><span>fail</span><span>"</span><span>;
                </span><span>throw</span><span>;
            }
            </span><span>return</span> View(<span>"</span><span>Add</span><span>"</span><span>);
        }

        </span><span>public</span><span> ViewResult AddInfo()
        {
            </span><span>return</span> View(<span>"</span><span>AddInfo</span><span>"</span><span>);
        }

        </span><span>public</span><span> ViewResult Delete()
        {
            </span><span>int</span> id = Int16.Parse(Request.Form[<span>"</span><span>id</span><span>"</span><span>]);
            </span><span>try</span><span>
            {
                </span><span>using</span> (<span>var</span> db = <span>new</span><span> TestDataContext())
                {
                    db.StuTable.DeleteOnSubmit(db.StuTable.First(info </span>=> info.ID ==<span> id));
                    db.SubmitChanges();
                    ViewData[</span><span>"</span><span>result</span><span>"</span>] = <span>"</span><span>ok</span><span>"</span><span>;
                }
            }
            </span><span>catch</span><span>
            {
                ViewData[</span><span>"</span><span>result</span><span>"</span>] = <span>"</span><span>fail</span><span>"</span><span>;
                </span><span>throw</span><span>;
            }
            </span><span>return</span> View(<span>"</span><span>Delete</span><span>"</span><span>);
        }

       

    }
}</span>
Copy after login
View Code

 

2:为models文件夹添加linq to sql 类文件然后把数据库中的表copy 进来

mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)
3:为控制器中的Action添加各自的视图

mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)

4 视图Index.cshtml的代码

mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)

<span>@using MvcTestData.Models
</span>


    <title>Index</title>


    <div>
    <table border="<span">"<span>0</span><span>"</span> cellspacing=<span>"</span><span>0</span><span>"</span> cellpadding=<span>"</span><span>0</span><span>"</span> width=<span>"</span><span>100%</span><span>"</span> style=<span>"</span><span>text-align:center</span><span>"</span> >
        <tr>
            <th>序号</th>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>住址</th>
<th>操作</th>
        </tr>
<span>
@foreach (StuTable info </span><span>in</span> (ViewData[<span>"</span><span>data</span><span>"</span>] <span>as</span> IEnumerable<stutable><span>))
{
   </span><tr>
        <td>@info.ID</td>
        <td>@info.StuId </td>
        <td>@info.StuName </td>
        <td>@info.StuSex </td>
        <td>@info.StuAge </td>
        <td>@info.StuAddress </td>
        <td>
            <form action="<span">"<span>/Home/Delete</span><span>"</span> method=<span>"</span><span>post</span><span>"</span>>
                <input type="<span">"<span>hidden</span><span>"</span> name=<span>"</span><span>id</span><span>"</span> value=<span>"</span><span>@info.ID</span><span>"</span>/>
                <input type="<span">"<span>submit</span><span>"</span> value=<span>"</span><span>删除</span><span>"</span>/>
              
            </form>
             
        </td>
   </tr>
<span>
}
          </span></stutable>
</table>
        <br><span>
        @Html.ActionLink(</span><span>"</span><span>添加个人信息</span><span>"</span>,<span>"</span><span>AddInfo</span><span>"</span>,<span>"</span><span>Home</span><span>"</span><span>)
    </span>
</div>

Copy after login
View Code

5 视图 Add.cshtml的代码

mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)

<span>@model MvcTestData.Models.StuTable

@{
    ViewBag.Title </span>= <span>"</span><span>Add</span><span>"</span><span>;
}
@if(ViewData[</span><span>"</span><span>result</span><span>"</span>].Equals(<span>"</span><span>ok</span><span>"</span><span>))
{
    </span><p>添加成功</p><span>
}
</span><span>else</span><span>
{
    </span><p>添加失败</p><span>
}</span>
Copy after login
View Code

6 视图AddInfo.cshtml代码

mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)

<span>@model MvcTestData.Models.StuTable

@{
    ViewBag.Title </span>= <span>"</span><span>AddInfo</span><span>"</span><span>;
}

</span><h2 id="AddInfo">AddInfo</h2>
<span>
@using(Html.BeginForm(</span><span>"</span><span>Add</span><span>"</span>,<span>"</span><span>Home</span><span>"</span><span>,FormMethod.Post))
{
    </span><p>Student 学号:@Html.TextBoxFor(x=>x.StuId)</p>
    <p>Student 姓名:@Html.TextBoxFor(x=>x.StuName)</p>
    <p>Student 性别:@Html.TextBoxFor(x=>x.StuSex)</p>
    <p>Student 年龄:@Html.TextBoxFor(x=>x.StuAge)</p>
    <p>Student 住址:@Html.TextBoxFor(x=>x.StuAddress)</p>
    <input type="<span">"<span>submit</span><span>"</span> value=<span>"</span><span>Add</span><span>"</span> /><span>
}

 </span>
Copy after login
View Code

7 视图Delete.cshtml代码

mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)

<span>@model MvcTestData.Models.StuTable

@{
    ViewBag.Title </span>= <span>"</span><span>Delete</span><span>"</span><span>;
}

@if (ViewData[</span><span>"</span><span>result</span><span>"</span>].Equals(<span>"</span><span>ok</span><span>"</span><span>))
{
    </span><p>删除成功</p><span>
}
</span><span>else</span><span>
{
   </span><p>删除失败</p><span>
}</span>
Copy after login
View Code

8 最终测试结果图:

mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)

mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. May 07, 2024 pm 05:00 PM

Recently, the military circle has been overwhelmed by the news: US military fighter jets can now complete fully automatic air combat using AI. Yes, just recently, the US military’s AI fighter jet was made public for the first time and the mystery was unveiled. The full name of this fighter is the Variable Stability Simulator Test Aircraft (VISTA). It was personally flown by the Secretary of the US Air Force to simulate a one-on-one air battle. On May 2, U.S. Air Force Secretary Frank Kendall took off in an X-62AVISTA at Edwards Air Force Base. Note that during the one-hour flight, all flight actions were completed autonomously by AI! Kendall said - "For the past few decades, we have been thinking about the unlimited potential of autonomous air-to-air combat, but it has always seemed out of reach." However now,

AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! Jun 08, 2024 pm 01:00 PM

Last week, amid the internal wave of resignations and external criticism, OpenAI was plagued by internal and external troubles: - The infringement of the widow sister sparked global heated discussions - Employees signing "overlord clauses" were exposed one after another - Netizens listed Ultraman's "seven deadly sins" Rumors refuting: According to leaked information and documents obtained by Vox, OpenAI’s senior leadership, including Altman, was well aware of these equity recovery provisions and signed off on them. In addition, there is a serious and urgent issue facing OpenAI - AI safety. The recent departures of five security-related employees, including two of its most prominent employees, and the dissolution of the "Super Alignment" team have once again put OpenAI's security issues in the spotlight. Fortune magazine reported that OpenA

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI 70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI Jun 13, 2024 pm 03:47 PM

70B model, 1000 tokens can be generated in seconds, which translates into nearly 4000 characters! The researchers fine-tuned Llama3 and introduced an acceleration algorithm. Compared with the native version, the speed is 13 times faster! Not only is it fast, its performance on code rewriting tasks even surpasses GPT-4o. This achievement comes from anysphere, the team behind the popular AI programming artifact Cursor, and OpenAI also participated in the investment. You must know that on Groq, a well-known fast inference acceleration framework, the inference speed of 70BLlama3 is only more than 300 tokens per second. With the speed of Cursor, it can be said that it achieves near-instant complete code file editing. Some people call it a good guy, if you put Curs

Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Jun 25, 2024 pm 07:09 PM

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

China Mobile: Humanity is entering the fourth industrial revolution and officially announced 'three plans” China Mobile: Humanity is entering the fourth industrial revolution and officially announced 'three plans” Jun 27, 2024 am 10:29 AM

According to news on June 26, at the opening ceremony of the 2024 World Mobile Communications Conference Shanghai (MWC Shanghai), China Mobile Chairman Yang Jie delivered a speech. He said that currently, human society is entering the fourth industrial revolution, which is dominated by information and deeply integrated with information and energy, that is, the "digital intelligence revolution", and the formation of new productive forces is accelerating. Yang Jie believes that from the "mechanization revolution" driven by steam engines, to the "electrification revolution" driven by electricity, internal combustion engines, etc., to the "information revolution" driven by computers and the Internet, each round of industrial revolution is based on "information and "Energy" is the main line, bringing productivity development

See all articles