I am using Visual Studio, I'm trying to make a Insert statement using C# and Dynamic SQL.
SQL Command:
comando.CommandText = @"
declare @sql nvarchar(max);
set @sql = 'INSERT INTO ' + @curso + '(Data_Local, Deletado) VALUES ( ' + @date + ', 0)';
exec (@sql);
";
@curso is the Table Name, it's working, Data_Local is a Datetime value, which is not working, and Deletado is a bit value, which is working.
Declaring @curso:
comando.Parameters.Add("@curso", SqlDbType.NVarChar);
comando.Parameters["@curso"].Value = idCurso;
Declaring @date
comando.Parameters.Add("@date", SqlDbType.DateTime);
comando.Parameters["@date"].Value = dateTimePicker2.Value.Date;
Then after:
comando.ExecuteNonQuery();
The error message appears:
'Conversion failed when converting date and/or time from character string.'
What should I do to read the value from the DateTimePicker, insert into the @date variable, and add it to the SQL Command properly?
Also, I have another doubt, where can I see how the SQL Command is sent? I need to see how the lines really are, how is the SQL Command without the variables, with the values instead.
Sorry if my issue is not explained very well, I'll keep trying to make it clear.
Edit:
I tried:
comando.CommandText = @"
declare @sql nvarchar(max);
set @sql = 'INSERT INTO ' + @curso + '(Data_Local, Deletado) VALUES ( ' + @date + ', 0)';
exec (@sql);
";
comando.Parameters.Add("@date", SqlDbType.NVarChar);
comando.Parameters["@date"].Value = "'" + dateTimePicker2.Value.Date + "'";
New error message:
'The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.'
Edit. Thanks, solved.