这样的问题怎么实现?

seeme1014 2007-01-15 11:51:51
我的数据表有个字段是记录一个CheckBoxList的值的,类型是char(15)型,我在记录到数据库的时候做了处理,都已'1,2,3,4'存入,1,2,3,4 分别是每个CheckBoxList.Item[i].value,也是数据库另外一个表的相应ID号(int),代表一个类别的编号.然后我要在另一个页面上把这些值(1,2,3,4)用那个有ID字段的表中name(char型)字段还原显示出来,有没有一个select语句直接实现? select ID,name where ID in '1,2,3,4'?我记得 有 delete * from tablename where ID in ****,如果没有,有什么替代的办法吗?


---------------------------------------表现层----------------------------------------
***.text=DisplayType('1,2,3')
public string DisplayType(string getchar)
{string Type;
GF Dis = new GF();//GF是封装DisplayType方法的类
SqlDataReader Kind = Dis.DisplayType(getchar);
if (Kind.Read())
{Type="";
while(Kind.Read())
{
Type+=" "+Kind["CorpKindName"].ToString();
}
}
else
Type="";
return Type;
}
-----------------------------------------业务层----------------------------------------
public SqlDataReader DisplayType(string CorpKind)
{
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("GetCorpKind",myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
SqlParameter parameterCorpKind = new SqlParameter("@CorpKind",SqlDbType.Char,15);
parameterCorpKind.Value= CorpKind;
myCommand.Parameters.Add(parameterCorpKind);
myConnection.Open();
SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
return result;

}
----------------------------------------数据层----------------------------------------------
CREATE PROCEDURE GetCorpKind
@CorpKind char(15)
AS
Select CorpKindName
FROM CorpKindType
WHERE CorpKindID in (@CorpKind)
GO
这是我想的 我用查询分析器的时候exec GetCorpKind 1 可以的 用exec GetCorpKind 1,2 说参数太多,用上面的code执行的时候,没有返回值,就是***.text=""最后,但事跟踪器中有执行 exec GetCorpKind @CorpKind = '2 ',然后我按照exec GetCorpKind @CorpKind = '2,3',系统报错服务器: 消息 245,级别 16,状态 1,过程 GetCorpKind,行 4
将 varchar 值 '2,3 ' 转换为数据类型为 tinyint 的列时发生语法错误。
...全文
113 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hyc_music1981 2007-01-15
  • 打赏
  • 举报
回复
楼上方法不错
akuzou 2007-01-15
  • 打赏
  • 举报
回复
改成这样试试:
CREATE PROCEDURE GetCorpKind
@CorpKind char(15)
AS
exec('Select CorpKindName
FROM CorpKindType
WHERE CorpKindID in ('+@CorpKind+')')
GO
seeme1014 2007-01-15
  • 打赏
  • 举报
回复
可是有办法改进吗?一定要把那些参数拆开,一个一个传到存储过程中检测吗?
leo_lesley 2007-01-15
  • 打赏
  • 举报
回复
exec GetCorpKind 1,2 这句话你带了两个参数 你的proc 只定义了一个参数当然说参数太多了

这个列是不是int类型啊! 你的参数是char的 当然报错了

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧