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>");
}
}
我的老天,浪费我一早上!!
- asp.net链接Access数据库字符串正确方法
- asp.net 数据库字符串链接
- Asp.net连接Access数据库的方法
- ASP.NET 链接 Access 数据库路径问题最终解决方案
- asp.net(VB)操作Access数据库(链接/添加/修改/删除)
- asp.net(VB)操作Access数据库(链接/添加/修改/删除)
- ASP链接数据库的几种方法(ACCESS,MSSQL,MySql)
- ASP.NET 2.0中访问Access数据库的连接字符串
- vb.net 链接Access数据库
- Asp.net数据库链接
- asp.net Access数据库连接字符串
- asp.net连接access数据库两种方法
- ASP.NET编程连接数据库Access的方法
- ASP.NET + C#读取ACCESS数据库内容的简单方法
- Asp.net相对路径连接Access数据库的方法
- 最简单的asp.net 操作 access 数据库方法
- ASP.NET 2.0中的数据库链接字符串的使用方法
- ASP.NET程序链接数据库字符串的处理
- 瑞星升级提示网络连接失败网络配置后重试(12007) 解决办法
- 以太网MII接口类型大全 MII、RMII、SMII、SSMII、SSSMII、GMII、RGMII、SGMII、TBI、RTBI、XGMII、XAUI、XL
- reactos操作系统实现(80)
- wine用户手册概要
- ASP.net2.0中数据库连接字符串
- asp.net链接Access数据库字符串正确方法
- linux线程同步之互斥锁——linux的关键区域
- 推荐给广大游戏爱好者和程序员一本书
- System Repair Engineer 2.81 (2.8.1.1279) by Smallfrogs
- 不用临时变量实现两个变量的交换
- 我的内部类笔记
- 切·格瓦拉(Che Guevara)
- 北航计算机2009年的复试上机题目
- JAVA开源之应用系统篇_GIS系统