0

I have a table in my database. I want to save all SQL SERVER Database Table records into the array.I searched everywhere all I am getting is to copying data from dataTable to array. Is that possible save all records into the array from the database table? If yes please help me. I would like to get the code in C#.

Edited: I want to process all the rows in the DataTable but going row by row through Datatable is consuming more time. So I am looking for better performance with other features like arrays or structures may be.

6
  • SqlDataReader + Instantiate class by passing in field values, or using reflection. Commented Apr 24, 2017 at 6:56
  • 1
    Have a look at github.com/StackExchange/Dapper. Commented Apr 24, 2017 at 6:58
  • Are you looking for a solution like EntityFramework or NHibernate? Commented Apr 24, 2017 at 7:03
  • @Habeeb In ADO.NET Commented Apr 24, 2017 at 7:15
  • @john Can you explain me with a code or link. It will be more helpful. Commented Apr 24, 2017 at 7:24

2 Answers 2

2

Not sure what you are trying but seems pretty expensive and probably not a good idea , we can probably find another approach to deal with your problem and take this one out completely.

However for your question , the answer goes something like this:

    public static void Main(string args[])
    {

        List<object> objectList = new List<object>();
        var commandText = "Select name  from sys.tables";

        SqlConnection sqlConn = null;//Initialize
        SqlCommand command = new SqlCommand(commandText, sqlConn);

        var sqlReader = command.ExecuteReader();

        while (sqlReader.Read())
        {
            commandText = $"Select * from {sqlReader["name"]}";
            command = new SqlCommand(commandText, sqlConn);

            var subReader = command.ExecuteReader();

            while (subReader.Read())
            {
                //Loop through and add to list
                objectList.Add();
            }
        }

    }
Sign up to request clarification or add additional context in comments.

2 Comments

I am not understanding this part in your code commandText = $"Select * from {sqlreader["name"]}"; can you explain me
@DilipKumar, thats a dynamic string which will create a select command for all the tables in database. It's called Interpolated String. msdn.microsoft.com/en-us/library/dn961160.aspx
0

You could use EF and load your data like this:

var data = context.YourTable.ToList();

learn about EF

but be aware that if you have a big table it may slow down your application and then you have to do some workarounds like paging. usually you may want to pass some criteria in Where extension method

also you can run a raw SQL query with EF:

context.Database.SqlQuery<YourMappingClass>("SELECT * FROM YourTable")

choosing how to connect to your databse is a matter of preference, I prefer EF you can use ADO.NET.

Using ADO:

    using(SqlConnection conn = new SqlConnection()) 
    {
        conn.ConnectionString = "Server=[server_name];Database=[database_name];Trusted_Connection=true";

    SqlCommand command = new SqlCommand("SELECT * FROM YourTable", conn);
    }
using (SqlDataReader reader = command.ExecuteReader())
{
    var list = new List<YourMappingClass>();
    while (reader.Read())
    {

        var obj = new YourMappingClass();
        obj.Prop1=reader[0];
        obj.Prop2=reader[1];
        list.Add(data);
    }
}

4 Comments

I would prefer to use ADO.NET code. If possible can you help me with ADO.NET code?
@DilipKumar Simsons answer is ADO.NET
@bradbury9 Yes Just now I saw. Looking on that
See the amended answer

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.