user7703620
user7703620

Reputation:

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();
           }           
        }

    }

Upvotes: 0

Views: 59

Answers (2)

Yawar Murtaza
Yawar Murtaza

Reputation: 3865

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!

Upvotes: 0

Willy David Jr
Willy David Jr

Reputation: 9131

Just call it like this:

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

That's it.

Upvotes: 2

Related Questions