Connect to oracle database with asp.net application

0 comments

      

              Connect to oracle database with asp.net application


Today we are going to learn how to connect to oracle database with asp.net simple application.
















Steps to follow:-


  1. you should know that all the previous methods to connect to oracle database is  now obsolete that's why I am showing you a new method to apply and believe      me this is method  i have known so far.

 2. first you need to download ODAC oracle data access component, to download click on the link

 3. Install it in your local computer , you just have to click next and next. 

 4. After that open visual studio and follow the instruction given below.

 5. first you need to provide a reference to the project click on reference and add reference than browse. after that browse on installed odac and include this dll Oracle.ManagedDataAccess.dll.
        
  path to folder where you can find dll :-  
        C:\app\client\yourusername\product\12.1.0\client_1\odp.net\managed\common\Oracle.ManagedData               Access.dll
  1. for those who are developer open any file like default.aspx and create a new function.
       private void storedatatodatabase()
        {
             string oradbcon = ConfigurationManager.ConnectionStrings["oradbc"].ConnectionString;

     //here goes your rest of the code.whatever you want to do with database.like....

                using (OracleConnection con = new OracleConnection(oradbcon))
                {

                    OracleCommand cmd = new OracleCommand("insertdata", con);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    con.Open();

                    cmd.Parameters.Add("x", parametervalue);
                    cmd.Parameters.Add("y", parametervalue);
                    cmd.Parameters.Add("z", parametervalue);
                    cmd.Parameters.Add("z", parametervalue);
                    cmd.Parameters.Add("z", parametervalue);
                    cmd.Parameters.Add("z", parametervalue);
                    cmd.Parameters.Add("z", System.DateTime.Now);
                    cmd.Parameters.Add("z", parametervalue);

                    int i = cmd.ExecuteNonQuery();
         }

      here string oradbcon is a connection string which i will explain later and i have used a stored procedure to connect to the database and insert data in to it .


6. Now you need a string oradbcon  which will be stored in web.config file, you can also use it in the default.aspx but the best practice is to implement it in web.config file so that you dont have to write whole code again and again, you just have to copy first line of code given above.

7.open web.config file and paste the code.

<connectionStrings>
   <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data                          Source=(LocalDb)\v11.0;Initial Catalog=aspnet-test1-20150707124857;Integrated                           Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-test1-20150707124857.mdf"                                 />
     <add name="oradbc"
           providerName="Oracle.ManagedDataAccess.Client"
           connectionString="DataSource=db_css;Persist Security Info=True;User                                                        ID=your_id;Password=password;"/>
  
  </connectionStrings>

  <oracle.manageddataaccess.client>
    <version number="4.121.2.0">
      <dataSources>
        <dataSource alias="db_css"
                    descriptor="(DESCRIPTION=(ADDRESS = (PROTOCOL = TCP)(HOST =                                                             hostname.com)(PORT = 1521))
                                         (CONNECT_DATA =(SERVICE_NAME =dbservicename)));"/>
      </dataSources>
      <edmMappings>
        <edmMapping dataType="number">
          <add name="bool" precision="1"/>
          <add name="byte" precision="2" />
          <add name="int16" precision="5" />
        </edmMapping>
       </edmMappings>
    </version>
  </oracle.manageddataaccess.client>



8. Save it and your ready to go. if the connection is not establish than check 
connection string name
HOST            ----- where your db is hosted
userid          ------- any userid or schema through which you want to connect to database.
password   -------- password of user
SERVICE_NAME :type show parameter service in your db it will give you service name.
version number -----version number odbc 

9. you can call this function anywhere in that file.

How to deploy on production:-

its very easy 
1. when you have build your project and copied all folder to your production server where your application is hosted. then check the BIN folder.

2. you need to paste this dll Oracle.ManagedDataAccess.dll.   
     in your bin folder also. after that it will work at least work for me.
    hope it will work for you also.


 please share and comment if you like this post.

                                       

                                   for you....


    
Kindly Bookmark this Post using your favorite Bookmarking service:


Post a Comment

Note: only a member of this blog may post a comment.