博客新地址

http://wyz.67ge.com/

这是.net的一小步,但却是我个人的一大步

虽然只有增删改功能,但却是我的第一个.Net2制作。你好.net2.0,你好博客园。

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"  validateRequest="false"%>
 
<!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:HiddenField ID="TextBox3" runat="server" />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><asp:Button ID="Button2"
            runat="server" Text="Update" OnClick="Button2_Click" />&nbsp;<br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="ADD" OnClick="Button1_Click" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="uid" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting">
        <Columns>
        <asp:BoundField HeaderText="UID" DataField="uid" />
        <asp:BoundField HeaderText="UNAME" DataField="uname" />
        <asp:CommandField  HeaderText="编辑"  ShowEditButton="True" ShowDeleteButton="true"  />
        </Columns>
        </asp:GridView>
 
    </div>
    </form>
</body>
</html>

Default.aspx.cs

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.OleDb;
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.ld();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string strSQL = "SELECT   *   FROM   i_user where uid is null";
        OleDbDataAdapter myODA = new OleDbDataAdapter(strSQL,DataAccess.GetStrConn());
        OleDbCommandBuilder myCommandBuilder = new OleDbCommandBuilder(myODA);
        DataSet myDS = new DataSet();
        myODA.FillSchema(myDS, SchemaType.Source, "i_user");
        myODA.Fill(myDS, "i_user");
        DataRow myRow = myDS.Tables["i_user"].NewRow();
        myRow["uname"] = TextBox1.Text;
        myDS.Tables["i_user"].Rows.Add(myRow);
        myODA.Update(myDS, "i_user");
        myDS.AcceptChanges();
        this.ld();
    }
    protected void ld()
    {
        GridView1.PageIndex = 1;
        GridView1.DataSource = DataAccess.dataSet("SELECT uid,uname FROM i_user order by uid desc");
        this.GridView1.DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int id = int.Parse(TextBox3.Value);
        string value = TextBox2.Text;
        this.md(id,"uname",value);
    } 
    protected void md(int id, string fild, string value)
    {
        string strSQL = "SELECT   *   FROM   i_user where uid="+id;
        OleDbDataAdapter myODA = new OleDbDataAdapter(strSQL, DataAccess.GetStrConn());
        OleDbCommandBuilder myCommandBuilder = new OleDbCommandBuilder(myODA);
        DataSet myDS = new DataSet();
        myODA.FillSchema(myDS, SchemaType.Source, "i_user");
        myODA.Fill(myDS, "i_user");
        DataTable tbl = myDS.Tables[0];
        tbl.PrimaryKey = new DataColumn[] { tbl.Columns["uid"] };
        DataRow myRow = tbl.Rows.Find(id);
        myRow[fild] = htmlencode(value);
      
        myODA.Update(myDS, "i_user");
        myDS.AcceptChanges();
        this.ld();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string i=GridView1.DataKeys[e.NewEditIndex].Value.ToString();
        TextBox3.Value = i.ToString();
        TextBox2.Text = DataAccess.dataSet("select uname from i_user where uid=" + i.ToString()).Tables[0].Rows[0][0].ToString();
 
    }
    protected static string htmlencode(string strContent)
    {
        strContent = strContent.Replace("&", "&amp;");
        strContent = strContent.Replace("<", "&lt;");
        strContent = strContent.Replace(">", "&gt;");
        strContent = strContent.Replace(@"/", "&frasl;");
        strContent = strContent.Replace("\n", "<br/>");
        strContent = strContent.Replace("   ", "&nbsp;");
        return strContent;
    }
 
    protected void GridView1_RowDeleting(object sender,GridViewDeleteEventArgs e)
    {
        string i = GridView1.DataKeys[e.RowIndex].Value.ToString();
        DataAccess.excuteSql("delete from i_user where uid=" + i);
        this.ld();
    }
}

conn.cs

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.OleDb;
using System.Data.SqlClient;
 
/// <summary>
/// Class1 的摘要说明
/// </summary>
public class DataAccess
{
    public DataAccess()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    protected static OleDbConnection conn = new OleDbConnection();
    protected static OleDbCommand comm = new OleDbCommand();
    public static void openConnection()
    {
        if (conn.State == ConnectionState.Closed)
        {
            conn.ConnectionString =  @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/#db.mdb");
            comm.Connection = conn;
            try
            {
                conn.Open();
            }
            catch (Exception e)
            { throw new Exception(e.Message); }
 
        }
 
    }//打开数据库
    public static string GetStrConn()
    {
        return @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/#db.mdb");
    }
    private static void closeConnection()
    {
        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
    }//关闭数据库
    public static DataSet dataSet(string sqlstr)
    {
        DataSet ds = new DataSet();
        OleDbDataAdapter da = new OleDbDataAdapter();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);
 
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
        return ds;
    }//返回指定sql语句的dataset
    public static void dataSet(string sqlstr, ref DataSet ds)
    {
        OleDbDataAdapter da = new OleDbDataAdapter();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
    }//返回指定sql语句的dataset
    public static void excuteSql(string sqlstr)
    {
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            comm.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        { closeConnection(); }
    }//执行sql语句
 
}
posted @ 2007-11-02 16:39  yongzhi  阅读(166)  评论(0编辑  收藏  举报

博客新地址

http://wyz.67ge.com/