Исключение: Operand type clash: int is incompatible with DaysAndHours at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
Вышеуказанное исключение возникает, когда я добавляю значение типа int к SQL параметрам. Как передать значение int в sql параметров.
C# код:
public bool VendorDaysAndHoursInfoByMemberID(UpdateVendaorDaysAndHoursDM updateVendaorDaysAndHoursDM)
{
_logger.LogInformation("VendorRepository.vendorDaysAndHoursInfoByMemberID method called!!!");
var memberId = updateVendaorDaysAndHoursDM.MemberID;
var vendorDaysAndHoursDMsList = updateVendaorDaysAndHoursDM.DaysAndHours.Select(c => c).Select(c => c).ToList();
DataTable dtVendorDaysAndHoursDMsList = DataReaderToList.ToDataTable<DaysAndHoursDM>(vendorDaysAndHoursDMsList);
using (SqlConnection conn = new SqlConnection(GetComapnyConnection.ConnectionString))
{
conn.Open();
using (var transaction = conn.BeginTransaction())
{
try
{
SqlCommand cmd = new SqlCommand("exec VendorDaysAndHoursInfoByMemberID @MemberID, @DaysAndHours", conn, transaction);
cmd.CommandTimeout = 0;
cmd.Transaction = transaction;
cmd.Parameters.AddWithValue("@MemberID", SqlDbType.Int).Value = memberId;
//cmd.Parameters.Add(new SqlParameter()
//{
// ParameterName = "@MemberID",
// SqlDbType = SqlDbType.Structured,
// Value = memberId,
// TypeName = "MemberID"
//});
var pDaysAndHoursDMsList = new SqlParameter("@DaysAndHours", SqlDbType.Structured);
pDaysAndHoursDMsList.TypeName = "DaysAndHours";
if (dtVendorDaysAndHoursDMsList.Rows.Count == 0)
pDaysAndHoursDMsList.Value = null;
else
pDaysAndHoursDMsList.Value = dtVendorDaysAndHoursDMsList;
cmd.Parameters.Add(pDaysAndHoursDMsList);
var dataReader = cmd.ExecuteReader();
var isResult = DataReaderToList.DataReaderMapToList<bool>(dataReader);
dataReader.Dispose();
transaction.Commit();
conn.Close();
return true;
}
catch (Exception e)
{
transaction.Rollback();
conn.Close();
_logger.LogError(e.Message);
return false;
}
}
}
}
Мне нужно разместить данные в следующем формате.
{
"MemberID":"a7f1819db38ccb5e03ad52396eafa5ac" ,
"DaysAndHours":[
{"DayName":"Sunday","StartTime":"08:00:00","EndTime":"17:00:00","Available":false},
{"DayName":"Monday","StartTime":"08:00:00","EndTime":"17:00:00","Available":true},
{"DayName":"Tuesday","StartTime":"08:00:00","EndTime":"17:00:00","Available":true},
{"DayName":"Wednesday","StartTime":"08:00:00","EndTime":"17:00:00","Available":true},
{"DayName":"Thursday","StartTime":"08:00:00","EndTime":"17:00:00","Available":true},
{"DayName":"Friday","StartTime":"08:00:00","EndTime":"17:00:00","Available":true},
{"DayName":"Saturday","StartTime":"08:00:00","EndTime":"19:00:00","Available":true}
]
}
Как передать значение типа int к SQL параметрам в указанном формате.
Хранимая процедура:
Create proc [dbo].[VendorDaysAndHoursInfoByMemberID]
(
@DaysAndHours as [dbo].DaysAndHours readonly,
@MemberID int,
@IsSuccess bit= 'false' output
)
as begin
SET NOCOUNT ON;
update DaysAndHours set
DaysAndHours.DayName=DaysAndHoursData.DayName,
DaysAndHours.StartTime=DaysAndHoursData.StartTime,
DaysAndHours.EndTime=DaysAndHoursData.EndTime,
DaysAndHours.Available=DaysAndHoursData.Available
from @DaysAndHours DaysAndHoursData
where MemberID = @MemberID;
set @IsSuccess = 'true'
End
return @IsSuccess
Код не отображает ошибок. Но isResult
получает ноль, который находится ниже DataReader.