vs2005C#提示 “无法将类型“object”隐式转换为“bool”。存在一个显式转换(是否缺少强制转换?)”

cbting 2008-10-01 03:24:12
代码我大致贴上来
类DataAccess
public class DataAccess
{
public static Object ExecutesqlSealar(string Connectionstring,string sql,SqlParameter[] parameters)
{
Object result = null ;
//构建连接对象
SqlConnection connection = new SqlConnection(Connectionstring);
//构建命令对象
SqlCommand command = new SqlCommand(sql ,connection);
//添加参数
foreach (SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
using (connection)
try
{
connection.Open();
result = command.ExecuteScalar();
}
catch (Exception ex)
{

Console.WriteLine(ex .Message);
}
return result;
}
}

类Class1
public class Class1
{
public bool Chexuser(string username, string pwd)
{

string Connectionstring = @"server=BE903443E99043B\SQLEXPRESS;Initial Catalog=NewsDB;Integrated Security=True";
string sql = "select Useridname from yonghutable where Useridname=@Useridname and pwd=@pwd";

SqlParameter patername = new SqlParameter("@Useridname",SqlDbType.VarChar);
SqlParameter paterpwd = new SqlParameter("@pwd",SqlDbType.VarChar);

patername.Value = username;
paterpwd.Value = pwd;
SqlParameter[] parameters = new SqlParameter[] { patername ,paterpwd};
object result=DataAccess.ExecutesqlSealar(Connectionstring ,sql ,parameters);
return result;
}
}
执行的是后提示“无法将类型“object”隐式转换为“bool”。存在一个显式转换(是否缺少强制转换?)”

...全文
3083 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
seveninfo 2010-08-16
  • 打赏
  • 举报
回复
学习下
888228 2008-10-14
  • 打赏
  • 举报
回复
封装-拆箱 系统开销大,谨慎使用
cbting 2008-10-14
  • 打赏
  • 举报
回复
谢谢了,先欠着吧,俺是农民工!
cbting 2008-10-03
  • 打赏
  • 举报
回复
谢谢各位的帮忙。终于调试好了。
wartim 2008-10-03
  • 打赏
  • 举报
回复
不如返回类型就是bool ,返回object这里看没什么意义
zbking 2008-10-03
  • 打赏
  • 举报
回复
bool result=DataAccess.ExecutesqlSealar(Connectionstring ,sql ,parameters);
return result;
jelin 2008-10-03
  • 打赏
  • 举报
回复
类Class1 中 return result; 改成 return (bool)result; 即可

解决完毕,记得结贴给分~~~~~~
andyliu1938 2008-10-01
  • 打赏
  • 举报
回复
你返回的结果Result是Object类型,需要拆箱,
object result=DataAccess.ExecutesqlSealar(Connectionstring ,sql ,parameters);
return (bool)result;
NowtAngell 2008-10-01
  • 打赏
  • 举报
回复
你直接定义BOOL 类型不可以么?
CONVERT.TOBOOL();
性手估计的 没去看开发工具..试下.
wuyi8808 2008-10-01
  • 打赏
  • 举报
回复
return (bool)result;
hackztx 2008-10-01
  • 打赏
  • 举报
回复
return (bool)DataAccess.ExecutesqlSealar(Connectionstring ,sql ,parameters);
winner2050 2008-10-01
  • 打赏
  • 举报
回复
object result=DataAccess.ExecutesqlSealar(Connectionstring ,sql ,parameters);
return (bool)result;

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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