C#;SQL Server:将数组直接移动到SQL Server中

本文关键字:SQL Server 移动 数组 | 更新日期: 2023-09-27 18:00:44

是否可以以某种方式将二维数组直接移动到数据库(SQL Server)中,而不将其转换为SQL语句?

我知道vb6.0中有某种直接连接,但我不记得当时是如何实现的,也不记得是否仍然可能。

C#;SQL Server:将数组直接移动到SQL Server中

在VB6中,您可以通过创建一个记录集、在其中插入行并调用.Update来避免显式的INSERT语句。如果Command模式为adCmdTableDirect,则这可能是"直接连接"。

在ADO.NET中,您也可以这样做,但有很多方法,例如Linq-To-Sql或实体框架。如果你想要一个普通的、低级的、类似VB6的样式,那可能就是DataAdapter.Update方法。

using (System.Data.SqlClient.SqlConnection c = new System.Data.SqlClient.SqlConnection("Data Source=..."))
{
    using (System.Data.SqlClient.SqlDataAdapter a = new System.Data.SqlClient.SqlDataAdapter("select * from atable;", c))
    {
        using (System.Data.SqlClient.SqlCommandBuilder b = new System.Data.SqlClient.SqlCommandBuilder(a))
        {
            using (DataTable t = new DataTable("atable"))
            {
                a.FillSchema(t, SchemaType.Source);
                t.Rows.Add(1, "foo");
                t.Rows.Add(2, "bar");
                a.Update(t);
            }
        }
    }
}