如何将所有记录从SQL SERVER数据库表保存到数组中?

时间:2022-09-05 21:21:36

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#.

我的数据库中有一个表。我想将所有SQL SERVER数据库表记录保存到数组中。我搜索到的所有地方都是将数据从dataTable复制到数组。是否可以将所有记录从数据库表保存到数组中?如果有,请帮助我。我想在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.

编辑:我想处理DataTable中的所有行,但是通过Datatable逐行处理会占用更多时间。所以我正在寻找更好的性能与其他功能,如阵列或结构可能。

2 个解决方案

#1


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();
            }
        }

    }

#2


0  

You could use EF and load your data like this:

您可以使用EF并加载您的数据,如下所示:

var data = context.YourTable.ToList();

var data = context.YourTable.ToList();

learn about EF

了解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

但请注意,如果你有一个大表,它可能会减慢你的应用程序,然后你必须做一些解决方法,如分页。通常你可能想在Where扩展方法中传递一些标准

also you can run a raw SQL query with EF:

您也可以使用EF运行原始SQL查询:

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

context.Database.SqlQuery (“SELECT * FROM YourTable”)

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

选择如何连接到数据库是一个偏好的问题,我更喜欢EF,你可以使用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);
    }
}

#1


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();
            }
        }

    }

#2


0  

You could use EF and load your data like this:

您可以使用EF并加载您的数据,如下所示:

var data = context.YourTable.ToList();

var data = context.YourTable.ToList();

learn about EF

了解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

但请注意,如果你有一个大表,它可能会减慢你的应用程序,然后你必须做一些解决方法,如分页。通常你可能想在Where扩展方法中传递一些标准

also you can run a raw SQL query with EF:

您也可以使用EF运行原始SQL查询:

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

context.Database.SqlQuery (“SELECT * FROM YourTable”)

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

选择如何连接到数据库是一个偏好的问题,我更喜欢EF,你可以使用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);
    }
}