access2007 和 access 2003的连接字符串不同,一个ADO版本12.0,一个是4.0;
public List<string> Access2007(string accdbpath,string tablename)
{
List<string> lstdata = new List<string>();
string strconn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + accdbpath + "'";
OleDbConnection conn = new OleDbConnection(strconn);
conn.Open();
string cmdtext = "select * from " + tablename+"";
OleDbDataAdapter apdate = new OleDbDataAdapter(cmdtext, conn);
DataTable table = new DataTable();
apdate.Fill(table);
if (table.Rows.Count > 0)
{
for (int irow = 0; irow < table.Rows.Count; irow++)
for (int icol = 0; icol < table.Columns.Count; icol++)
lstdata.Add("Row:" + irow
+ " Column:" + icol
+ " ColumnName:" + table.Columns[icol].ColumnName
+ " Value:" + table.Rows[irow][table.Columns[icol].ColumnName]);
}
return lstdata;
}
namespace 留言板_access数据库_
{
public partial class _Default : System.Web.UI.Page
{
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=F:\\留言板.mdb ";
string sex;
protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection coon = new OleDbConnection(strConnection);
coon.Open();
Response.Write("Access数据库连接状态显示:" + coon.State);
coon.Close();
Label1.Text=DateTime.Now.ToString();
if (RadioButton1.Checked) { sex = "男"; }
else { sex = "女"; }
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox2.Text = "";
}
public void Seek(string str)
{
using (OleDbConnection coon = new OleDbConnection(strConnection))
{
OleDbCommand cmd = new OleDbCommand(str, coon);
try
{
coon.Open();
OleDbDataReader r = cmd.ExecuteReader();
while (r.Read())
TextBox3.Text = string.Format("留言人:{0};性别:{1};留言内容:{2};留言时间{3}", r[0], r[1], r[2],r[4]);
}
catch (Exception ee)
{
Response.Write(ee.Message);
}
}
}
public void Insert(string str)
{
using (OleDbConnection coon = new OleDbConnection(strConnection))
{
OleDbCommand cmd = new OleDbCommand(str, coon);
try
{
coon.Open();
cmd.ExecuteNonQuery();
Response.Write("<script>window.alert('数据添加成功!');</script>");
}
catch (Exception ee)
{
Response.Write(ee.Message);
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Seek("select * from 留言板 where 姓名= '" + TextBox1.Text.Trim() + "'");
}
protected void Button3_Click(object sender, EventArgs e)
{
Insert("insert into 留言板(姓名,性别,留言内容,留言时间) values('"+TextBox1.Text.Trim()+"','"+sex.Trim()+"','"+TextBox2.Text.Trim()+"','"+Label1.Text+"')");
Button2_Click(sender,e);
}
}
}
Copyright © 2004-2024 Ynicp.com 版权所有 法律顾问:建纬(昆明)律师事务所 昆明市网翼通科技有限公司 滇ICP备08002592号-4