关于asp.net sql连接数据库语句 sql语句update set中set选取控件的值

asp.net使用LINQ to SQL连接数据库及SQL操作语句用法分析
来源:易贤网&& 阅读:372 次&&日期: 14:44:11
温馨提示:易贤网小编为您整理了“asp.net使用LINQ to SQL连接数据库及SQL操作语句用法分析”,方便广大网友查阅!
本文实例讲述了asp.net使用LINQ to SQL连接数据库及SQL操作语句用法。分享给大家供大家参考,具体如下:
LINQ:语言集成查询(Language INtegrated Query)是一组用于c#和Visual Basic语言的扩展。它允许编写C#或者Visual Basic代码以查询数据库相同的方式操作内存数据。
LINQ是一门查询语言,和SQL一样,通过一些关键字的组合,实现最终的查询。
LINQ的分类
LINQ to Object
LINQ to XML
LINQ to SQL
LINQ to DataSet
LINQ to ADO.NET
命名空间为System.L
from 临时变量 in 集合对象或数据库对象
where 条件表达式
[orderby条件]
[group by 条件]
select 临时变量中被查询的值
from c in S
假设Student是一个数据库表对应的一个实体类
则查询语句为:
from c in S
//整表查询
from c in Student where c.name=="张三"
//查询姓名为张三的所有信息
其中C为临时变量,可任意取。
查询几个字段
1、查询student表中的几个字段
var query=from c in student select new {c.number,c.name,c.age};
2、查询student表中的几个字段,并重新设定列名
var query=from c in student select new {学号=c.number,姓名=c.name, 年领=c.age};
linq查询语句必须以from子句开始,以select 子句结束。
Linq是在.NET Framework 3.5 中出现的技术,所以在创建新项目的时候必须要选3.5或者更高版本,否则无法使用。
var query=from c in student orderby c.ag//升序
var query=from c in studeng orderby c.age//降序
var query=from c in student group c by c.sex into d select new {性别=c.age}; //d为新表,c.sex为分组字段
5、过滤重复记录
var query=(from c in dc.student select new {c.place}).Distinct();//Distinct()的作用是过滤重复的记录。
var query=(from c in dc.student select new {分布地区=c.place}).Distinct();
6、查询行数
(1)查询表的总行数
int count=student.count();
(2)查询满足条件的行数
int count=(from c in student where c.name=="王明" select c).count();
7、模糊查询
from c in dc.Student where c.name.Contain("王") select c
查询姓名中含有王字的所有学生
var query=from c in dc.Student where c.number.Contain("2009") select c
查询学号中含有2009字符的所有学生
LINQ的查询结果有可能是一个对象,也有可能是一个数据集,可用var类型进行接收
var query=from c in S
输入结果可用foreach循环
var query=from c in S
foreach( var x in query)
{ Response.Write(x.toString());}
Count( ):计算查询结果的行数
Distinct( ):对查询结果的重复行进行筛选
First( ):取得查询结果的第一行
Last( ):取得查询结果的最后一行
Take(n):取得查询结果的前n行
Skip(n):略过前n行,从n+1行开始取
Skip(m).Take(n):从m+1行开始取后面的n行
8、更新操作
思路:先把需要更新的行查询出来,然后进行更新。LINQ只需要写出查询语句即可,不需要写更新语句!
例:将学生表中学号为00001的学生进行更新
1、(from c in Stuent where c.id=="00001" select c).First();
在数据空间中显示数据查询结果:
前两行是连接数据库,其中第一中,经常用,可以放到最开始,这样就不必每次用到时都写了。
studentDataContext dc = new studentDataContext();
//注意:xxxDataContext需要与.dbml的文件名一致
var query=from c in dc.
GridView1.DataSource=
GridView1.DataBind();
string num = TextBox2.Text.Trim(); //将要更新学号为多少的相关信息
string name = TextBox3.Text.Trim();//更新的姓名
int age = Convert.ToInt32(TextBox4.Text.Trim());//Int32整型 //更新的年龄
StudentDataContext dc=new StudentDataContext();
student stu=(from c in dc.student where c.number==num select c).First();//变量,选取第一行。where后根据主键来更新,其他字段不能。即通过获取主键后,来更新其他字段。
//除过主键不修改外,其他字段都可以修改
stu.name =//将新修改的名字赋值给数据库中的字段名name
stu.age =//修改年龄
dc.SubmitChanges();//真正的用于修改数据库。
bind();//一更改,就显示,和及时刷新相同。
private void bind()
studentDataContext dc = new studentDataContext();
var query = (from c in dc.student select c); //全表查询
GridView1.DataSource =
GridView1.DataBind();
9、插入操作
string num = TextBox1.Text.Trim();
string username = TextBox2.Text.Trim();
string sex = TextBox3.Text.Trim();
string place = TextBox4.Text.Trim();
int age = Convert.ToInt32(TextBox5.Text.Trim());
student stu = new student();//创建对象
//主键不能重复
stu.number =
stu.name =
stu.place =
dc.student.InsertOnSubmit(stu);
//对表studen表进行插入操作。
//注意,该函数必须写正确。
dc.SubmitChanges();//数据库保存
bind();//内容和上面的相同
10、数据删除
string num = TextBox6.Text.Trim();
student stu =(from c in dc.student where c.number == num select c).First();
dc.student.DeleteOnSubmit(stu);
//删除数据库中的字段,具体怎样删除不管,只管调用该函数即可。
dc.SubmitChanges();
希望本文所述对大家asp.net程序设计有所帮助。
更多信息请查看
【】&&&&&【点此处查询各地各类考试咨询QQ号码及交流群】
易贤网手机网站地址:
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
相关阅读 & & &
&&& &nbsp&nbsp&nbsp会员注册
本站不参与评论!()
自觉遵守:爱国、守法、自律、真实、文明的原则
尊重网上道德,遵守中华人民共和国各项有关法律法规
严禁发表危害国家安全,破坏民族团结、国家宗教政策和社会稳定,含侮辱、诽谤、教唆、淫秽等内容的评论
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
您在本站发表的评论,本站有权保留、转载、引用或者删除
参与本评论即表明您已经阅读并接受上述条款FormView显示、更新、插入、删除数据库操作[ASP.NET源代码](一)
我的图书馆
FormView显示、更新、插入、删除数据库操作[ASP.NET源代码](一)
FormView可分页呈现一个表格的数据,每页只呈现表格中的一项。它的最大特点是可自由编辑模板,一般用来显示商品的详细信息。FormView有三个可编辑模板,ItemTemplate、EditItemTemplate和InsertItemTemplate、常用来管理数据库表格数据,显示、编辑、插入、删除表格中的数据项。一、使用&FormView控件显示&SqlDataSource控件中的值
1、设置FormView控件,注意DataKeyNames="ItemID"项。2、设置SqlDataSource属性,由于要查询两个内联表,两个表中都有一个Name字段,因此用了别名。3、编辑ItemTemplate模板,先添加了“编辑”、“删除”、“新建”按钮,“编辑”和“新建”按钮都有个CommandName属性,分别为Edit和New,点击可分别进入EditItemTemplate和InsertItemTemplate、模板;删除按钮的CommandName属性是Delete,点击可执行SqlDataSource中的DeleteCommand命令,同时,还可发出OnItemDeleting等命令,在删除前完成一些功能。4、窗体文件代码如下:[html]&%@ Page Language="C#" AutoEventWireup="true" CodeFile="FormViewDemo1.aspx.cs" Inherits="FormViewDemo1" %&&&&!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""&&&html xmlns=""&&&head runat="server"&&&&& &title&肯德基订餐系统&/title&&&/head&&&body&&&&& &form id="form1" runat="server"&&&&&&&&& &h3&FormView 显示、更新、插入、删除数据库操作&/h3&&&&&&&&& &:FormView ID="fvwItem" DataSourceID="sdsItem" runat="server"&&&&&&&&&&&&& AllowPaging="True"&&&&&&&&&&&& DataKeyNames="ItemID"&&&&&&&&&&&&& EmptyDataText="数据库中暂时没有任何数据"&&&&&&&&&&&&&&&&&&&&&&&&&& &RowStyle BackColor="Yellow" Wrap="False" /&&&&&&&&&&&&& &InsertRowStyle BackColor="GreenYellow" Wrap="False" /&&&&&&&&&&&&& &EditRowStyle BackColor="LightPink" Wrap="false" /&&&&&&&&&&&&&&&&&&&&&&&&&& &ItemTemplate&&&&&&&&&&&&&&&&& &table border="0" cellpadding="0" cellspacing="0" width="420"&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td colspan="6" height="30" width="420" align="center"&&&&&&&&&&&&&&&&&&&&&&&&& &h4&FormView ItemTemplate 模板&/h4&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td rowspan="4" width="120"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:Image Width="120" Height="120" ID="imgItem" ImageUrl='&%# Eval("Image") %&' AlternateText='&%# Eval("Name") %&'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& runat="server" /&&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 类别:&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td colspan="2"&&&&&&&&&&&&&&&&&&&&&&&&& &%# Eval("CategoryName") %&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 名称:&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td colspan="2"&&&&&&&&&&&&&&&&&&&&&&&&& &%# Eval("Name") %&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 价格:&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td colspan="2"&&&&&&&&&&&&&&&&&&&&&&&&& &%# Eval("Price") %&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="120"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="60"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:Button ID="btnEdit" runat="server" Text="编辑" CommandName="Edit"/&&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:Button ID="btnDelete" runat="server" Text="删除" CommandName="Delete"/&&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:Button ID="btnNew" runat="server" Text="新建" CommandName="New" /&&/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&& &/table&&&&&&&&&&&&& &/ItemTemplate&&&&&&&&& &/asp:FormView&&//==========&&&&&&& &asp:SqlDataSource ID="sdsItem"& runat="server" ConnectionString="&%$ ConnectionStrings:NetShopConnString %&"&&&&&&&&&&&& SelectCommand="SELECT Item.ItemId AS ItemId,Item.CategoryId AS CategoryId,Item.Name AS Name,Item.Price AS Price,Item.Image AS Image,Category.Name As CategoryName FROM Item INNER JOIN Category ON Item.CategoryId=Category.CategoryId"&&&&&&&&& &/asp:SqlDataSource&&&&& &/form&&&/body&&&/html&&
&5、代码页不需要任何代码,可直接在查看运行情图,如图1示。
二、使用 FormView控件编辑数据1、编辑EditItemTemplate模板,代码如下:[html]&EditItemTemplate&&&&&&&&&&&&&&&&& &table border="0" cellpadding="0" cellspacing="0" width="420"&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td colspan="6" height="30" width="420" align="center"&&&&&&&&&&&&&&&&&&&&&&&&& &h4&FormView EditItemTemplate 模板&/h4&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td rowspan="4" width="120"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:Image ID="imgItem" runat="server" AlternateText="上传浏览图片" Height="120px" ImageUrl='&%# Eval("Image") %&'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& Width="120px" /&&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td colspan="2"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:FileUpload ID="fupImage" runat="server" Width="100%" /&&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="上传" /&&/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 分类:&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="120"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:DropDownList ID="ddlCategory" runat="server" DataSourceID="sdsCategory" DataTextField="Name"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& DataValueField="CategoryId"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &/asp:DropDownList&&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 名称:&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="120"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:TextBox ID="txtName" runat="server" Text='&%# Bind("Name") %&'&&/asp:TextBox&&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 价格:&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="120"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:TextBox ID="txtPrice" runat="server" Text='&%# Bind("Price") %&'&&/asp:TextBox&&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="120"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="60"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td align="right" height="30" width="120"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:Button ID="btnUpdate" runat="server" CommandName="Update" Text="更新" /&&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:Button ID="btnCancel" runat="server" CommandName="Cancel" Text="取消" /&&/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&& &/table&&&&&&&&&&&&& &/EditItemTemplate&&
&2、为SqlDataSource控件sdsItem添加UpdateCommand命令,并添加&UpdateParameters&[html]UpdateCommand="UPDATE Item SET
[html]&UpdateParameters&&&&& &:Parameter Name="CategoryId" /&&&&& &asp:Parameter Name="Name" /&&&&& &asp:Parameter Name="Price" /&&&&& &asp:Parameter Name="Image" /&&&&& &asp:Parameter Name="ItemId" /&&&/UpdateParameters&&
3、编辑模板加了一个FileUpload控件,可以选择并上传图片图片,为了能在上传后显示图片,添加了一个btnUpload按钮,并添加了这个按钮的响应函数,点击后,可将文件上传,并在窗体中显示上传后的图片,代码如下:[csharp]protected void btnUpload_Click(object sender, EventArgs e)&{&&&& FileUpload fup = (FileUpload)fvwItem.FindControl("fupImage");&&&&& if (fup.HasFile)&&&& {&&&&&&&& fup.SaveAs(Server.MapPath("~\\Images\\Items\\") + fup.FileName);&&&&&&&&& String str = "~\\Images\\Items\\" + fup.FileName.ToString();&&&&&&&& Image img = (Image)fvwItem.FindControl("imgItem");&&&&&&&& img.ImageUrl =&&&& }&&&& else&&&& {&&&&&&&& Response.Write("&script&alert('请先浏览并选择图片')&/script&");&&&& }&}&
&4、在模板中添加一个类别下拉列表框,为了获得一个完全的类别,只能再弄一个SqlDateSource,配置如下:[html]&asp:SqlDataSource ID="sdsCategory" runat="server" ConnectionString="&%$ ConnectionStrings:NetShopConnString %&"&&&& SelectCommand="SELECT CategoryId,Name FROM Category"&&&/asp:SqlDataSource&&
5、5、编辑模板中,CategoryID和Image等参数没有双向绑定,需要在上传前给这两个参数赋值,为些,为fvwItem添加了OnItemUpdating="fvwItem_ItemUpdating"消息响应函数,代码如下:[csharp]protected void fvwItem_ItemUpdating(object sender, FormViewUpdateEventArgs e)&{&&&& DropDownList ddl = (DropDownList)fvwItem.FindControl("ddlCategory");&&&& sdsItem.UpdateParameters["CategoryId"].DefaultValue = ddl.SelectedV&&&&& Image img = (Image)fvwItem.FindControl("imgItem");&&&& sdsItem.UpdateParameters["Image"].DefaultValue = img.ImageU&&}&
6、在中查看运行结果。
三、使用 FormView控件更新数据
1、编辑InsertItemTemplate模板,代码如下:
[html] &InsertItemTemplate&&&&&&&&&&&&&&&&& &table border="0" cellpadding="0" cellspacing="0" width="420"&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td colspan="6" height="30" width="420" align="center"&&&&&&&&&&&&&&&&&&&&&&&&& &h4&FormView InsertItemTemplate 模板&/h4&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td rowspan="4" width="120"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:Image ID="imgItem" runat="server" Width="120px" Height="120px" ImageUrl='&%# Eval("Image") %&'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& AlternateText="上传浏览图片" /&&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td colspan="2"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:FileUpload ID="fupImage" runat="server" Width="100%"/&&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:Button ID="btnUpload" Text="上传" OnClick="btnUpload_Click" runat="server"&&/asp:Button&&/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 分类:&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="120"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:DropDownList ID="ddlCategory" runat="server" DataSourceID="sdsCategory" DataTextField="Name"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& DataValueField="CategoryId"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &/asp:DropDownList&&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 名称:&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="120"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:TextBox ID="txtName" Text='&%# Bind("Name") %&' runat="server" /&&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 价格:&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="120"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:TextBox ID="txtPrice" Text='&%# Bind("Price") %&' runat="server" /&&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="120"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="30"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="60"&&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="120" align="right"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:Button ID="btnInsert" Text="插入" CommandName="Insert" runat="server" /&&/td&&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:Button ID="btnCancel" Text="取消" CommandName="Cancel" runat="server" /&&/td&&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&& &/table&&&&&&&&&&&&& &/InsertItemTemplate&&&&&&&&& &/asp:FormView&&&&&&&&& &asp:SqlDataSource ID="sdsItem"& runat="server" ConnectionString="&%$ ConnectionStrings:NetShopConnString %&"&&&&&&&&&&&& SelectCommand="SELECT Item.ItemId AS ItemId,Item.CategoryId AS CategoryId,Item.Name AS Name,Item.Price AS Price,Item.Image AS Image,Category.Name As CategoryName FROM Item INNER JOIN Category ON Item.CategoryId=Category.CategoryId"&&&&&&&&&&&& UpdateCommand="UPDATE Item SET
WHERE "&&&&&&&&&&&& InsertCommand="INSERT INTO Item(CategoryId,Name,Price,Image) VALUES (@CategoryId,@Name,@Price,@Image)"&&&&&&&&&&&&& &UpdateParameters&&&&&&&&&&&&&&&&& &asp:Parameter Name="CategoryId" /&&&&&&&&&&&&&&&&& &asp:Parameter Name="Name" /&&&&&&&&&&&&&&&&& &asp:Parameter Name="Price" /&&&&&&&&&&&&&&&&& &asp:Parameter Name="Image" /&&&&&&&&&&&&&&&&& &asp:Parameter Name="ItemId" /&&&&&&&&&&&&& &/UpdateParameters&&&&&&&&&&&&& &InsertParameters&&&&&&&&&&&&&&&&& &asp:Parameter Name="CategoryId" /&&&&&&&&&&&&&&&&& &asp:Parameter Name="Name" /&&&&&&&&&&&&&&&&& &asp:Parameter Name="Price" /&&&&&&&&&&&&&&&&& &asp:Parameter Name="Image" /&&&&&&&&&&&&& &/InsertParameters&&&InsertItemTemplate&&&&&&&&&&&&&&&& &table border="0" cellpadding="0" cellspacing="0" width="420"&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&& &td colspan="6" height="30" width="420" align="center"&&&&&&&&&&&&&&&&&&&&&&&& &h4&FormView InsertItemTemplate 模板&/h4&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&& &td rowspan="4" width="120"&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:Image ID="imgItem" runat="server" Width="120px" Height="120px" ImageUrl='&%# Eval("Image") %&'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& AlternateText="上传浏览图片" /&&/td&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&& &td colspan="2"&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:FileUpload ID="fupImage" runat="server" Width="100%"/&&/td&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:Button ID="btnUpload" Text="上传" OnClick="btnUpload_Click" runat="server"&&/asp:Button&&/td&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&& 分类:&/td&&&&&&&&&&&&&&&&&&&&&&&& &td width="120"&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:DropDownList ID="ddlCategory" runat="server" DataSourceID="sdsCategory" DataTextField="Name"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& DataValueField="CategoryId"&&&&&&&&&&&&&&&&&&&&&&&&&&&& &/asp:DropDownList&&/td&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&& 名称:&/td&&&&&&&&&&&&&&&&&&&&&&&& &td width="120"&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:TextBox ID="txtName" Text='&%# Bind("Name") %&' runat="server" /&&/td&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&& &td width="30"&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&& 价格:&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&& &td width="120"&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:TextBox ID="txtPrice" Text='&%# Bind("Price") %&' runat="server" /&&/td&&&&&&&&&&&&&&&&&&&&&&&& &td width="60"&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="30"&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="120"&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="30"&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="60"&&&&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="120" align="right"&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:Button ID="btnInsert" Text="插入" CommandName="Insert" runat="server" /&&/td&&&&&&&&&&&&&&&&&&&&&&&& &td height="30" width="60"&&&&&&&&&&&&&&&&&&&&&&&&&&&& &asp:Button ID="btnCancel" Text="取消" CommandName="Cancel" runat="server" /&&/td&&&&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&& &/table&&&&&&&&&&&& &/InsertItemTemplate&&&&&&&& &/asp:FormView&&&&&&&& &asp:SqlDataSource ID="sdsItem"& runat="server" ConnectionString="&%$ ConnectionStrings:NetShopConnString %&"&&&&&&&&&&& SelectCommand="SELECT Item.ItemId AS ItemId,Item.CategoryId AS CategoryId,Item.Name AS Name,Item.Price AS Price,Item.Image AS Image,Category.Name As CategoryName FROM Item INNER JOIN Category ON Item.CategoryId=Category.CategoryId"&&&&&&&&&&& UpdateCommand="UPDATE Item SET
WHERE "&&&&&&&&&&& InsertCommand="INSERT INTO Item(CategoryId,Name,Price,Image) VALUES (@CategoryId,@Name,@Price,@Image)"&&&&&&&&&&&& &UpdateParameters&&&&&&&&&&&&&&&& &asp:Parameter Name="CategoryId" /&&&&&&&&&&&&&&&& &asp:Parameter Name="Name" /&&&&&&&&&&&&&&&& &asp:Parameter Name="Price" /&&&&&&&&&&&&&&&& &asp:Parameter Name="Image" /&&&&&&&&&&&&&&&& &asp:Parameter Name="ItemId" /&&&&&&&&&&&& &/UpdateParameters&&&&&&&&&&&& &InsertParameters&&&&&&&&&&&&&&&& &asp:Parameter Name="CategoryId" /&&&&&&&&&&&&&&&& &asp:Parameter Name="Name" /&&&&&&&&&&&&&&&& &asp:Parameter Name="Price" /&&&&&&&&&&&&&&&& &asp:Parameter Name="Image" /&&&&&&&&&&&& &/InsertParameters&
2、这个模板和编辑模板基本一样,就是点击“新建”按钮进入时,没有绑定数据而已,因此,“上传”按钮的响应函数可复用,更新前的赋值操作也基本是一样的。
为fvwItem添加响应函数,代码如下:
[csharp] protected void fvwItem_ItemInserting(object sender, FormViewInsertEventArgs e)&{&&&& DropDownList ddl = (DropDownList)fvwItem.FindControl("ddlCategory");&&&& sdsItem.InsertParameters["CategoryId"].DefaultValue = ddl.SelectedV&&&&& Image img = (Image)fvwItem.FindControl("imgItem");&&&& sdsItem.InsertParameters["Image"].DefaultValue = img.ImageU&}&&protected void fvwItem_ItemInserting(object sender, FormViewInsertEventArgs e)&{&&&& DropDownList ddl = (DropDownList)fvwItem.FindControl("ddlCategory");&&&& sdsItem.InsertParameters["CategoryId"].DefaultValue = ddl.SelectedV
&&&& Image img = (Image)fvwItem.FindControl("imgItem");&&&& sdsItem.InsertParameters["Image"].DefaultValue = img.ImageU&}&
3、别忘了添加fvwItem的InsertCommand命令,并添加参数变量UpdateParameters:
[html] InsertCommand="INSERT INTO Item(CategoryId,Name,Price,Image) VALUES (@CategoryId,@Name,@Price,@Image)"&InsertCommand="INSERT INTO Item(CategoryId,Name,Price,Image) VALUES (@CategoryId,@Name,@Price,@Image)"
[html] &asp:SqlDataSource ID="sdsCategory" runat="server" ConnectionString="&%$ ConnectionStrings:NetShopConnString %&"&&&& SelectCommand="SELECT CategoryId,Name FROM Category"&&&/asp:SqlDataSource&&&asp:SqlDataSource ID="sdsCategory" runat="server" ConnectionString="&%$ ConnectionStrings:NetShopConnString %&"&&& SelectCommand="SELECT CategoryId,Name FROM Category"&&/asp:SqlDataSource&&
4、在浏览器中查看运行结果。
四、使用 FormView控件删除数据
这个操作不需要参数,所了也就最简单了,只要在sdsItem中添加一个DeleteCommand="DELETE FROM Item WHERE()"命令就可以了。
为了在删除中的图片地址的同时,也删除服务器端的图片文件,还是添加了一个消息响应函数,代码如下:
[csharp] protected void fvwItem_ItemDeleting(object sender, FormViewDeleteEventArgs e)&{&&&& Image img = (Image)fvwItem.FindControl("imgItem");&&&& File.Delete(Server.MapPath(img.ImageUrl));&}&&&& protected void fvwItem_ItemDeleting(object sender, FormViewDeleteEventArgs e)&&& {&&&&&&& Image img = (Image)fvwItem.FindControl("imgItem");&&&&&&& File.Delete(Server.MapPath(img.ImageUrl));&&& }
protected void fvwItem_ItemDeleting(object sender,FormViewDeleteEventArgs e)
&&& Image img = (Image)fvwItem.FindControl("imgItem");
&&& File.Delete(Server.MapPath(img.ImageUrl));
TA的最新馆藏[转]&[转]&[转]&[转]&[转]&[转]&[转]&
喜欢该文的人也喜欢

我要回帖

更多关于 sql update set 多个 的文章

 

随机推荐