简体   繁体   中英

Connecting to a database in ASP.NET vs. Classic ASP

I have years of programming experience with ASP, but I am novice to ASP.NET programming.

I was looking at the codes of a one website and I noticed programmer who coded this website made two connections to the database. One in Web.config, other in the ASPX page.

Web.config has this:

<connectionStrings>
    <add name="pearl" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Hosting\7195242\html\db\xxx.mdb" providerName="System.Data.OleDb" />  
</connectionStrings>

ASPX page has this:

<asp:AccessDataSource ID="AccessDataSource1" runat="server"
    DataFile="D:\Hosting\7195242\html\db\xxx.mdb"
    SelectCommand="SELECT * FROM [Pearl_PageContents]">
</asp:AccessDataSource>

With ASP, I would usually create one ASP file called connection.asp, add my database connection codes in this file and attach it to all other ASP pages that interact with the databse. Why is it different with ASP.NET?

The first one is not a connection. It's just the declaration of a connection string. Some other piece of code will use the connection string to connect to the database, perhaps using code somewhat similar to what you're used to with ADODB.

The second one is the use of a data source control. This is a control which can supply data to another control. Nothing like it exists in ASP. It allows for purely-declarative web forms, where you can declare the data in one control and have a FormView or GridView control consume that, doing CRUD operations with no additional code at all.

I recommend you begin with the tutorials at http://www.asp.net .

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Volta_Reporting_Application.DBL
{
    public class DBHandler
    {
        public SqlConnection _SqlConnection { get; set; }
        public String _SqlConnectionString { get; set; }
        public DataSet _DataSet { get; set; }
        public List<SqlCommand> _CommandList { set; get; }
        public DBHandler()
        {
            //_SqlConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; ;
            _SqlConnectionString = Helpers.Helper.GetConnectionString();
        }
        public bool OpenConnection()
        {

                _SqlConnection = new SqlConnection(_SqlConnectionString);
            if (SqlConnection != null && SqlConnection.State == ConnectionState.Closed)
            {

                _SqlConnection.Open();
            }
   .Open);
        }

        public bool CloseConnection()
        {
            if (SqlConnection != null && SqlConnection.State == ConnectionState.Open)
                _SqlConnection.Close();
            if (_SqlConnection != null)
                _SqlConnection.Dispose();
            return _SqlConnection == null;
        }
        public object ExecuteMyCommand(SqlCommand cmd) 
        {
            bool res = false;
            try
            {
                OpenConnection();
                cmd.Connection = _SqlConnection;
                if (cmd.ExecuteNonQuery() > 0)
                {
                    res = true;
                }
            }
            catch (Exception)
            {

                res = false;
            }
            finally 
            {
                CloseConnection();
            }
            return res;
        }

        public object CRUD(string Query, char operation = 'c')
        {
            operation = char.Parse(operation.ToString().ToLower());
            object res = null;
            try
            {
                OpenConnection();
                SqlDataAdapter da = new SqlDataAdapter();
                switch (operation)
                {
                    case 'c':
                    case 'i':
                        da.InsertCommand = _SqlConnection.CreateCommand();
                        da.InsertCommand.CommandText = Query;
                        da.InsertCommand.ExecuteNonQuery();
                        res = true; 
                        break;
                    case 'z':
                        da.SelectCommand = _SqlConnection.CreateCommand();
                        da.SelectCommand.CommandText = Query;
                        return da.SelectCommand.ExecuteScalar();
                    case 's':
                    case 'r':
                        DataSet ds = new DataSet();
                        da.SelectCommand = _SqlConnection.CreateCommand();
                        da.SelectCommand.CommandText = Query;
                        //da.SelectCommand.ExecuteReader();
                        da.Fill(ds);
                        res = ds;
                       //ds.Dispose(); 
                        break;
                    case 'u':
                        da.UpdateCommand = _SqlConnection.CreateCommand();
                        da.UpdateCommand.CommandText = Query;
                        res=da.UpdateCommand.ExecuteNonQuery();
                        break;
                    case 'd':
                        da.DeleteCommand = _SqlConnection.CreateCommand();
                        da.DeleteCommand.CommandText = Query;
                        da.DeleteCommand.ExecuteNonQuery();
                        res = true;
                        break;
                    default: break;
                }
                if (res == null)
                    res = false;
            }
            catch (Exception)
            {
                res = null;
            }
            finally
            {
                CloseConnection();
            }
             return res;
        }
    }
}

Web.config is just so you can store global variables. There is technically nothing wrong with just creating another class and holding the same data in that cs file. However with web.config you can easily edit that connection, add more connections strings to that file, and then use ConnectionManager to grab any of those connection strings. You can also encrypt your web.config file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM