c# - 如何遍历复杂类型对象列表并传递给调用 SQL 存储过程的方法

标签 c# arrays sql-server json foreach

如何循环遍历对象列表,以便将所述对象传递给通过存储过程在 SQL 数据库中插入行的方法?

求助in this question我到了这一点:

namespace NA.Controllers
{
   public class NC : ApiController
   {
      [Route("AddNote")]
      [HttpPost]
    public HttpResponseMessage PostNote(List<Note> items)
    {
        //NoteJson deserializednote = JsonConvert.DeserializeObject<NoteJson>(item);
        //Note notesdata = new Note(item);
        NotesAccept.Models.INoteRepository Repository = new NotesAccept.Models.NoteDataRepository();
        foreach (Note item in items)
        {

        item = Repository.Add(item);

        }
        var response = Request.CreateResponse<List<Note>>(HttpStatusCode.OK, items);
        return response;
    }
   }
}

但现在我卡住了,因为 item= 现在是一个迭代变量,但我需要将它传递给一个方法:

namespace NA.Models
{
  class NoteDataRepository : INoteRepository
  {
     public void Add(Note item)
     {
        if (item == null)
        {
            throw new ArgumentNullException("item");
        }
        else
        {
            String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "BL_IntegrationInsertNote";
            cmd.Parameters.Add("@Client", SqlDbType.VarChar).Value = item.Client.Trim();
            cmd.Parameters.Add("@Case", SqlDbType.VarChar).Value = item.Case;
            cmd.Parameters.Add("@Text", SqlDbType.VarChar).Value = item.Text.Trim();
            cmd.Parameters.Add("@When", SqlDbType.DateTime).Value = item.Date;
            cmd.Parameters.Add("@Ext", SqlDbType.Bit).Value = item.Type;
            cmd.Parameters.Add("@return", SqlDbType.Int).Direction = ParameterDirection.Output;
            cmd.Connection = con;

            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                string id = cmd.Parameters["@return"].Value.ToString();
                string lblMessage = null;
                lblMessage = "Record inserted successfully. ID = " + id;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }    
        //return item;
    }

    IEnumerable<Note> INoteRepository.GetAll()
    {
        throw new NotImplementedException("getitems");
    }
}
}

我对 C# 还是菜鸟,所以我不知道如何实现它,尤其是因为整个解决方案仍然是来自整个网络的“复制和粘贴”,而且整个网络好奇地专注于遍历简单类型。如何使用复杂类型做到这一点?

如其他问题所述,这是一个职业生死攸关的问题(我是数据库开发人员,而不是 VS 大师,尤其是在两天两夜之后)。

最佳答案

  1. 您仍然忘记将该 ID 从 DB 分配给该项目。

  2. 你还有

    return item;
    

    在不返回任何内容的方法中 (public void Add(Note item))。

    所以只需删除返回行即可。

  3. 并替换

    item = Repository.Add(item);
    

    只是

    Repository.Add(item);
    

关于c# - 如何遍历复杂类型对象列表并传递给调用 SQL 存储过程的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35293864/

相关文章:

c# - 如何降低 Switch case 语句的圈复杂度

arrays - 将二维数组发布到 Excel 工作表

c++ - 数组的大小

sql-server - 使用 SQL 选择 @ 参数

sql-server - 日志传送数据库的限制

c# - 如何在 Json 字符串中添加 JArray

c# - 我需要在父级而不是子级中 InsertBefore 或 InsertAfter

c# - 等效的 Java 泛型循环引用 C#

C++ 写入 myArray[0] 并设置 myInt = myFloat,为什么这样工作?

c# - Entity Framework - 获取 SQL Server 字段数据类型