對於網站程式設計的初學者來說,總是會上網找些原始碼來看,但久而久之還是停留在改程式碼的階段,並不明白怎樣去寫一個完整的網站程式.有見如此我就開始寫這樣的文章(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"); } }
以上是ASP.NET數據入門的詳細內容。更多資訊請關注PHP中文網其他相關文章!

C#是一種現代、面向對象的編程語言,由微軟開發並作為.NET框架的一部分。 1.C#支持面向對象編程(OOP),包括封裝、繼承和多態。 2.C#中的異步編程通過async和await關鍵字實現,提高應用的響應性。 3.使用LINQ可以簡潔地處理數據集合。 4.常見錯誤包括空引用異常和索引超出範圍異常,調試技巧包括使用調試器和異常處理。 5.性能優化包括使用StringBuilder和避免不必要的裝箱和拆箱。

C#.NET應用的測試策略包括單元測試、集成測試和端到端測試。 1.單元測試確保代碼的最小單元獨立工作,使用MSTest、NUnit或xUnit框架。 2.集成測試驗證多個單元組合的功能,常用模擬數據和外部服務。 3.端到端測試模擬用戶完整操作流程,通常使用Selenium進行自動化測試。

C#高級開發者面試需要掌握異步編程、LINQ、.NET框架內部工作原理等核心知識。 1.異步編程通過async和await簡化操作,提升應用響應性。 2.LINQ以SQL風格操作數據,需注意性能。 3..NET框架的CLR管理內存,垃圾回收需謹慎使用。

C#.NET面試問題和答案包括基礎知識、核心概念和高級用法。 1)基礎知識:C#是微軟開發的面向對象語言,主要用於.NET框架。 2)核心概念:委託和事件允許動態綁定方法,LINQ提供強大查詢功能。 3)高級用法:異步編程提高響應性,表達式樹用於動態代碼構建。

C#.NET是構建微服務的熱門選擇,因為其生態系統強大且支持豐富。 1)使用ASP.NETCore創建RESTfulAPI,處理訂單創建和查詢。 2)利用gRPC實現微服務間的高效通信,定義和實現訂單服務。 3)通過Docker容器化微服務,簡化部署和管理。

C#和.NET的安全最佳實踐包括輸入驗證、輸出編碼、異常處理、以及身份驗證和授權。 1)使用正則表達式或內置方法驗證輸入,防止惡意數據進入系統。 2)輸出編碼防止XSS攻擊,使用HttpUtility.HtmlEncode方法。 3)異常處理避免信息洩露,記錄錯誤但不返回詳細信息給用戶。 4)使用ASP.NETIdentity和Claims-based授權保護應用免受未授權訪問。

C 語言中冒號 (':') 的含義:條件語句:分隔條件表達式和語句塊循環語句:分隔初始化、條件和增量表達式宏定義:分隔宏名和宏值單行註釋:表示從冒號到行尾的內容為註釋數組維數:指定數組的維數


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript開發工具

禪工作室 13.0.1
強大的PHP整合開發環境

Atom編輯器mac版下載
最受歡迎的的開源編輯器