I am using an SQL Server stored procedure to put data into a database via C#. When I run the stored procedure from within SQL Server Management Studio it works but when I try to insert the row with C# it fails and I cannot find out why. I initially had trouble getting with the DATETIME
type in the stored procedure but now that works when I use CAST. I have also tried passing the DateTime
values in as strings from C# but this does not work either!?
My SP is below:
ALTER PROCEDURE [dbo].[SaveFlightInfo]
(
@FlightInfoID int,
@AirportFrom varchar(5),
@AirportTo varchar(5),
@TimeDeparture datetime,
@TimeArrival datetime,
@Price float,
@DateAdded datetime,
@Carrier varchar(30),
@Url varchar(300)
)
AS
DECLARE @sql as varchar(1000)
--If the primary key is zero then insert new record
IF ( @FlightInfoID = 0) BEGIN
DECLARE @sql3 as varchar(1000)
SET @sql ='
INSERT INTO '+@AirportFrom+'
(AirportFrom,
AirportTo,
TimeDeparture,
TimeArrival,
Price,
DateAdded,
Carrier,
Url)
VALUES ('''+@AirportFrom+''',
'''+@AirportTo+''',
CAST('''+CAST(@TimeDeparture as varchar(50))+''' as DATETIME),
CAST('''+CAST(@TimeArrival as varchar(50))+''' as DATETIME),
CAST('''+CAST(@Price as varchar(10))+''' as FLOAT),
CAST('''+CAST(@DateAdded as varchar(50))+''' as DATETIME),
'''+@Carrier+''',
'''+@Url+''')
'
END
EXEC(@sql)
My C# code is below:
Database db = DatabaseFactory.CreateDatabase("DBConnectionString");
DbCommand dbCommand = db.GetStoredProcCommand("SaveFlightInfo");
db.AddInParameter(dbCommand, "FlightInfoID", DbType.Int32, flightInfoID);
db.AddInParameter(dbCommand, "AirportFrom", DbType.String, airportFrom);
db.AddInParameter(dbCommand, "AirportTo", DbType.String, airportTo);
db.AddInParameter(dbCommand, "TimeDeparture", DbType.DateTime, timeDeparture);
db.AddInParameter(dbCommand, "TimeArrival", DbType.DateTime, timeArrival);
db.AddInParameter(dbCommand, "Price", DbType.Int16, price);
db.AddInParameter(dbCommand, "DateAdded", DbType.DateTime, dateAdded);
db.AddInParameter(dbCommand, "Carrier", DbType.String, carrier);
db.AddInParameter(dbCommand, "Url", DbType.String, url);
IDataReader dr = db.ExecuteReader(dbCommand);
Also you can use sp_executesql system stored procedure with parameters
DECLARE @sql as varchar(1000)
--If the primary key is zero then insert new record
IF (@FlightInfoID = 0)
BEGIN
DECLARE @sql3 as varchar(1000)
SET @sql = 'INSERT INTO ' + @AirportFrom +
'(AirportFrom, AirportTo, TimeDeparture, TimeArrival, Price, DateAdded, Carrier, Url)
VALUES @AirportFrom, @AirportTo, @TimeDeparture, @TimeArrival, @Price, @DateAdded, @Carrier, @Url)'
END
EXEC sp_executesql @sql, N'@AirportFrom varchar(5),
@AirportTo varchar(5),
@TimeDeparture datetime,
@TimeArrival datetime,
@Price float,
@DateAdded datetime,
@Carrier varchar(30),
@Url varchar(300)', @AirportFrom,
@AirportTo,
@TimeDeparture,
@TimeArrival,
@Price,
@DateAdded,
@Carrier,
@Url
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.