Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
as follow :

as Follow : 

          public List<T> GetDataReader<T>(string Command, int Cap)
             {
            SqlConnection con = this.openConnection();
            List<T> arr = new List<T>(Cap);
            object[] values = null;
            int fieldCount = 0;
            try
            {
                using (SqlCommand comm = new SqlCommand())
                {
                    comm.Connection = con;
                    comm.CommandText = Command;
                    comm.CommandType = CommandType.Text;
                    using (var reader = comm.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            values = new object[reader.FieldCount];
                            fieldCount = reader.GetValues(values);
                            for (int i = 0; i < fieldCount; i++)
                            {
                              
                                if(values[i] != System.DBNull.Value)
                                arr.Add((T)values[i]);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return arr;
        }


when Call the Method as below : 

      List<string>() = MyClass.GetDataReader<string>("select * from dbo.Users", 50);

i faced Syntax Error??!!


What I have tried:

I changed called method to any typed , but faced Error
Posted
Updated 29-Jan-18 3:44am
Comments
F-ES Sitecore 29-Jan-18 9:00am    
When you get an error always say what the error is and what line it is on.
[no name] 29-Jan-18 9:21am    
First of all while calling the GetDtaReader<t> function you should not pass sql commands.It should be parameterized.There is another way with reflection where you can convert data readers to list or ienumerable objects.

1 solution

Your syntax error is obvious:
C#
List<string>() = MyClass...
You need to name the variable, and get rid of the brackets:
C#
List<string> myList = MyClass...

But... as debasish suggested, passing an SQL command as text is very dangerous: it exposes you to SQL Injection if you aren't very, very careful - and that can delete your database!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900