asp.net 2.0

1、

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>未命名頁面</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Insert" OnClick="Button1_Click" />&nbsp;<asp:Button ID="Button3"
            runat="server" Text="Insert more" OnClick="Button3_Click" />
        <br />
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
        &nbsp;<br />
        <asp:Button ID="btn_procedure" runat="server" Text="Procedure" OnClick="Button2_Click" /><br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting">
            <Columns>
                <asp:BoundField  DataField="usid" HeaderText="id"/>
                <asp:BoundField  DataField ="pwd" HeaderText="pwd"/>
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

2、

using System;
using System.Data;
using System.Configuration;
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;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GDataBind();
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

    }

    void GDataBind()
    {
        string sql = "select * from ls_test";
        DataSet ds = new DataSet();
        ds = DBAccess.ExecuteDataSet(sql);
        this.GridView1.DataSource = ds.Tables[0].DefaultView;
        this.GridView1.DataBind();

    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        string sql = "insert into ls_test(usid,pwd) values (" + this.TextBox1.Text.Trim() + ",'" + this.TextBox2.Text.Trim().ToUpper() + "')";
        if (DBAccess.ExecuteSql(sql) > 0)
        {
            this.GDataBind();
            Response.Write("Insert OK!");
        }
        else
        {
            Response.Write("Something is wrong!");
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        ArrayList list = new ArrayList();
        string sql = "";
        sql = "insert into ls_test(usid,pwd) values (" + this.TextBox1.Text.Trim() + ",'" + this.TextBox2.Text.Trim().ToUpper() + "')";
        list.Add(sql);
        sql = "insert into ls_test(usid,pwd) values (" + this.TextBox3.Text.Trim() + ",'" + this.TextBox4.Text.Trim().ToUpper() + "')";
        list.Add(sql);

        DBAccess.ExecuteSqlTran(list);
        this.GDataBind();
    }


    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string deid = GridView1.Rows[e.RowIndex].Cells[0].Text;
        string sql="delete ls_test where usid='" + deid + "'";
        DBAccess.ExecuteSql(sql);
        this.GDataBind();
   
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((LinkButton)(e.Row.Cells[2].Controls[0])).Attributes.Add("onclick", "return confirm('確認要刪除嗎?')");
        }
    }
}

3、

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>未命名頁面</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting">
            <Columns>
                <asp:BoundField  DataField="usid" HeaderText="id"/>
                <asp:BoundField  DataField="pwd" HeaderText="pwd"/>
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

4、

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GDataBind();
        }

    }
    void GDataBind()
    {
        string sql = "select * from ls_test";
        DataSet ds = new DataSet();
        ds = DBAccess.ExecuteDataSet(sql);
        this.GridView1.DataSource = ds.Tables[0].DefaultView;
        this.GridView1.DataBind();

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string deid = GridView1.Rows[e.RowIndex].Cells[0].Text;
        string sql = "delete ls_test where usid='" + deid + "'";
        DBAccess.ExecuteSql(sql);
        GDataBind();

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((LinkButton)(e.Row.Cells[2].Controls[0])).Attributes.Add("onclick","return confirm('刪除?')");
       
        }
    }
}

5、

/*
 author:BrianLee(lishuai)
 * date:20110923
 * title:just a test
 
 */


using System;
using System.Data;
using System.Configuration;
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;
using System.Data;
using System.Data.OracleClient;
using System.Collections;

/// <summary>
/// DBAccess 的摘要描述
/// </summary>
public abstract class DBAccess
{
    private readonly static string connectionstring = "data source=wip;uid=cpt;pwd=lcmmes;";

   
 public DBAccess()
 {
  //
  // TODO: 在此加入建構函式的程式碼
  //
    }

    #region public static method

    /// <summary>
    /// 執行SQL語句
    /// </summary>
    /// <param name="sql"></param>
    /// <returns></returns>
    public static int ExecuteSql(string sql)
    {
        using (OracleConnection cnn = new OracleConnection(connectionstring))
        {

            using (OracleCommand cmd = new OracleCommand(sql, cnn))
            {
               

                try
                {

                    cnn.Open();
                    return cmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
           
            }

       
        }


    }

    /// <summary>
    /// 運用事務處理多個SQL
    /// </summary>
    /// <param name="list"></param>
    public static void ExecuteSqlTran(ArrayList list)
    {
        using (OracleConnection cnn = new OracleConnection(connectionstring))
        {
            cnn.Open();
            OracleCommand cmd = new OracleCommand();
            cmd.Connection = cnn;

            OracleTransaction tr = cnn.BeginTransaction();
            cmd.Transaction = tr;

           

            try
            {
                for (int i = 0; i < list.Count; i++)
                {
                    string sql = list[i].ToString();
                    if (sql.Trim().Length > 0)
                    {
                        cmd.CommandText = sql;
                        cmd.ExecuteNonQuery();
                    }

                }

                tr.Commit();
            }
            catch (Exception ex)
            {
                tr.Rollback();
                throw new Exception(ex.Message) ;
            }

        }
   
    }


    public static DataSet ExecuteDataSet(string sql)
    {
        using (OracleConnection cnn = new OracleConnection(connectionstring))
        {
           

            try
            {
                OracleDataAdapter da = new OracleDataAdapter(sql, cnn);
                DataSet ds = new DataSet();
                da.Fill(ds);

                return ds;
            }
            catch (Exception ex)
            {
               
                throw new Exception(ex.Message);
            }
       
        }
   
    }

    #endregion public static method
}

原文地址:https://www.cnblogs.com/BrianLee/p/2192344.html