如果我通过 T-SQL 调用我的存储过程:
exec [dbo].[StoredProcedureName] '''Vijay'', ''Rana'', 1, 0'
在 SQL Server Mgmt Studio 中,它工作正常,但是当我从我的应用程序调用它时,它给了我错误
字符串 ''Vijay','Rana',1,0' 后面的非闭合引号。
我在谷歌上搜索并找到了这个EXEC sp_executesql @FinalQuery
,但它不适合我
编辑
我这样称呼它
public virtual IDataReader ImportFirefighter(String query)
{
Database database = DatabaseFactory.CreateDatabase();
DbCommand command = database.GetStoredProcCommand("[StoreProcedureName]");
database.AddInParameter(command, "@query", DbType.String, query);
IDataReader reader = null;
try
{
reader = database.ExecuteReader(command);
}
catch (DbException ex)
{
throw new DataException(ex);
}
return reader;
}
编辑我的完整商店程序
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
--[dbo].[ImportIntoFirefighter] '''Vijay'',''Rana'',''AC'',''AC'',''VOL'',1,0,0,1,1,''NA'','''',''VOL'','''','''',0,'''','''',0,1,1,'''',0&''Vijay21'',''Rana2'',''AC'',''AC'',''VOL'',1,0,0,1,1,''NA'','''',''VOL'','''','''',0,'''','''',0,1,1,'''',0&''Vijay32'',''Rana3'',''AC'',''AC'',''VOL'',1,0,0,1,1,''NA'','''',''VOL'','''','''',0,'''','''',0,1,1,'''',0&''Vijay42'',''Rana4'',''AC'',''AC'',''VOL'',1,0,0,1,1,''NA'','''',''VOL'','''','''',0,'''','''',0,1,1,'''',0'
ALTER PROCEDURE [dbo].[ImportIntoFirefighter]
@query VARCHAR(MAX)
AS
BEGIN
DECLARE @TotalRecord int
DECLARE @loopcount int
DECLARE @TempQueryList TABLE
(
[ID] INT IDENTITY(1,1),
[VALUE] VARCHAR(1000)
)
DECLARE @Result TABLE
(
[iff_id] INT IDENTITY(1,1),
[last_name] VARCHAR(50),
[first_name] VARCHAR(50),
[email] VARCHAR(50),
[mobile_number] VARCHAR(50),
[error] VARCHAR(max)
)
insert into @TempQueryList (VALUE) (
SELECT SUBSTRING('&' + @query + '&', Number + 1,
CHARINDEX('&', '&' + @query + '&', Number + 1) - Number -1)AS VALUE
FROM master..spt_values
WHERE Type = 'P'
AND Number <= LEN('&' + @query + '&') - 1
AND SUBSTRING('&' + @query + '&', Number, 1) = '&' )
Set @TotalRecord = (select count(*) FROM @TempQueryList)
--select * from @TempQueryList
--Loop For Each Repeated Schedule
set @loopcount = 1
WHILE @loopcount <= @TotalRecord
BEGIN
Declare @SingleQuery varchar(1000)
select @SingleQuery = Value from @TempQueryList where id = @loopcount
BEGIN TRY
--print '[AddFirefighter] ' + @SingleQuery
--SELECT 1/0;
--execute (@SingleQuery)
declare @FinalQuery varchar(max)
-- Select @SingleQuery = LEFT(RIGHT(@SingleQuery, len(@SingleQuery)-1),len(@SingleQuery)-2)
set @FinalQuery = '[AddFirefighter] ' + @SingleQuery
print @FinalQuery
EXEC (@FinalQuery)
END TRY
BEGIN CATCH
insert into @Result (last_name,first_name,email,mobile_number,error) values ( '','','','',ERROR_MESSAGE() )
-- Execute the error retrieval routine.
END CATCH
--print @loopcount
SET @loopcount = @loopcount + 1
END
select * from @Result
--execute (@query)
END