日期:2014-05-18  浏览次数:20988 次

[求助]c#中连接ACESS数据,老出现错误!
连接的是WEBCONFIG中CONN ACESS数据库 OLEDB方式
我目前理解连接ACESS就要用OLEDB方式,连接SQL用SQL的方式?这种理解对吗?

下面是错误源代码,麻烦看下
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
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.Common;

public partial class Admin_Login : System.Web.UI.Page
{

   
  //连接数据库
  OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);

  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
  {
  string UserName = tbxaccount.Text.ToString();
  string Password = tbxpwd.Text.ToString();
   
  if (Session["CheckCode"] == null)
  {
  Label1ERR.Text = "系统错误,不能生成验证码";
  Label1ERR.Visible = true;
  return;
  }
  if (tbxcheckcode.Text == "")
  {
  Label1ERR.Text = "请输入验证码";
  Label1ERR.Visible = true;
  return;
  }
  if (String.Compare(Session["CheckCode"].ToString(), tbxcheckcode.Text, true) != 0)
  {
  Label1ERR.Text = "验证码错误,请输入正确的验证码。";
  Label1ERR.Visible = true;
  return;
  }
  if (UserName == "" | Password == "")
  {
  Label1ERR.Visible = true;
  Label1ERR.Text = "请输入用户名、密码";
  }
  else
  {
   
  try
  {
   
  conn.Open();
   
  String sql_ = "select * from login_users Where login_name='" + UserName + "' And login_pwd='" + Password+"'";
   
  OleDbCommand cmd = new OleDbCommand(sql_, conn);
   

  //就下面2句抛出异常,为什么呀?我用错了?
  OleDbDataReader rs;
  rs = cmd.ExecuteReader();

   
  if (rs.Read())
  {
  Session["login_name"] = rs["login_name"].ToString(); //管理员名称
  Session["login_power"] = rs["login_power"].ToString(); //管理权限
   
  if (rs["login_lock"].ToString() == "1")
  {
  Response.Redirect("");
  }
  }
  else
  {
  Response.Write("没有数据集合!");
  Response.End();
  }
  rs.Close();
  conn.Close();
  }
  catch (Exception rs_e)
  {
  Response.Write("数据操作出现错误了");
  Response.End();
  }
   
  }
  }