0

ユーザー定義のテーブル型をストアドプロシージャに渡しています。しかし、 "オブジェクト型System.Collections.Generic.List`1から既知のマネージ型プロバイダのネイティブ型へのマッピングが存在しないため、エラーが発生します。"ストアドプロシージャのUDT型:エラー - オブジェクト型System.Collections.Generic.List'1から既知のマネージプロバイダのネイティブ型へのマッピングがありません

UserDefinedTableType: 
CREATE TYPE UserQueryAttachmentList AS TABLE 
(
[UserQueryId] NVARCHAR(50), 
[AttachmentPath] NVARCHAR(200), 
[AttachmentName] NVARCHAR(200), 
[AttachmentFor] NVARCHAR(50), 
[CreatedBy] NVARCHAR(50) 
); 

Stored Procedure: 
CREATE PROCEDURE PROC_UserQueryAttachment_Insert 
(
@Table UserQueryAttachmentList READONLY 
) 
AS 
BEGIN 

INSERT INTO dbo.[UserQueryAttachments] 
(
    UserQueryId, 
    AttachmentPath, 
    AttachmentName, 
    AttachmentFor, 
    CreatedBy, 
    CreatedDate 
) 
SELECT 
    UserQueryId, 
    AttachmentPath, 
    AttachmentName, 
    AttachmentFor, 
    CreatedBy, 
    GETDATE() 
FROM 
    @Table T 

END 

C#: 
public override bool SaveUserQueryAttachment(List<UserQueryAttachmentToCreate> fileList) 
    { 

     try 
     { 
      this.ExecuteStoredProcedureOrQuery<UserQueryAttachmentToCreate>("PROC_UserQueryAttachment_Insert", CommandType.StoredProcedure, 
       new SqlParameter("@Table", fileList) 
       ); 
      return true; 

     } 
     catch (Exception ex) 
     { 
      return false; 
     } 
    } 

なぜこのエラーが発生するのか教えてください。

+0

DataTableをリストではなくストアドプロシージャに送信する必要があります。 –

答えて

1

オブジェクトの一覧をSQL Serverに直接渡すことはできません。

public override bool SaveUserQueryAttachment(List<UserQueryAttachmentToCreate> fileList) 
{ 
    // TODO: open a SQL connection here 

    using (SqlCommand cmd = new SqlCommand("exec PROC_UserQueryAttachment_Insert @table", connection)) 
    { 
     using (var table = new DataTable()) 
     { 
      table.Columns.Add("UserQueryId", typeof(string)); 
      table.Columns.Add("AttachmentPath", typeof(string)); 
      table.Columns.Add("AttachmentName", typeof(string)); 
      table.Columns.Add("AttachmentFor", typeof(string)); 
      table.Columns.Add("CreatedBy", typeof(string)); 

      table.Rows.Add(fileList.ToArray()); 

      var list = new SqlParameter("@table", SqlDbType.Structured); 
      list.TypeName = "dbo.UserQueryAttachmentList"; 
      list.Value = table; 

      cmd.Parameters.Add(list); 
      cmd.ExecuteReader(); 
     } 
    } 

    // TODO: close the SQL connection 
} 
関連する問題