在C#中将嵌套集合写入数据表可以通过以下步骤实现:
以下是一个示例代码:
// 嵌套集合示例数据
List<List<string>> nestedList = new List<List<string>>();
nestedList.Add(new List<string> { "John", "Doe", "john.doe@example.com" });
nestedList.Add(new List<string> { "Jane", "Smith", "jane.smith@example.com" });
// 连接到数据库
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// 遍历嵌套集合并插入数据
foreach (List<string> innerList in nestedList)
{
// 创建SQL插入语句
string sql = "INSERT INTO YourTable (Column1, Column2, Column3) VALUES (@Value1, @Value2, @Value3)";
// 创建命令对象
using (SqlCommand command = new SqlCommand(sql, connection))
{
// 设置参数值
command.Parameters.AddWithValue("@Value1", innerList[0]);
command.Parameters.AddWithValue("@Value2", innerList[1]);
command.Parameters.AddWithValue("@Value3", innerList[2]);
// 执行插入操作
command.ExecuteNonQuery();
}
}
}
上述代码中,假设已经定义了一个名为YourTable的数据表,包含三个列Column1、Column2和Column3,分别对应嵌套集合中的三个数据项。代码通过循环遍历嵌套集合,将每个内部列表的数据插入到数据表中。
请注意,上述示例代码仅为演示目的,实际应用中可能需要根据具体情况进行适当的修改和优化。
腾讯云相关产品和产品介绍链接地址:
请注意,以上链接仅为示例,具体产品选择应根据实际需求和情况进行评估和决策。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云