Resolved public static function to return datatable? - public invalid

Rhodan

Well-known member
Joined
Apr 7, 2015
Messages
52
Programming Experience
10+
I'm trying to make a function to carry out SQL queries in my own libray but I can't figure out how to return the query result. Declaring the function "public static DataTable MyQuery(string sql) gives the error "public not valid". So I have to figure out how to get the data back to the caller in some other form that they can use. This could be a single value or multiple values in multiple rows so I am at a complete loss in how to proceed. Anyone have any ideas other than doing queries separately in several different forms?
 
Are you sure you put that code on a class? Show us the code where you are attempting to do this.

Your code should looks something like:
C#:
class Utility
{
    public static DataTable LoadData(string query)
    {
        // Your code to perform query here
    }
}
 
As an aside, most modern code doesn't use DataTable and DataSet anymore. People have moved on to using LINQ with Entity Framework or some other ORM.
 

Latest posts

Back
Top Bottom