For the website programming beginner, always will surf the net to look for some source code to look, but in the passage of time or stay in the phase of the change, do not understand how to write a complete Web site program. So I began to write such articles (C #), please criticize the deficiencies.
Database Connection Chapter
See the Web.config configuration file in the Web project, add the following code to the configuration line to connect to the SQL Server
<appSettings>
<!--database connection string-->
<add key= "ConnStr" value= "Data source=localhost;database=company; Uid=sa; password=; Persist security info=true; "/>
</appSettings>
The data list shows the article, as shown in 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;
referencing namespaces: SQL managed, configuration files
Using System.Data.SqlClient;
Using System.Configuration;
public partial class _default:system.web.ui.page
{
Protected SqlConnection myconn = new SqlConnection (configurationsettings.appsettings["ConnectionString"]);
Reads the database connection string in the Web.config configuration file and connects to the specified database
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)//To determine whether the page is first run
{
String Strsql= "select * from Product";//define a query string for a database
DataSet ds = new DataSet ();
MyConn. Open ();//Opening database connection
SqlDataAdapter command = new SqlDataAdapter (strsql,myconn);//Represents a set of data commands and a database connection for populating the dataset and updating the SQL Server database
Command. Fill (ds, "Product");
Productlist.datasource = ds. Tables[0]. DefaultView;
Productlist.databind ();
Ds. Clear ();
MyConn. Close ();//Shut Down 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 add article
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 (' Sorry, the product number already exists! ')");
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 +" ', ' "+ Categor Yid + "', '" + Name + "', '" + Description + "', '" + Price + "');
SqlCommand Cmd_add = new SqlCommand (Sql_add, myconn);//sqlcommand: Represents a Transact-SQL statement or stored procedure to be executed against a SQL Server database
Cmd_add. ExecuteNonQuery ();//performs Transact-SQL statements on the connection 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 article [/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 SqlDataReader object to read and return a recordset
Shows. DataSource = rd_show;//point to Data source
Shows. DataBind ()//binding Data
Rd_show. Close ();//Turn off SqlDataReader
}
}
}
Data modification Chapter
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") (' Save success! ') </script> ");
Response.Redirect ("show.aspx?id=" + ProductId);
}
Data Deletion Chapter
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");
}
}
Example Downloads