Creating a generic template in C#

JohnCTX 636 Reputation points
2021-01-20T06:12:29.877+00:00

I have researched that C#, as well as its predecessor, C++, enable users to create generic templates.

Here are the two source code snippets below:

using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.IO;

namespace Access_mdb
{
    public class DataOperations
    {
        public static string ConnectionString =
            $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "[DatabaseFileName].mdb")};";

        public static List<string> Read()
        {

            var list = new List<string>();

            using (var cn = new OleDbConnection() { ConnectionString = ConnectionString })
            {
                using (var cmd = new OleDbCommand() { Connection = cn })
                {
                    cmd.CommandText = "SELECT [FieldOne] FROM tblOne;";

                    cn.Open();

                    var reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        list.Add(reader.GetString(0));
                    }

                    cn.Close();
                }
            }
            return list;
        }
    }
}

and here is the second source code snippet below:

using System;
using System.IO;
using System.Data.OleDb;
using System.Collections.Generic;


namespace Access_mdb
{
    public class DataOpsTwo
    {
        public static string connectionstring = $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "[DatabaseFileName.mdb]")};";

        public static List<string> ReadOne()
        {
            var listOne = new List<string>();

            using (var connection = new OleDbConnection() { ConnectionString = connectionstring })
            { 
            using (var cmdOle1 = new OleDbCommand() { Connection = connection })
                {
                    cmdOle1.CommandText = "SELECT [FieldOne] FROM tblTwo;";
                    connection.Open();
                    var readerOne = cmdOle1.ExecuteReader();
                    while (readerOne.Read())
                    {
                        listOne.Add(readerOne.GetString(0));
                    }
                    connection.Close();
                    }
                }
            return listOne;
            }
        }


    }

What I want to perform is to create a generic template, so that I do not have to perform double or repetitive work on creating different classes.

Is there any way or method on how to perform this effecitively?

Regards,

JohnCTX

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,892 questions
Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
741 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,913 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,159 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,626 Reputation points
    2021-01-20T07:37:28.843+00:00

    Hi JohnCTX-6479,
    First, you can add a new class in your project via following steps(as a template):
    Right-click your project name->Add->Class->Change class name to DataOperations.cs
    Then you need put the template code into DataOperations class.
    In this step, I suggest you construct a function with parameters and pass in "ConnectionString" as a parameter.
    Due to the Read method is static, so you can call it via class name directly.

    class DataOperations  
    {  
        public static string ConnectionString = $"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "[DatabaseFileName].mdb")};";  
      
        public static List<string> Read(string s)  
        {  
            var list = new List<string>();  
      
            using (var cn = new OleDbConnection() { ConnectionString = s })  
            {  
                using (var cmd = new OleDbCommand() { Connection = cn })  
                {  
                    cmd.CommandText = "SELECT [FieldOne] FROM tblOne;";  
      
                    cn.Open();  
      
                    var reader = cmd.ExecuteReader();  
                    while (reader.Read())  
                    {  
                        list.Add(reader.GetString(0));  
                    }  
      
                    cn.Close();  
                }  
            }  
            return list;  
        }  
    }  
      
      
    public class DataOpsTwo  
    {  
           public void test()   
           {  
                 List <string> list= DataOperations.Read(DataOperations.ConnectionString);//you can also pass another connectionString  
            }  
    }  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.