|
對于網站編程的初學者來說,總是會上網找些源碼來看,但久而久之還是停留在改代碼的階段,并不明白怎樣去寫一個完整的網站程序.有見如此我就開始寫這樣的文章(c#版),不足之處請批評指正.
數據庫連接篇
在WEB項目里看到Web.config配置文件,在configuration這行加入下面代碼用于和SQL服務器進行連接
<appSettings>
<!-- 數據庫連接字符串 -->
<add key="ConnStr" value="Data Source=localhost;database=company;UID=sa;Password=;Persist Security Info=True;" />
</appSettings>
數據列表顯示篇,如圖:
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("<script language='JavaScript'>");
Response.Write("alert('對不起,該產品編號已經存在!')");
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:表示要對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("<script language=Javascript>window.alert('保存成功!')</script>");
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");
}
}
例子下載
AspNet技術:ASP.NET入門數據篇,轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。