asp.net链接Access数据库字符串正确方法

来源:互联网 发布:百度云管家软件 编辑:程序博客网 时间:2024/04/30 04:02

web  config中  作如下配置:

 

<connectionStrings>
    <add name="ydycon" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;

Data Source=|DataDirectory|ydy.mdb" providerName="System.Data.OleDb"/>

 

  </connectionStrings>

 

 

在.net文件中应用链接字符串:

 

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;  //添加引用
using System.Text;
using System.IO;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
     // string ConStr = "Server=(./SQLEXPRESS);DataBase=|Datadrictory|db_17.mdf;Uid=;Pwd=";
            string cmdtxt = "SELECT * FROM tb_13";
            OleDbConnection Con = new OleDbConnection(System.Configuration .ConfigurationManager.ConnectionStrings ["ydycon"].ToString() );
            //OleDbConnection Con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("ydy.mdb"));
           // SqlConnection Con = new SqlConnection(ConStr);
            Con.Open();
            OleDbDataAdapter da = new OleDbDataAdapter(cmdtxt, Con);
           // SqlDataAdapter da = new SqlDataAdapter(cmdtxt, Con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            this.DataList1.DataSource = ds;
            this.DataList1.DataKeyField = "ID";
            this.DataList1.DataBind();
            //将文件绑定到 ListBox中
            ArrayList arylst = new ArrayList();
            string filepath = Server.MapPath("HTMLPage");
            DirectoryInfo info = new DirectoryInfo(filepath);
            FileInfo[] fileinfo = info.GetFiles();
            foreach (FileInfo allfile in fileinfo)
            {
                arylst.Add(allfile);
            }
            this.ListBox1.DataSource = arylst;
            this.ListBox1.DataBind();
        }
    }

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
       // string ConStr = "Server=(local);DataBase=db_17;Uid=sa;Pwd=";
        string cmdtxt = "SELECT * FROM tb_13 WHERE ID="+this.DataList1.DataKeys[e.Item.ItemIndex].ToString()+"";
   //  OleDbConnection Con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("ydy.mdb"));
        OleDbConnection Con = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ydycon"].ToString());
        //SqlConnection Con = new SqlConnection(ConStr);
        Con.Open();
        OleDbCommand Com = new OleDbCommand(cmdtxt, Con);
       // SqlCommand Com = new SqlCommand(cmdtxt,Con);
        OleDbDataReader dr = Com.ExecuteReader();
       // SqlDataReader dr = Com.ExecuteReader();
        dr.Read();
        if(dr.HasRows)
        {
            WriteFile(dr["ArticleTitle"].ToString(), dr["ArticleContent"].ToString(), dr["ID"].ToString());
            Response.Write("<script>alert('静态页生成成功!');location='Default.aspx'</script>");
        }
        dr.Close();

    }

    public bool WriteFile(string ArticleTitle, string ArticleContent, string ArticleID)
    {
        string OutPutPath = HttpContext.Current.Server.MapPath("HTMLPage/");
        Encoding encoding = Encoding.GetEncoding("gb2312");
        // 读取模板文件
        string ModelTemp = HttpContext.Current.Server.MapPath("ModelHTML.htm");
        StreamReader sr = null;
        StreamWriter sw = null;
        string str = "";
        try
        {
            sr = new StreamReader(ModelTemp, encoding);
            str = sr.ReadToEnd(); // 读取文件
        }
        catch (Exception exp)
        {
            HttpContext.Current.Response.Write(exp.Message);
            HttpContext.Current.Response.End();
            sr.Close();
        }


        string HtmlFilename = DateTime.Now.ToString("yyyyMMddHHmmss_") +ArticleID+ ".html";
        // 替换内容
        str = str.Replace("PageTitle", ArticleTitle); //模板页中的PageArticle
        str = str.Replace("ArticleTitle", ArticleTitle);
        str = str.Replace("ArticleContent", ArticleContent);
        // 写文件
        try
        {
            sw = new StreamWriter(OutPutPath+HtmlFilename, false, encoding);
            sw.Write(str);
            sw.Flush();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message);
            HttpContext.Current.Response.End();
        }
        finally
        {
            sw.Close();
        }
        return true;
    }
    protected void btnView_Click(object sender, EventArgs e)
    {
        Response.Redirect("HTMLPage/" + this.ListBox1.SelectedValue);
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string filepath = Server.MapPath("HTMLPage/");
        File.Delete(filepath + this.ListBox1.SelectedValue);
        Response.Write("<script>alert('页面删除成功!');location='Default.aspx'</script>");
    }
}
   我的老天,浪费我一早上!!

原创粉丝点击