Asp.net2005  连接一个文件夹下面的多个access数据库,这些数据库里表结构相同内容不同,我测试了一些连接方式没有能够实现。所以来请教100分长期在线。我给了我写的一些代码,这样只能连接一个数据库。有高手另写代码也给满分。能指出的我的错误在那里也给满分string filepath = Server.MapPath("App_Data");
        string[] file = System.IO.Directory.GetFiles(filepath);
        foreach (string thisFile in file)
        {
            if (System.IO.File.Exists(thisFile) && System.IO.Path.GetExtension(thisFile).ToLower() == ".mdb")
            {
                string olecon = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source= '" + thisFile + "'";
                OleDbConnection mycon = new OleDbConnection(olecon);
                mycon.Open();
                string sql = "select * from Person_XX where selfbh = '" + txtMName.Text.Trim() + "' and QueryCode='" + txtMPwd.Text.Trim() + "' ";
                OleDbCommand com = new OleDbCommand(sql, mycon);
                OleDbDataReader dr = com.ExecuteReader();
                dr.Read();
                if (txtMName.Text != "" && txtMPwd.Text != "")
                {
                    if (dr.HasRows)
                    {
                        Session["MName"] = txtMName.Text.Trim();
                       
                        dr.Close();
                        mycon.Close();
                        Response.Redirect("Default.aspx");
                    }
                    else
                    {
                        Response.Write("<script>alert('档案号和查询码错误,请检查');</script>");
                        txtMName.Focus();
                       
                        dr.Close();
                        mycon.Close();
                    }
                }
                else
                {
                    Response.Write("<script>alert('档案号和查询码不能为空');</script>");
                    txtMName.Focus();
                  
                    dr.Close();
                    mycon.Close();
                    
                }            }
        }    
       
    }

解决方案 »

  1.   


            //先判断用户名与密码是否为空
            if (string.IsNullOrEmpty(txtMName.Text) || string.IsNullOrEmpty(txtMPwd.Text))
            {
                Response.Write(" <script> alert( '档案号和查询码不能为空 '); </script> ");
                txtMName.Focus();
                return;
            }        bool isValidate = false;  //是否通过验证        string filepath = Server.MapPath("App_Data ");
            string[] file = System.IO.Directory.GetFiles(filepath);        foreach (string thisFile in file)
            {
                if (System.IO.File.Exists(thisFile) && System.IO.Path.GetExtension(thisFile).ToLower() == ".mdb ")
                {
                    string olecon = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source= ' " + thisFile + " ' ";
                    OleDbConnection mycon = new OleDbConnection(olecon);
                    mycon.Open();
                    string sql = "select count* from Person_XX where selfbh = ' " + txtMName.Text.Trim() + " ' and QueryCode= ' " + txtMPwd.Text.Trim() + " ' ";
                    OleDbCommand com = new OleDbCommand(sql, mycon);
                    OleDbDataReader dr = com.ExecuteReader();
                    //dr.Read();如果没有记录,直接读就返回false
                    if (dr.Read())
                    {
                        Session["MName"] = txtMName.Text.Trim();
                        IsValid = true;
                    }
                    else
                    {
                        Response.Write(" <script> alert( '档案号和查询码错误,请检查 '); </script> ");
                        txtMName.Focus();
                    }
                    //关闭资源
                    dr.Close();
                    olecon.Clone();
                    if (IsValid)
                    {
                        Response.Redirect("Default.aspx ");
                        return;
                    }            }
            }        if (!IsValid)
            {
                Response.Write(" <script> alert( '档案号和查询码错误,请检查 '); </script> ");
                txtMName.Focus();
            }
      

  2.   

    上面那段有些错误,楼主看看这段代码:
    //先判断用户名与密码是否为空
    if (string.IsNullOrEmpty(txtMName.Text) || string.IsNullOrEmpty(txtMPwd.Text))
    {
        Response.Write(" <script> alert( '档案号和查询码不能为空 '); </script> ");
        txtMName.Focus();
        return;
    }bool isValidate = false;  //是否通过验证string filepath = Server.MapPath("App_Data ");
    string[] file = System.IO.Directory.GetFiles(filepath);foreach (string thisFile in file)
    {
        if (System.IO.File.Exists(thisFile) && System.IO.Path.GetExtension(thisFile).ToLower() != ".mdb ")
        {
            continue;
        }    string olecon = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source= ' " + thisFile + " ' ";
        OleDbConnection mycon = new OleDbConnection(olecon);
        mycon.Open();
        string sql = "select * from Person_XX where selfbh = '" + txtMName.Text.Trim() + "' and QueryCode= '" + txtMPwd.Text.Trim() + "'";
        OleDbCommand com = new OleDbCommand(sql, mycon);
        OleDbDataReader dr = com.ExecuteReader();    if (dr.Read())
        {
            Session["MName"] = txtMName.Text.Trim();
            isValid = true;
            dr.Close();
            olecon.Close();        break;
        }    //关闭资源
        dr.Close();
        olecon.Clone();
    }//循环结束,判断是否通过验证
    if (isValid)
    {
        Response.Redirect("Default.aspx");
    }
    else
    {
        Response.Write("<script>alert('档案号和查询码错误,请检查 ');</script>");
        txtMName.Focus();
    }
      

  3.   

    你还在测试? 向1楼学习  这样会你的测试会顺利
          if (string.IsNullOrEmpty(txtMName.Text) || string.IsNullOrEmpty(txtMPwd.Text))
            {
                Response.Write(" <script> alert( '档案号和查询码不能为空 '); </script> ");
                txtMName.Focus();
                return;
            }        bool isValid = true ;  //是否通过验证        string filepath = Server.MapPath("App_Data ");
            string[] file = System.IO.Directory.GetFiles(filepath);        foreach (string thisFile in file)
            {
                if (System.IO.File.Exists(thisFile) && System.IO.Path.GetExtension(thisFile).ToLower() == ".mdb ")
                {
                    continue;
                }            string olecon = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source='"+ thisFile+"'";
                OleDbConnection mycon = new OleDbConnection(olecon);
                mycon.Open();
                string sql = "select * from Person_XX where selfbh = '" + txtMName.Text.Trim() + "' and QueryCode= '" + txtMPwd.Text.Trim() + "'";
                OleDbCommand com = new OleDbCommand(sql, mycon);
                OleDbDataReader dr = com.ExecuteReader();            if (dr.Read())
                {
                    Session["MName"] = txtMName.Text.Trim();
                    isValid = true;
                    dr.Close();
                    mycon.Close();
                    olecon.Clone ();                break;
                }            //关闭资源
                dr.Close();
                olecon.Clone();
            }        //循环结束,判断是否通过验证
            if (isValid)
            {
                Response.Redirect("Default2.aspx");
            }
            else
            {
                Response.Write("<script>alert('档案号和查询码错误,请检查 ');</script>");
                txtMName.Focus();
            }