Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does ASP.NET connect to the database

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces ASP.NET how to connect to the database, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Database connection section

See the Web.config configuration file in the WEB project, and add the following code to the configuration line to connect to the SQL server

The data list is displayed, as shown in the figure:

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;// reference namespace: SQL hosting, configuration file using System.Data.SqlClient;using System.Configuration;public partial class _ Default: System.Web.UI.Page {protected SqlConnection myconn = new SqlConnection (ConfigurationSettings.AppSettings ["ConnectionString"]) / / read the database connection string in the web.config configuration file and connect to the specified database protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) / / determine whether the page runs {string strsql= "select * from Product" for the first time; / / defines a database query string DataSet ds = new DataSet () Myconn.Open (); / / Open database connection SqlDataAdapter command = new SqlDataAdapter (strsql,myconn); / / represents a set of data commands and a database connection command.Fill (ds, "Product") for populating the DataSet and updating the SQL Server database; productList.DataSource = ds.Tables [0] .DefaultView; productList.DataBind () Ds.Clear (); myconn.Close () / / close database connection}} 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 ('do you really want to delete this record?')) {return false;} ");}

Data addition

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 ("); Response.Write (" alert ('sorry, the product number already exists!')) ; 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: represents a Transact-SQL statement or stored procedure cmd_add.ExecuteNonQuery () to be executed against the SQL Server database / / A pair of connections executes the Transact-SQL statement and returns the number of rows affected. For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is-1. If a rollback occurs, the return value is also-1. Myconn.Dispose (); myconn.Close ();} [/ code [color = Red] data display [/ 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 () / / use the SqlDataReader object to read and return a recordset shows.DataSource = rd_show;// pointing to the data source shows.DataBind (); / bind data rd_show.Close (); / / close SqlDataReader}

Data modification

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 ("window.alert (' saved!')") ; Response.Redirect ("show.aspx?id=" + ProductId);}

Data deletion

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");}} Thank you for reading this article carefully. I hope the article "how to connect ASP.NET to the database" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report