首页 新闻 会员 周边

declare table 无法得到数据

0
悬赏园豆:15 [已解决问题] 解决于 2010-09-01 16:17

 不多说,直接看代码。

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;
}
只是测试代码,还希望不要针对代码的优劣。
问题补充: 问题找到了!不是因为Oledb和Sql的问题, 使用OledbConnection时,连接字符串必须有Provider这个关键字, 但是加了这个关键字后,我的Sp就无法得到数据了, 我改成SqlServer,然后在连接字符串中去掉那个关键字就OK了. 很郁闷吧.
双调的主页 双调 | 初学一级 | 园豆:7
提问于:2010-09-01 14:14
< >
分享
最佳答案
0

你只是插入了表变量,而没有查询,最后加一句select * from @tempTable 就可以了。

收获园豆:15
清海扬波 | 小虾三级 |园豆:825 | 2010-09-01 14:49
其他回答(1)
0

刚才看错了回答错了,看你的存储过程和程序基本没有问题,

如果直接在Sql Server IDE中使用exec Proc_Product_Related 'the office seasons'

可以得到数据集那么程序肯定也可以,
 
应该是你程序调用出现问题了
jowo | 园豆:2834 (老鸟四级) | 2010-09-01 15:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册