Solved

C# Class to return datatable

Posted on 2014-03-27
1
519 Views
Last Modified: 2014-05-20
Hi Experts

I'm new to C# I'm writing some code to return a datatable when a user passes a sql statement to a class, it's not working though as the class want's a sting returned.


 class SQL_Array
    {

        string SQL;
        DataTable results;

         public SQL_Array()
        {

        }

        public SQL_Array(string strSQL,DataTable result )
        {
            results = result;
            SQL = strSQL;
        }

        public string SQL_Results()
        {
           // string []SQL_Results;

            string SQL = "Select * from dmt.dbo.DIT_tblBlah where txtstaffid is not null";

            SqlConnection cnnDIT = new SqlConnection("Blah");
            cnnDIT.Open();

      

            SqlDataAdapter adap = new SqlDataAdapter(SQL, cnnDIT);

          // create an empty DataTable to hold the results
            DataTable results = new DataTable();

            // populate the DataTable. Note: the SqlDataAdapter will take care of opening/closing the connection for you
            adap.Fill(results);

            cnnDIT.Close();

            return results;

Open in new window

0
Comment
Question by:MrDavidThorn
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39959513
Change the return type of the method:

public DataTable SQL_Results()
...

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now