• 一个插入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

  • 相关阅读:
    Spring整合hibernate4:事务管理
    eclipse添加约束文件
    转]@SuppressWarnings 详解
    spring + springMVC +hibernate 配置2
    spring +spring+ hibernate配置1
    eclipse下 hibernate逆向数据库操作示例!!
    Spring+hibernate 配置实例
    dubbo接口快速测试技巧
    Spring Boot Starter列表
    Spring Boot Starters是什么?
  • 原文地址:https://www.cnblogs.com/mane/p/1829942.html
Copyright © 2020-2023  润新知