首页 > 解决方案 > 将 list<> 内容插入 SQL

问题描述

我的代码中有一些函数,它们返回一个包含一些项目的列表。我需要在不同的行中将每个列表组件插入到 SQL 中,但实际上不知道如何。

任何人都可以帮助它吗?我将在下面留下代码

string RecursosInfra = "Email, Impressao, Desktop";
string[] recurso = Regex.Split(RecursosInfra, @"\W+");

List<string> ListaRecursosInfra = new List<string>();

foreach (string valor in recurso)
{
    ListaRecursosInfra.Add(valor);
}

要将常用值插入我的 SQL DB,我在 DAL 层中使用了类似的东西:

string id;
AbriConexao();
Tr = Con.BeginTransaction();

String query =
    "INSERT INTO[dbo].[TI01A]" +
    "  ([Name]    " +
    "  ,[Age])            " +
    " VALUES                       " +
    "  (@Name     " +
    "  ,@Age);             " +
    "  SELECT SCOPE_IDENTITY()";

//string insertedID = dbcm.ExecuteScalar();
//// This will insert the record and also get the id of the inserted record in database

Cmd = new SqlCommand(query, Con, Tr);
Cmd.Parameters.AddWithValue("@Name", s.Name);
Cmd.Parameters.AddWithValue("@Age", s.Age);

id = Cmd.ExecuteScalar().ToString();
//Cmd.ExecuteNonQuery();

Tr.Commit();

return id;

标签: c#asp.netasp.net-mvc

解决方案


推荐阅读