asp.net(C#)连接access数据库

来源:互联网 发布:python 元组 编辑:程序博客网 时间:2024/04/30 12:53

asp.net(C#)连接access数据库

前期工作:创建一个access数据库,名为yonghu.mdb ,表明yonghu,字段为user_id,user_pwd
PS:如何新建access数据库:左上角菜单按钮->新建-》点右下角起名字文件夹(保存类型选2002-2003数据库)-》(yonghu)确定-》打开access-》左上角试图按钮-》设计试图-》起表名字(yonghu)-》字段填user_id类型文本,字段user_pwd类型文本-》切换到数据表试图,天上数据—》关闭access OK!数据库建完!
vs2008中新建网站-》asp.net网站,用控件制作个登录界面,
-》控件名txtyh,txtpwd,btnok,btnqx
登录按钮事件->using System.Data.OleDb;填包
public partial class datalogin : System.Web.UI.Page
{
    OleDbConnection con = new OleDbConnection();
    OleDbCommand cmd = new OleDbCommand();
    OleDbDataAdapter oda = new OleDbDataAdapter();
    DataSet ds = new DataSet();
    DataTable dta = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
//定义
}
单击按钮事件代码
//Server.MapPath是你的数据库的地址,我把他放到该项目的app_data目录下
con.ConnetctionString="provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath("app_data/yonghu.mdb");
con.Open();
cmd.Connection=con;
cmd.CommandText="select * from yonghu where user_id='"+txtyh.Text+"'and user_pwd='"+txtpwd.Text+"'";
oda=new OleDbDataAdapter(cmd);
oda.Fill(ds,"yonghu");
if(ds.Tables[0].Rows.Count==0)
{
 Response.Write("<script>alert('用户姓名或者密码错误!!')</script>");
}
else
{
Response.Redirect("OK.aspx");
}


新建ok.aspx;如果成功就跳转过去了
别忘了配置odbc数据源哦,亲
原创粉丝点击