Asp.net访问Access数据库3-后台文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
    oledbAccessHelper myacc;
    protected void Page_Load(object sender, EventArgs e)
    {
            myacc = new oledbAccessHelper(Server.MapPath("App_Data/db1.mdb"));
        if (!IsPostBack)
        {
            gridViewBind();
        }
    }
   
    private void gridViewBind()
    {
        string mystr = "select * from tab1";
        DataTable mytab = new DataTable();
        if (myacc.oledbSqlToDataTable(mystr, ref mytab))
        {
            Response.Write("
        }
        GridView1.DataSource = mytab;
        GridView1.DataBind();
    }

    private void dataListBind()
    {
        if (TextBox1.Text.Trim() == "") return;
        string mystr = "select * from tab1 where ID=@a";
        OleDbParameter[] mps ={
                                    new OleDbParameter("@a",TextBox1.Text.Trim())
                                };
        DataTable mytab = new DataTable();
        if (myacc.oledbSqlToDataTable(mystr,mps, ref mytab))
        {
            Response.Write("
        }
        DataList1.DataSource = mytab;
        DataList1.DataBind();
        return;
    }
    private void NameAdd()
    {
        if (TextBox2.Text.Trim() == "") return;
        string mystr = "insert into tab1(Name) select @a";
        OleDbParameter[] mps ={
                                    new OleDbParameter("@a",TextBox2.Text.Trim())
                                };
        string mystr1 = "";
        if (myacc.oledbSqlZhixing(mystr, mps, ref mystr1))
        {
            Response.Write("
        }
        else
        {
            Response.Write("
        }
        //gridViewBind();
        return;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Button mbt = (Button)sender;
        if (mbt == null) return;
        if (mbt.ID == "Button1")
        {
            //查询
            dataListBind();
        }
        if (mbt.ID == "Button2")
        {
            //添加
            NameAdd();
            return;
        }
    }
}

Asp.net访问Access数据库3-后台文件,布布扣,bubuko.com

Asp.net访问Access数据库3-后台文件

上一篇:mongodb 简单的CRUD


下一篇:SQLServer2005打开表时出现“Object reference not set to an instance of an object. (SQLEditors)”错误的解决办法