I get a "No Mapping exists" error on my code that should be inserting records into a SQL Server table. I can tell that it has something to do with the parameter setup I have, but not much more than that. From my googling, my basic code structure seems to line up with how to use SQL parameters and C# but I'm clearly missing something. Image below of offending code:
EDIT Per Feedback, adding code block:
using (SqlConnection connection = new SqlConnection(ConnHelper.CnnVal("DWTest")))
{
connection.Open();
SqlParameter jsonText = new SqlParameter("@Param1", System.Data.SqlDbType.NVarChar, -1)
{
Value = PostResult
}; //-1 = max
string sqlCMD =
"DECLARE @json NVARCHAR(MAX) " +
" SET @json = N'[ @Param1 ]'" +
"DECLARE @FormattedJson NVARCHAR(MAX) = REPLACE(REPLACE(@json, '{\"success\":1,\"data\":[',''),']}','')" +
"Insert into Staging.CDOPSTest" +
" SELECT * FROM OPENJSON(@FormattedJson) " +
" WITH (" +
"CallFrom char(10) 'strict $.call_from'," +
"Direction BIT '$.direction'," +
"[Hour] INT '$.hour'," +
"[Minute] INT '$.minute'," +
"ReachedServerLevel CHAR(5) '$.reached_service_level'," +
"CallDate NVARCHAR(256) '$.date'," +
"TalkTime INT '$.talk_time'," +
"AnswerSpeed INT '$.speed_to_answer'," +
"DID INT '$.did'," +
"StartTime NVARCHAR(256) '$.start_time'," +
"Endtime NVARCHAR(256) '$.end_time'," +
"ContactID BIGINT '$.contact_id'," +
"AccountType NVARCHAR(256) '$.account_type'" +
")";
SqlCommand cmd = new SqlCommand(sqlCMD, connection);
cmd.Parameters.AddWithValue("Param1", jsonText);
cmd.ExecuteNonQuery();
connection.Close();
}
