Solved

C# Share code in program

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

685 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