简体   繁体   中英

How to access static methode when its in class

Here I write some code in static methode which is in class file please Help me how can i create instance of static class

public class ConnectionString
    {

       public static void CreateCommand(string querystring, string connectionString)
        {
           using(SqlConnection cn = new SqlConnection(connectionString))
           {
               SqlCommand cmd = new SqlCommand(querystring, cn);
               cmd.Connection.Open();
               cmd.ExecuteNonQuery();
           }           
        }

    }

Just call it like this:

string querystring = "Your values here";
string connectionString = "Your values here";
ConnectionString.CreateCommand(querystring, connectionString);

That's it.

Your ConnectionString class can be refactored to implement an interface like this:

  public interface IDataAccess
    {
        void CreateCommand(string querystring, string connectionString);
    }

this interface allows us to inject its implementation in the controller that you mentioned in the comments. So your ConnectionString class (renamed to more meaningful name DataAccess ) should look like this:

 public class DataAccess : IDataAccess
    {       

        public void CreateCommand(string querystring, string connectionString)
        {
            using (SqlConnection cn = new SqlConnection(connectionString))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(querystring, cn))
                {
                    cmd.ExecuteNonQuery();
                }             
            }
        }
    } 

then in your controller / client class you can have the concrete implementation injected at the run time..

public class DataController : Controller
{
    private readonly IDataAccess dataAccess;
    public DataController(IDataAccess dataAcces)
    {
        this.dataAccess = dataAcces;
    }

    public ActionResult ShowData()
    {
        string querystring = "you t-sql query";
        string connectionString = "<you sql connection string>";

        this.dataAccess.CreateCommand(querystring, connectionString);

        return this.View();
    }
} 

If you are using MVC and dont know how to resolve the dependencies then refer to this article

Alternatively you can just new up the instance of DataAccess class like this:

public class DataController : Controller
    {
        private readonly IDataAccess dataAccess;
        public DataController()
        {
            this.dataAccess = new DataAccess();
        }

        public ActionResult ShowData()
        {
            string querystring = "you t-sql query";
            string connectionString = "<you sql connection string>";

            this.dataAccess.CreateCommand(querystring, connectionString);

            return this.View();
        }
    }

I will not recommend this approach as it wont be possible to unit test it.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM