Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# Share code in program

Posted on 2014-04-03
2
293 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hey!!!! 1 29
FTP file download using c# 3 25
What are the simplest ways to create a Merge Sort? 3 29
ASP.NET MVC -Update model when click on <li> element 29 35
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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