All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
C#中的类型相等与恒等(Equality,&,Identity)_[Asp.Net教程]
XML的站内全文检索解决方案_[XML教程]
配置整合Win+Apache+PHP+MySQL+Tcomcat(或Resin)完全手册_php资料_编程技术
在C#中运用SQLDMO备份和恢复Microsoft,SQL,Server数据库_[Asp.Net教程]
SQL Server2005数字转中文大写字母_[SQL Server教程]
ASP.NET,2.0站点地图搭建网站导航结构_[Asp.Net教程]
小例子:ASP.NET定制简单的错误处理页面_.net资料_编程技术
C#教程:线程同步
SQL,Server存储图像数据的策略与方法_[SQL,Server教程]
C#网络应用编程基础练习题与答案(七)_[Asp.Net教程]
Reading number is top 10 pictures
HongMenYan premiere XinLiangGong clairvoyant outfit PK YiFeiLiu1
传销的好处
The Soviet union swimsuit exposure in the 70 year2
Photographed the passion of the clients and prostitutes in the sex trade picture2
The wise woman of chest2
你白吃了多少药
Thrilling English baby
邪恶搞笑内涵图
The little girl with long hair1
Ashlynn Brooke a group sexy photo2
Download software ranking
Unix video tutorial7
Professional killers2 for Android
Tram sex maniac 2 (H) rar bag19
C#高级编程(第4版)
美女写真3
虚拟机5.5.3版
尖东毒玫瑰A
Unix video tutorial1
The cock of the Grosvenor LTD handsome
Unix video tutorial5
delv published in(发表于) 2014/1/6 9:03:00 Edit(编辑)
ASP.NET入门数据篇_[Asp.Net教程]

ASP.NET入门数据篇_[Asp.Net教程]

ASP.NET入门数据篇_[Asp.Net教程]























对于网站编程的初学者来说,总是会上网找些源码来看,但久而久之还是停留在改代码的阶段,并不明白怎样去写一个完整的网站程序.有见如此我就开始写这样的文章(c#版),不足之处请批评指正.




数据库连接篇




在WEB项目里看到Web.config配置文件,在configuration这行加入下面代码用于和SQL服务器进行连接












数据列表显示篇,如图:










using System;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;




//引用命名空间:SQL托管,配置文件
using System.Data.SqlClient;
using System.Configuration;





public partial class _Default : System.Web.UI.Page
{
protected SqlConnection myconn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
//读取web.config配置文件中的数据库连接字符串,并连接到指定的数据库




protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)//判断页面是否第一次运行
{




string strsql="select * from Product";//定义一个数据库的查询字符串
DataSet ds = new DataSet();
myconn.Open();//打开数据库连接
SqlDataAdapter command = new SqlDataAdapter(strsql,myconn);//表示用于填充DataSet 和更新SQL Server 数据库的一组数据命令和一个数据库连接
command.Fill(ds, "Product");
productList.DataSource = ds.Tables[0].DefaultView;
productList.DataBind();
ds.Clear();
myconn.Close();//关闭数据库连接
}
}




protected void grid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
foreach (System.Web.UI.WebControls.HyperLink link in e.Item.Cells[7].Controls)
{
link.Attributes.Add("onClick", "if (!window.confirm('您真的要删除这条记录吗?')){return false;}");
}
}




}




数据添加篇







protected void btnAdd_Click(object sender, EventArgs e)
{
string ProductId = this.txtProductId.Text;
string CategoryId = this.txtCategoryId.Text;
string Name = this.txtName.Text;
string Description = this.txtDescription.Text;
string Price =this.txtPrice.Text;




string sql_Exeits = "select * from Product where ProductId='" + ProductId + "'";
SqlCommand cmd_Exeits = new SqlCommand(sql_Exeits, myconn);
myconn.Open();
SqlDataReader rdr = cmd_Exeits.ExecuteReader();
while (rdr.Read())
{
Response.Write("");
this.txtCategoryId.Text = "";
this.txtDescription.Text = "";
this.txtName.Text = "";
this.txtPrice.Text = "";
this.txtProductId.Text = "";
return;
}
rdr.Close();





string sql_add = "insert into Product(ProductId,CategoryId,Name,Description,Price)values('" + ProductId + "','" + CategoryId + "','" + Name + "','" + Description + "','" + Price + "')";
SqlCommand cmd_add = new SqlCommand(sql_add, myconn);//SqlCommand:表示要对SQL Server数据库执行的一个Transact-SQL语句或存储过程
cmd_add.ExecuteNonQuery();//对连接执行Transact-SQL语句并返回受影响的行数。对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。对于所有其他类型的语句,返回值为 -1。如果发生回滚,返回值也为 -1。
myconn.Dispose();
myconn.Close();
}
[/CODE





[COLOR=Red]数据显示篇[/COLOR]
[CODE]
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string id = Request.Params["id"];
if (id == null || id.Trim() == "")
{
Response.Redirect("default.aspx");
Response.End();
}
else
{
string sql_show = "select * from Product Where ProductId=" + id;
SqlCommand cmd_show = new SqlCommand(sql_show, conn);
conn.Open();
SqlDataReader rd_show = cmd_show.ExecuteReader();//使用SqlDataReader对象读取并返回一个记录集
shows.DataSource = rd_show;//指向数据源
shows.DataBind();//绑定数据
rd_show.Close();//关闭SqlDataReader
}
}
}







数据修改篇







protected void btnAdd_Click(object sender, EventArgs e)
{
string ProductId = this.lblProductId.Text;
string CategoryId = this.txtCategoryId.Text;
string Name = this.txtName.Text;
string Description = this.txtDescription.Text;
decimal Price = decimal.Parse(this.txtPrice.Text);




string sql_edit = "update Product set CategoryId='" + CategoryId + "',Name='" + Name + "',Description='" + Description + "',Price='" + Price + "' where ProductId =" + ProductId;
SqlCommand cmd_edit = new SqlCommand(sql_edit, conn);




conn.Open();
cmd_edit.ExecuteNonQuery();
conn.Close();
Response.Write("");
Response.Redirect("show.aspx?id=" + ProductId);
}







数据删除篇










protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string ProductId = Request.Params["id"];
string sql_del = "delete from Product where ProductId=" + ProductId;
SqlCommand cmd_del = new SqlCommand(sql_del, conn);
conn.Open();
cmd_del.ExecuteNonQuery();
conn.Close();
Response.Redirect("default.aspx");
}
}




点击这里下载源文件






作者:szboz 来源:蓝色理想












































添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.