亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

ASP.NET入門數(shù)據(jù)篇

 更新時間:2006年07月14日 00:00:00   作者:  

對于網站編程的初學者來說,總是會上網找些源碼來看,但久而久之還是停留在改代碼的階段,并不明白怎樣去寫一個完整的網站程序.有見如此我就開始寫這樣的文章(c#版),不足之處請批評指正.

數(shù)據(jù)庫連接篇

在WEB項目里看到Web.config配置文件,在configuration這行加入下面代碼用于和SQL服務器進行連接

<appSettings>
<!-- 數(shù)據(jù)庫連接字符串 -->
<add key="ConnStr" value="Data Source=localhost;database=company;UID=sa;Password=;Persist Security Info=True;" />
</appSettings>

數(shù)據(jù)列表顯示篇,如圖:

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配置文件中的數(shù)據(jù)庫連接字符串,并連接到指定的數(shù)據(jù)庫

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)//判斷頁面是否第一次運行
     {

          string strsql="select * from Product";//定義一個數(shù)據(jù)庫的查詢字符串
                DataSet ds = new DataSet();
                myconn.Open();//打開數(shù)據(jù)庫連接
                SqlDataAdapter command = new SqlDataAdapter(strsql,myconn);//表示用于填充DataSet 和更新SQL Server 數(shù)據(jù)庫的一組數(shù)據(jù)命令和一個數(shù)據(jù)庫連接
                command.Fill(ds, "Product");
                productList.DataSource = ds.Tables[0].DefaultView;
                productList.DataBind();
                ds.Clear();
          myconn.Close();//關閉數(shù)據(jù)庫連接
            }
 }

    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;}");
        }
    }

}

數(shù)據(jù)添加篇

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數(shù)據(jù)庫執(zhí)行的一個Transact-SQL語句或存儲過程
        cmd_add.ExecuteNonQuery();//對連接執(zhí)行Transact-SQL語句并返回受影響的行數(shù)。對于 UPDATE、INSERT 和 DELETE 語句,返回值為該命令所影響的行數(shù)。對于所有其他類型的語句,返回值為 -1。如果發(fā)生回滾,返回值也為 -1。
        myconn.Dispose();
        myconn.Close();
    }
[/CODE


[COLOR=Red]數(shù)據(jù)顯示篇[/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;//指向數(shù)據(jù)源
                shows.DataBind();//綁定數(shù)據(jù)
                rd_show.Close();//關閉SqlDataReader
             }
         }
    }

數(shù)據(jù)修改篇

    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);
    }

數(shù)據(jù)刪除篇

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 獲取數(shù)據(jù)庫連接字符串

    asp.net 獲取數(shù)據(jù)庫連接字符串

    本文主要介紹了asp.net獲取數(shù)據(jù)庫連接字符串的具體實現(xiàn)代碼,具有一定參考價值,需要的朋友可以看下
    2016-12-12
  • DataGridView控件詳細介紹

    DataGridView控件詳細介紹

    DataGridView是用于Windows Froms 2.0的新網格控件。它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我們的用戶需要的特性
    2012-11-11
  • ADO.NET實用技巧兩則

    ADO.NET實用技巧兩則

    ADO.NET實用技巧兩則...
    2006-07-07
  • Oracle中TO_DATE格式介紹

    Oracle中TO_DATE格式介紹

    Oracle中TO_DATE格式介紹;可供需求的朋友參考
    2012-11-11
  • .NET提取?Thread?中返回值詳情

    .NET提取?Thread?中返回值詳情

    這篇文章主要介紹了.NET提取?Thread?中返回值詳情,關于如何獲取?Thread?中的返回值,不同的版本有不同的解決方案。需要的朋友可以參考一下
    2022-01-01
  • .NET?Core?中對象池?Object?Pool的使用

    .NET?Core?中對象池?Object?Pool的使用

    這篇文章主要介紹了?.NET?Core?中對象池?Object?Pool的使用,對象池簡單來說就是一種為對象提供可復用能力的軟件設計思路,對象池最常用的場景是游戲設計,因為在游戲中大量存在著可復用的對象,源源不斷的子彈出現(xiàn)并不是循環(huán)再生的,下面一起進入文章了解具體內容吧
    2021-11-11
  • ASP.NET Core 中間件的使用之全局異常處理機制

    ASP.NET Core 中間件的使用之全局異常處理機制

    我們今天這篇文章就來說說代碼異常問題怎么快速定位,減少不必要的時間浪費。異常是一種運行時錯誤,當異常沒有得到適當?shù)奶幚恚芸赡軙е履愕某绦蛞馔饨K止。下面雄安邊將詳細介紹,需要的朋友可以參考下
    2021-09-09
  • 設計windows phone頁面主題

    設計windows phone頁面主題

    這篇文章主要介紹了設計windows phone頁面主題,需要的朋友可以參考下
    2015-07-07
  • 利用.NET 開發(fā)服務器 應用管理工具

    利用.NET 開發(fā)服務器 應用管理工具

    這篇文章主要介紹如何利用.NET 開發(fā)一個應用管理工具的服務器,文章回先聊背景接著其是喲美好方法,需要的的小伙伴可以參考一下小面文章的具體內容
    2021-10-10
  • Net內存管理五大基礎

    Net內存管理五大基礎

    這篇文章主要給大家分享Net內存管理五大基礎內容,文章講圍繞Net內存管理詳細介紹文章內容,感興趣的朋友可以參考一下,希望對你有所幫助
    2021-10-10

最新評論