对于网站编程的初学者来说,总是会上网找些源码来看,但久而久之还是停留在改代码的阶段,并不明白怎样去写一个完整的网站程序.有见如此我就开始写这样的文章(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#代码,2)编译为中间语言(IL),3)由.NET运行时(CLR)执行。C#在.NET中的优势在于其现代化语法、强大的类型系统和与.NET框架的紧密集成,适用于从桌面应用到Web服务的各种开发场景。

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无尽的。

热门文章

热工具

WebStorm Mac版
好用的JavaScript开发工具

Dreamweaver Mac版
视觉化网页开发工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

禅工作室 13.0.1
功能强大的PHP集成开发环境