C#.net4中与OLE的ACCESS数据库建立连接

web.config文件中的配置为:

<connectionStrings>
    <add name="connStr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\1001\NewsWeb\test.MDB" providerName="System.Data.OleDb"/>
  </connectionStrings>

//Error's static string connStr = ConfigurationManager.AppSettings["connStr"];
//static string connStr=System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
//static string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;

static string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
Dbconnect mydbconn = new Dbconnect();
OleDbConnection odbCon = new OleDbConnection(connStr);

   void bindNewsClassGrid()
    {
        string sql = "select id,newstype from NewsClass order by id";
        GridView1.DataSource = mydbconn.dbConnectRtnDS(sql, odbCon);
        GridView1.DataBind();
    }

    protected void b_newsLBAdd_Click(object sender, EventArgs e)
    {
        addNewsClass();
    }
    void addNewsClass()
    {
        if (!String.IsNullOrEmpty(TextBox1.Text))
        {
            string sql = "insert into NewsClass(newstype) values(" + "'" + TextBox1.Text + "')";
            int i = mydbconn.dbConnectCmd(sql, odbCon, 1);
            Response.Write("添加了" + i + "条记录!");
            bindNewsClassGrid();
        }
        else
        {
            Response.Write("请输入新闻类型!");
        }
    }

   void bindDropNewsCL()
    {
        string sql = "select id,newstype from NewsClass order by id";
        DropDownList1.DataSource = mydbconn.dbConnectRtnDS(sql, odbCon);
        DropDownList1.DataTextField = "newstype";
        DropDownList1.DataValueField = "id";
        DropDownList1.DataBind();
    }
    void addNewsInfo()
    {
        if (!String.IsNullOrEmpty(txtTitle.Text) && !String.IsNullOrEmpty(txtContent.Text))
        {
            System.DateTime currentTime = new System.DateTime();
            currentTime = System.DateTime.Now;
            //
            string strT = currentTime.ToString("u").Substring(0, 19);
            //strT 的格式化: yyyy-mm-dd hh:mm:ss
            //string sdate = Calendar1.SelectedDate.ToString("u").Substring(0, 19);
            string sql = "insert into newsinfo(newsclassid,newstitle,newscontent,userid,oprdate) "
                      + "values(" + DropDownList1.SelectedValue
                      + "," + "'" + txtTitle.Text + "'"
                      + "," + "'" + txtContent.Text + "'"
                      + "," + txtOper.Text
                      //+ "," + "#" + Calendar1.SelectedDate + "#)";
                      + ",'" + sdate + "')";
            Response.Write(sql);
            int i = mydbconn.dbConnectCmd(sql, odbCon, 1);
            Response.Write("添加了" + i + "条记录!");
            Response.Write(Calendar1.SelectedDate.ToShortDateString());
            //}
        }
        else
        {
            Response.Write("请输入新闻类型!");
        }
    }

转载于:https://www.cnblogs.com/yxlq/p/5656591.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值