mvc3.0 +linq 操作数据库中表的数据(ps:本人菜鸟刚学)
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其代码如下:
<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>
2:为models文件夹添加linq to sql 类文件然后把数据库中的表copy 进来
3:为控制器中的Action添加各自的视图
4 视图Index.cshtml的代码
<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>
5 视图 Add.cshtml的代码
<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>
6 视图AddInfo.cshtml代码
<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>
7 视图Delete.cshtml代码
<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>
8 最终测试结果图:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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,

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

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

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())

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, 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

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,

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
