Solved

C# Share code in program

Posted on 2014-04-03
2
287 Views
Last Modified: 2014-04-03
Novice user here. I want to be able to share this with all my forms. How do I add the code to the project?

        private OleDbCommand GetCommand()
        {
            //set connection to Access 2007 database
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=""" + AppDomain.CurrentDomain.BaseDirectory + @"Project.accdb""";
            con.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = con;
            return cmd;
        }
0
Comment
Question by:Idarac
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39976587
The simplest way would be to create a static class:
public static class DataAccess
{
     public static OleDbCommand GetCommand()
     {
         ...
     }
}

Open in new window

Then from any form you can call it like:
OleDbCommand cmd = DataAccess.GetCommand();

Open in new window

0
 
LVL 1

Author Closing Comment

by:Idarac
ID: 39976606
Thank you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now