ASP.NET入門(mén)數(shù)據(jù)篇
對(duì)于網(wǎng)站編程的初學(xué)者來(lái)說(shuō),總是會(huì)上網(wǎng)找些源碼來(lái)看,但久而久之還是停留在改代碼的階段,并不明白怎樣去寫(xiě)一個(gè)完整的網(wǎng)站程序.有見(jiàn)如此我就開(kāi)始寫(xiě)這樣的文章(c#版),不足之處請(qǐng)批評(píng)指正.
數(shù)據(jù)庫(kù)連接篇
在WEB項(xiàng)目里看到Web.config配置文件,在configuration這行加入下面代碼用于和SQL服務(wù)器進(jìn)行連接
<appSettings>
<!-- 數(shù)據(jù)庫(kù)連接字符串 -->
<add key="ConnStr" value="Data Source=localhost;database=company;UID=sa;Password=;Persist Security Info=True;" />
</appSettings>
數(shù)據(jù)列表顯示篇,如圖:
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配置文件中的數(shù)據(jù)庫(kù)連接字符串,并連接到指定的數(shù)據(jù)庫(kù)
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)//判斷頁(yè)面是否第一次運(yùn)行
{
string strsql="select * from Product";//定義一個(gè)數(shù)據(jù)庫(kù)的查詢(xún)字符串
DataSet ds = new DataSet();
myconn.Open();//打開(kāi)數(shù)據(jù)庫(kù)連接
SqlDataAdapter command = new SqlDataAdapter(strsql,myconn);//表示用于填充DataSet 和更新SQL Server 數(shù)據(jù)庫(kù)的一組數(shù)據(jù)命令和一個(gè)數(shù)據(jù)庫(kù)連接
command.Fill(ds, "Product");
productList.DataSource = ds.Tables[0].DefaultView;
productList.DataBind();
ds.Clear();
myconn.Close();//關(guān)閉數(shù)據(jù)庫(kù)連接
}
}
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('您真的要?jiǎng)h除這條記錄嗎?')){return false;}");
}
}
}
數(shù)據(jù)添加篇
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("<script language='JavaScript'>");
Response.Write("alert('對(duì)不起,該產(chǎn)品編號(hào)已經(jīng)存在!')");
Response.Write("</script>");
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:表示要對(duì)SQL Server數(shù)據(jù)庫(kù)執(zhí)行的一個(gè)Transact-SQL語(yǔ)句或存儲(chǔ)過(guò)程
cmd_add.ExecuteNonQuery();//對(duì)連接執(zhí)行Transact-SQL語(yǔ)句并返回受影響的行數(shù)。對(duì)于 UPDATE、INSERT 和 DELETE 語(yǔ)句,返回值為該命令所影響的行數(shù)。對(duì)于所有其他類(lèi)型的語(yǔ)句,返回值為 -1。如果發(fā)生回滾,返回值也為 -1。
myconn.Dispose();
myconn.Close();
}
[/CODE
[COLOR=Red]數(shù)據(jù)顯示篇[/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對(duì)象讀取并返回一個(gè)記錄集
shows.DataSource = rd_show;//指向數(shù)據(jù)源
shows.DataBind();//綁定數(shù)據(jù)
rd_show.Close();//關(guān)閉SqlDataReader
}
}
}
數(shù)據(jù)修改篇
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("<script language=javascript>window.alert('保存成功!')</script>");
Response.Redirect("show.aspx?id=" + ProductId);
}
數(shù)據(jù)刪除篇
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");
}
}
相關(guān)文章
asp.net 獲取數(shù)據(jù)庫(kù)連接字符串
本文主要介紹了asp.net獲取數(shù)據(jù)庫(kù)連接字符串的具體實(shí)現(xiàn)代碼,具有一定參考價(jià)值,需要的朋友可以看下2016-12-12.NET?Core?中對(duì)象池?Object?Pool的使用
這篇文章主要介紹了?.NET?Core?中對(duì)象池?Object?Pool的使用,對(duì)象池簡(jiǎn)單來(lái)說(shuō)就是一種為對(duì)象提供可復(fù)用能力的軟件設(shè)計(jì)思路,對(duì)象池最常用的場(chǎng)景是游戲設(shè)計(jì),因?yàn)樵谟螒蛑写罅看嬖谥蓮?fù)用的對(duì)象,源源不斷的子彈出現(xiàn)并不是循環(huán)再生的,下面一起進(jìn)入文章了解具體內(nèi)容吧2021-11-11ASP.NET Core 中間件的使用之全局異常處理機(jī)制
我們今天這篇文章就來(lái)說(shuō)說(shuō)代碼異常問(wèn)題怎么快速定位,減少不必要的時(shí)間浪費(fèi)。異常是一種運(yùn)行時(shí)錯(cuò)誤,當(dāng)異常沒(méi)有得到適當(dāng)?shù)奶幚恚芸赡軙?huì)導(dǎo)致你的程序意外終止。下面雄安邊將詳細(xì)介紹,需要的朋友可以參考下2021-09-09設(shè)計(jì)windows phone頁(yè)面主題
這篇文章主要介紹了設(shè)計(jì)windows phone頁(yè)面主題,需要的朋友可以參考下2015-07-07利用.NET 開(kāi)發(fā)服務(wù)器 應(yīng)用管理工具
這篇文章主要介紹如何利用.NET 開(kāi)發(fā)一個(gè)應(yīng)用管理工具的服務(wù)器,文章回先聊背景接著其是喲美好方法,需要的的小伙伴可以參考一下小面文章的具體內(nèi)容2021-10-10