declare table 无法得到数据

 不多说,直接看代码。

alter proc Proc_Product_Related
(
@name nvarchar(2000),
@splitStr nvarchar(100) = ' '
)
as
begin
declare @tempTable table(
id
int identity(1,1),
ProductID
int,
[Name] nvarchar(255),
ProductNo
nvarchar(50),
MemberPrice
money,
ThumbnailImg
nvarchar(255),
ProductImg
nvarchar(255)
)
if(@splitStr is Null)
begin
set @splitStr = ' ';
end
begin transaction
insert into @tempTable
select ProductID, [Name], ProductNo, MemberPrice, ThumbnailImg, ProductImg from product p, (select * from dbo.f_splitIncrease(@name, @splitStr)) f
where p.name like + f.one + '%'
if(@@error > 0)
begin
rollback transaction
end
select * from @tempTable
if @@error > 0
rollback
else
commit transaction
end

 

dbo.f_splitIncrease是我自己写的一个方法,用来分词的。
我直接在Sql Server IDE中使用exec
Proc_Product_Related 'the office seasons'可以得到数据集,
但是我在程序中使用DataAdapter.Fill()却无法得到数据。
于是我将declare table去掉,直接使用
select ProductID, [Name], ProductNo, MemberPrice, ThumbnailImg, ProductImg from product p, (select * from dbo.f_splitIncrease(@name, @splitStr)) f 
where p.name like + f.one + '%'
是可以得到数据的。
问题就在于我加了declare table后,无法在程序端得到数据。

程序端代码如下:
public DataTable GetRelatedProduct(string name, string splitStr)
{
string strSql = "Proc_Product_Related";
DataTable dataTable
= new DataTable();

OleDbParameter[] parameters
={
new OleDbParameter("@name", OleDbType.VarChar, 1000),
new OleDbParameter("@splitStr", OleDbType.VarChar, 100)
};
parameters[
0].Value = name;
if (!string.IsNullOrEmpty(splitStr))
{
parameters[
1].Value = splitStr;
}
OleDbCommand command
= new OleDbCommand();
command.CommandText
= strSql;
command.CommandType
= CommandType.StoredProcedure;
command.Connection
= new OleDbConnection(ConnectionString.GetConnectionString());
command.Parameters.AddRange(parameters);
using (OleDbDataAdapter oledbda = new OleDbDataAdapter(command))
{
oledbda.Fill(dataTable);
}
return dataTable;
}
只是测试代码,还希望不要针对代码的优劣。
天涯尽头无女友
浏览 644回答 2
2回答

米脂

刚才看错了回答错了,看你的存储过程和程序基本没有问题, 如果直接在Sql Server IDE中使用exec Proc_Product_Related 'the office seasons' 可以得到数据集那么程序肯定也可以, 应该是你程序调用出现问题了
打开App,查看更多内容
随时随地看视频慕课网APP