zoukankan      html  css  js  c++  java
  • 一个插入access实例!

    1.Class1.cs


    --------------------------------------------------------------------------------

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

    namespace Mane
    {
        public class Class1
        {
            public DataTable selectTable(string sqltext, OleDbConnection con)
            {
                DataTable dt = new DataTable();
                OleDbDataAdapter adp = new OleDbDataAdapter(sqltext, con);
                adp.Fill(dt);
                return dt;
            }

            public void selectcmd(string sqltext, OleDbConnection con)
            {
                OleDbCommand cmd = con.CreateCommand();
                cmd.CommandText = sqltext;
               try
               {
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw new SystemException(e.Message);
                }
                finally
                {
                    con.Close();
                }
               
            }
        }
    }

    2.News_add.aspx.cs

    --------------------------------------------------------------------------------


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

    namespace Mane.Admin
    {
        public partial class WebForm2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {

            }

            protected void Button1_Click(object sender, EventArgs e)
            {
                if (!Request.IsAuthenticated)
                {
                    Response.Redirect(Server.MapPath("Login_Admin.aspx"));
                }
                else
                {
                    //添加
                    Class1 cl = new Class1();
                    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @Server.MapPath("/App_Data/db1.mdb"));
                    string Sqlstr = "INSERT INTO News(标题, 作者, 正文,日期) VALUES ('" + TextBox1.Text.Trim() + "','" + TextBox2.Text.Trim() +
                                     "','" + FreeTextBox1.Text + "','" + DateTime.Now.ToShortDateString() + "')";
                    cl.selectcmd(Sqlstr, con);
                    //刷新
                    System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('成功!');</script>");
                }
            }
        }
    }


    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/mane_yao/archive/2010/07/02/5708374.aspx

  • 相关阅读:
    KubeCon 2020 演讲集锦|《阿里巴巴云原生技术与实践 13 讲》开放下载
    突围数字化转型,让特步同比增长24.8%的全渠道中台
    阿里云飞天大数据产品价值解读——《一站式高质量搜索开放搜索》
    高德AR驾车导航解决方案
    我在阿里写代码学会的六件事
    送外卖也要“黑科技”?阿里移动感知技术应用揭秘
    阿里云机器学习怎么玩?这本新手入门指南揭秘了!
    用户自定义类型03 零基础入门学习Delphi33
    用户自定义类型03 零基础入门学习Delphi33
    用户自定义类型01 零基础入门学习Delphi31
  • 原文地址:https://www.cnblogs.com/mane/p/1829942.html
Copyright © 2011-2022 走看看