我有一个更新表中的行的存储过程。我正在使用 C# 将所有参数传递到存储过程中,如下所示:
public int editFestival(String festId, String festNaam, String festLocatie, String festDatum,
String festDuur, String festEindDatum, String festUrl)
{
sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["project"].ConnectionString);
sqlCommand = new SqlCommand("EditFestival", sqlConnection);
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.Add(new SqlParameter("@festId", Convert.ToInt32(festId)));
sqlCommand.Parameters.Add(new SqlParameter("@festNaam", festNaam));
sqlCommand.Parameters.Add(new SqlParameter("@festLocatie", festLocatie));
sqlCommand.Parameters.Add(new SqlParameter("@festDatum", Convert.ToDateTime(festDatum)));
//Also tried -> , SqlDbType.date)).Value = festDatum
sqlCommand.Parameters.Add(new SqlParameter("@festDuur", Convert.ToInt32(festDuur)));
sqlCommand.Parameters.Add(new SqlParameter("@festEindDatum", Convert.ToDateTime(festEindDatum)));
sqlCommand.Parameters.Add(new SqlParameter("@festUrl", festUrl));
sqlConnection.Open();
sqlTransaction = sqlConnection.BeginTransaction();
sqlCommand.Transaction = sqlTransaction;
rows = sqlCommand.ExecuteNonQuery();
sqlTransaction.Commit();
return rows;
}
尽管字符串 festDatum(在英语中表示 festDate)的格式正确(yyyy-MM-dd),但它不被存储过程接受。
当我使用自己给定的变量执行存储过程时,它会生成以下内容:
USE [groep2_festivals]
GO
DECLARE @return_value int
EXEC @return_value = [dbo].[EditFestival]
@festId = 1,
@festNaam = N'Rock Werchter',
@festLocatie = N'Werchter - Belge',
@festDatum = 2013-07-01, --This is where it says that there's a syntax error near '-'
@festDuur = 4,
@festEindDatum = 2013-07-04,
@festUrl = N'www.rockwerchter.be'
SELECT 'Return Value' = @return_value
GO
当我用单引号手动将两个日期括起来时,它工作正常。我究竟做错了什么?还是像用单引号将更新语句中的参数括起来一样简单?
编辑:这是原始存储过程(应该早点发布)
USE [groep2_festivals]
GO
/****** Object: StoredProcedure [dbo].[EditFestival] Script Date: 10/05/2013 12:44:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Robbie Vercammen
-- Create date: 10/05/2013
-- Description: Veranderd de gegevens van een bepaald festival
-- =============================================
CREATE PROCEDURE [dbo].[EditFestival]
(
@festId int,
@festNaam nvarchar(255),
@festLocatie nvarchar(255),
@festDatum date,
@festDuur int,
@festEindDatum date,
@festUrl nvarchar(255)
)
AS
BEGIN
SET NOCOUNT ON;
UPDATE festivals
SET fest_naam = @festNaam, fest_locatie = @festLocatie, fest_datum = @festDatum,
fest_duur = @festDuur, fest_einddatum = @festEindDatum, fest_url = @festUrl
WHERE fest_id = @festid
END
编辑:我正在尝试从管理工作室中执行存储过程
生成的代码已经在上面发布了;)执行时它只是说
消息 102,级别 15,状态 1,第 8 行
'-' 附近的语法不正确。