1

I am calling this method to my Login Form. I don't know what is wrong with this. I've created a class named MyConnection and this class contains my SQL ConnectionString. What i want is I can call this function ex. Class1 method named Myfunction would be called to my Login Form so calling a connection string would be faster.

public static class MyConnection
{
    public static SqlConnection getConnection()
    {                
        string conn = "Data Source=EDGAR-PC\\SQLEXPRESS;Initial Catalog=Project1;Integrated Security=True";
        SqlConnection myConn = new SqlConnection(conn);
        return myConn;    
    }    
}
4
  • 14
    I am really struggling to understand what your actual question is. Can you clarify? Commented Apr 3, 2013 at 16:46
  • What I want to do is call this instantiate this class to my Login form and call this method(getConnection) on that Form. so creating a connection to SQL Server would be faster. Commented Apr 3, 2013 at 16:50
  • Did you examine the object to see what is being returned? Also, are you opening the connection in your calling method? Commented Apr 3, 2013 at 16:56
  • @Daniel Kelley, it's really hard to understand, but he tries to call that function, not knowing how to instantiate a static class =). Pay attention to the "What i want is I can call this function..." Not everybody of us are guru. Commented Apr 3, 2013 at 17:07

2 Answers 2

5

You can't instantiate the static class. You can call it like this:

using (var connection = MyConnection.getConnection())
{
    connection.Open();
    //do whatever you need
}
Sign up to request clarification or add additional context in comments.

Comments

0

Since the static class cannot be instantiated, you will have to call like this:

private static void OpenSqlConnection(string connectionString)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.