最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2006年07月14日 00:00:00   作者:  

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

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

在WEB項(xiàng)目里看到Web.config配置文件,在configuration這行加入下面代碼用于和SQL服務(wù)器進(jìn)行連接

<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)//判斷頁面是否第一次運(yùn)行
     {

          string strsql="select * from Product";//定義一個(gè)數(shù)據(jù)庫的查詢字符串
                DataSet ds = new DataSet();
                myconn.Open();//打開數(shù)據(jù)庫連接
                SqlDataAdapter command = new SqlDataAdapter(strsql,myconn);//表示用于填充DataSet 和更新SQL Server 數(shù)據(jù)庫的一組數(shù)據(jù)命令和一個(gè)數(shù)據(jù)庫連接
                command.Fill(ds, "Product");
                productList.DataSource = ds.Tables[0].DefaultView;
                productList.DataBind();
                ds.Clear();
          myconn.Close();//關(guān)閉數(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('您真的要?jiǎng)h除這條記錄嗎?')){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('對(duì)不起,該產(chǎn)品編號(hào)已經(jīng)存在!')");
            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:表示要對(duì)SQL Server數(shù)據(jù)庫執(zhí)行的一個(gè)Transact-SQL語句或存儲(chǔ)過程
        cmd_add.ExecuteNonQuery();//對(duì)連接執(zhí)行Transact-SQL語句并返回受影響的行數(shù)。對(duì)于 UPDATE、INSERT 和 DELETE 語句,返回值為該命令所影響的行數(shù)。對(duì)于所有其他類型的語句,返回值為 -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對(duì)象讀取并返回一個(gè)記錄集
                shows.DataSource = rd_show;//指向數(shù)據(jù)源
                shows.DataBind();//綁定數(shù)據(jù)
                rd_show.Close();//關(guān)閉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");
        }
    }

 

例子下載

相關(guān)文章

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

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

    這篇文章主要介紹了.NET提取?Thread?中返回值詳情,關(guān)于如何獲取?Thread?中的返回值,不同的版本有不同的解決方案。需要的朋友可以參考一下
    2022-01-01
  • DataGridView控件詳細(xì)介紹

    DataGridView控件詳細(xì)介紹

    DataGridView是用于Windows Froms 2.0的新網(wǎng)格控件。它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我們的用戶需要的特性
    2012-11-11
  • 詳解如何使用Net將HTML簡(jiǎn)歷導(dǎo)出為PDF格式

    詳解如何使用Net將HTML簡(jiǎn)歷導(dǎo)出為PDF格式

    這篇文章主要為大家介紹了詳解如何使用Net將HTML簡(jiǎn)歷導(dǎo)出為PDF格式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • ASP.NET webUploader上傳大視頻文件相關(guān)web.config配置

    ASP.NET webUploader上傳大視頻文件相關(guān)web.config配置

    本文主要介紹了webUploader上傳大視頻文件相關(guān)web.config的配置。具有一定的參考價(jià)值,下面跟著小編一起來看下吧
    2017-01-01
  • .Net Core HttpClient處理響應(yīng)壓縮詳細(xì)

    .Net Core HttpClient處理響應(yīng)壓縮詳細(xì)

    .Net Core作為后起之秀直接將HttpClient扶正,并且在此基礎(chǔ)上改良了HttpClientFactory,接下來我們就來探究一下在.Net Core中使用HttpClient處理響應(yīng)壓縮的機(jī)制。,需要的朋友可以參考下面文章的具體內(nèi)容
    2021-09-09
  • upload上傳單張圖片

    upload上傳單張圖片

    這篇文章主要介紹了upload上傳單張圖片的代碼,需要的朋友可以參考下。
    2015-07-07
  • .NET 6新特性試用Timer類之PeriodicTimer?

    .NET 6新特性試用Timer類之PeriodicTimer?

    這篇文章主要介紹了.NET 6新特性試用Timer類之PeriodicTimer,PeriodicTimer與其他Timer需要?jiǎng)?chuàng)建事件回調(diào)不同,下,下面文章詳細(xì)介紹PeriodicTimer的使用方式,需要的朋友可以參考一下
    2022-02-02
  • ASP.NET Core 中間件的使用之全局異常處理機(jī)制

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

    我們今天這篇文章就來說說代碼異常問題怎么快速定位,減少不必要的時(shí)間浪費(fèi)。異常是一種運(yùn)行時(shí)錯(cuò)誤,當(dāng)異常沒有得到適當(dāng)?shù)奶幚恚芸赡軙?huì)導(dǎo)致你的程序意外終止。下面雄安邊將詳細(xì)介紹,需要的朋友可以參考下
    2021-09-09
  • 深入分析XmlSerializer對(duì)象的Xml序列化與反序列化的示例詳解

    深入分析XmlSerializer對(duì)象的Xml序列化與反序列化的示例詳解

    本篇文章是對(duì)XmlSerializer 對(duì)象的Xml序列化與反序列化的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • ASP.NET Core MVC 依賴注入View與Controller

    ASP.NET Core MVC 依賴注入View與Controller

    本文重點(diǎn)給大家介紹的是ASP.NET Core MVC 之依賴注入 View 和ASP.NET Core MVC 之依賴注入 Controller的相關(guān)資料,需要的小伙伴可以參考下面文章具體內(nèi)容
    2021-09-09

最新評(píng)論

绥棱县| 巴塘县| 安平县| 金阳县| 裕民县| 旬阳县| 中西区| 武威市| 乡宁县| 和顺县| 彰化县| 遂宁市| 远安县| 恭城| 原平市| 太仓市| 黄浦区| 竹溪县| 雅安市| 奉节县| 柞水县| 大同县| 宽甸| 乐业县| 冷水江市| 松阳县| 金坛市| 手游| 农安县| 左贡县| 金寨县| 峨眉山市| 苗栗县| 东乌珠穆沁旗| 陆河县| 蒙自县| 武强县| 满城县| 唐海县| 徐汇区| 甘孜|