Saturday 19 October 2013

Connection with Access, Excel and MDF (SQL SERVER)

Instructions::
1. Access File Name --  AccessDB_2007.accdb (2007)  & AccesDB_2003.mdb (97-2003 format) and Table name is - 'MemberMaster'.
2. Excel File Name --  ExcelDB_2007.xlsx (2007)  & ExcelDB_2003.xls (97-2003 format) and Sheet name is - 'MemberMaster'.
3. MDF File --  Database name SQLDB. Table Name -- MemberMaster.

      <<<<<<      If getting error while copying MDF then use these query in SQL SERVER. You have to copy mdf to App_Data folder.      >>>>>>>
    
            --    1.    --
                    USE MASTER; // Yeah its Master Database.

                    -- Take database in single user mode -- if you are facing errors.  
                    -- This may terminate your active transactions for database

                    ALTER DATABASE SQLDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

                    -- Detach DB
                    EXEC MASTER.dbo.sp_detach_db @dbname = N'SQLDB'

            --    2.    --
                    -- Attach again
                    CREATE DATABASE [SQLDB] ON
                    ( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\SQLDB.mdf' ),
                    ( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\SQLDB_log.ldf' )
                    FOR ATTACH

                    use SQLDB
                    Select * FROM MemberMaster


Tables:: Database and Table names are written in Connections strings.






ASPX Page

    <asp:TextBox ID="uid" runat="server" />
    <asp:TextBox ID="pwd" runat="server" />
    <asp:Button ID="submit" runat="server" Text="Submit" onclick="submit_Click" />







 C#

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.OleDb; // For Access & Excel.
using System.Data.SqlClient; // For SQL SERVER.


public partial class Programfiles_Access_login : System.Web.UI.Page
{
    //string conn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\DataBase\AccesDB_2003.mdb;";  //Access 2003.
    string conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\DataBase\AccessDB_2007.accdb"; //Access 2007.

    //string conExl = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\DataBase\ExcelDB_2003.xls;Extended Properties=Excel 8.0;";    //MS Excel 2003.
    //string conExl = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\DataBase\ExcelDB_2007.xlsx;Extended Properties=Excel 12.0;";    //MS Excel 2007.

    //string conSQL = @"Data Source=.;AttachDbFilename=SQLDB.mdf;User Instance=true; uid=sa;pwd=saa;"; // MDF Connection is best possible with SQL SERVER 2005.

  

    protected void Page_Load(object sender, EventArgs e)
    {
      
    }

    protected void submit_Click(object sender, EventArgs e)
    {
        #region AccessDB Connection
        if ((!string.IsNullOrEmpty(uid.Text)) && (!string.IsNullOrEmpty(pwd.Text)))
        {
            string qry = "Select * From MemberMaster where UserID='" + uid.Text + "' and Password='" + pwd.Text + "'";
            using (OleDbConnection con = new OleDbConnection(conn))
            {
                using (OleDbCommand cmd = new OleDbCommand(qry, con))
                {
                    con.Open();
                    OleDbDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        if (dr.HasRows == true)
                        {
                            Session["Name"] = dr["FullName"].ToString();
                            Response.Redirect("Welcomepage.aspx");
                        }
                    }
                    if (dr.HasRows == false)
                    {
                        Response.Write("Either ID Or Password Is Incorrect.");
                    }
                }
            }
        }
        #endregion

        #region Excel Connection
        //if ((!string.IsNullOrEmpty(uid.Text)) && (!string.IsNullOrEmpty(pwd.Text)))
        //{
        //    string ExlSheet = "MemberMaster";// Name of Excel Sheet.;
        //    string qry = "Select * From " + "[" + ExlSheet + "$]" + " where UserID='" + uid.Text + "' and Password='" + pwd.Text + "'";
        //    using (OleDbConnection con = new OleDbConnection(conExl))
        //    {
        //        using (OleDbCommand cmd = new OleDbCommand(qry,con ))
        //        {
        //            con.Open();
        //            OleDbDataReader dr = cmd.ExecuteReader();
        //            while (dr.Read())
        //            {
        //                if (dr.HasRows == true)
        //                {
        //                    Session["Name"] = dr["FullName"].ToString();
        //                    Response.Redirect("Welcomepage.aspx");
        //                }
        //            }
        //            if (dr.HasRows == false)
        //            {
        //                Response.Write("Either ID Or Password Is Incorrect.");
        //            }
        //        }
        //    }
        //}
        #endregion

        #region Connection MDF (SQL Server)
        //if ((!string.IsNullOrEmpty(uid.Text)) && (!string.IsNullOrEmpty(pwd.Text)))
        //{
        //    string ExlSheet = "MemberMaster";// Excel Sheet name;
        //    string qry = "Select * From " + "[" + ExlSheet + "$]" + " where UserID='" + uid.Text + "' and Password='" + pwd.Text + "'";
        //    using (SqlConnection con = new SqlConnection(conSQL))
        //    {
        //        using (SqlCommand cmd = new SqlCommand(qry, con))
        //        {
        //            con.Open();
        //            SqlDataReader dr = cmd.ExecuteReader();
        //            while (dr.Read())
        //            {
        //                if (dr.HasRows == true)
        //                {
        //                    Session["Name"] = dr["FullName"].ToString();
        //                    Response.Redirect("Welcomepage.aspx");
        //                }
        //            }
        //            if (dr.HasRows == false)
        //            {
        //                Response.Write("Either ID Or Password Is Incorrect.");
        //            }
        //        }
        //    }
        //}
        #endregion
    }
}


Cheers!!!

No comments:

Post a Comment