我用的是VS2010和ACCESS2003,
配置文件中连接字符:
<connectionStrings>
<add name ="conns" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\桌面\three\base.mdb"/>
</connectionStrings>
*.ASP文件里拖了两个textbox分别用来输入密码和用户名
再拖了一个button按扭来执行成功登陆
代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
string conn = ConfigurationManager.ConnectionStrings["conns"].ToString();
OleDbConnection con = new OleDbConnection(conn);
try
{
con.Open();
if (con.State != ConnectionState.Closed)
{
Response.Write("连接成功");
string na = TextBox1.Text.Trim();
string d = TextBox2.Text.Trim();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = "select * from tabless where id =" + TextBox1.Text.Trim() + " and name='" + TextBox2.Text.Trim() + "'";
OleDbDataReader cmdrdr = cmd.ExecuteReader();
//判断是否存在
if (cmdrdr.Read())
{
Response.Write("成功登陆");
con.Close();
con.Dispose();
}
else
{
Response.Write("用户名或者密码错误");
con.Close();
}
}
else
{
Response.Write("连接失败");
}
}
catch (Exception ex)
{
Response.Write(ex.Message );
}
PS:数据库中就两个字段,一个是id,一个是name,共一条记录,id为1,name为“张三”
运行后数据库连接成功,但是登陆不成功,显示:标准表达式中数据类型不匹配。