public int Add(Model.Categories model) 这方法要怎么用

vboy28 2009-07-13 02:37:30
菜鸟求助;
问题有几个第一: 写一个添加方法老提示
我编译器错误消息: CS0029: 无法将类型“int”隐式转换为“bool”

源错误:



行 25: BLL.Categories bllca = new BLL.Categories();
行 26: //SQLServerDAL.Categories dalca = new SQLServerDAL.Categories();
行 27: if (bllca.Add(model))
行 28: Response.Write("添加成功");
行 29:





protected void btpost_Click(object sender, EventArgs e)
{
Model.Categories model = new Model.Categories();
model.CategoryName = bt2.Text;


BLL.Categories bllca = new BLL.Categories();

if (bllca.Add(model))



Response.Write("添加成功");



}


这个BLL层的代码:
public int Add(Model.Categories model)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into [Categories](");
strSql.Append("[CategoryName],[Description],[Picture])");
strSql.Append(" values (");
strSql.Append("@CategoryName,@Description,@Picture)");
strSql.Append(";select @@Identity");
SqlParameter[] parameters = {
new SqlParameter("@CategoryName", SqlDbType.NVarChar,15),
new SqlParameter("@Description", SqlDbType.NText),
new SqlParameter("@Picture", SqlDbType.Image)
};
parameters[0].Value = model.CategoryName;

if (model.Description != null)
parameters[1].Value = model.Description;
else
parameters[1].Value = DBNull.Value;

parameters[2].Value = model.Picture;
return SqlHelper.GetInt(SqlHelper.GetSingle(SqlHelper.LocalSqlServer, strSql.ToString(), parameters));
}


第二个问题:
如果把这个方法中间的反回类型改了 public int Add(Model.Categories model) 中间的INT 改为public Bool Add(Model.Categories model) 要怎么写。。。


第三个问题是:有没有什么书可以很好介绍一下这个类的方法反回类型不同,怎么调用,带参数和不带参数的。。使用方法。。。非常感谢



还有一个问题就是: private IList<Model.Categories> GetList(DataSet ds)
{
List<Model.Categories> l = new List<Model.Categories>();
foreach (DataRow r in ds.Tables[0].Rows)
{
l.Add(GetModel(r));
}
return l;
}
我想用这个方法写一个邦定到DATALIST上把数据显示出来要怎么写。。。。还是有更好的方法谢谢
...全文
296 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
1q23111111qqqq 2009-07-25
  • 打赏
  • 举报
回复
小的转大的

string最大
byte最小

有些类型是不能转换的 bool 类 接口 等。。。
vboy28 2009-07-25
  • 打赏
  • 举报
回复
有没有那位大鸟有关数据类型转换的方面的东东?
vboy28 2009-07-25
  • 打赏
  • 举报
回复
谢谢大家的意见帮大忙
现在可以做一些比较简单的功能啦。。

特别感谢
(jsoner) ; hzzasdf; hbxtlhx. 这三位。


北京的雾霾天 2009-07-14
  • 打赏
  • 举报
回复
1:if (bllca.Add(model))
----------------------------
要求Add一定是一个Bool才可以这样调用,这是C#,不是C++。
但是你可以写成,如if (bllca.Add(model)>-1)之类的表达式。

2:类似如下吧:
public int Add(Model.Categories model)
{
int result=-1;
result = SqlHelper.GetInt(SqlHelper.GetSingle(SqlHelper.LocalSqlServer, strSql.ToString(), parameters));
return result!=-1;

3:DATALIST应该支持了列表。没有环境不好说。
  • 打赏
  • 举报
回复
第二个问题:
如果把这个方法中间的反回类型改了 public int Add(Model.Categories model) 中间的INT 改为public Bool Add(Model.Categories model) 要怎么写。。。

最后一句
return SqlHelper.GetInt(SqlHelper.GetSingle(SqlHelper.LocalSqlServer, strSql.ToString(), parameters));
改成

return SqlHelper.GetInt(SqlHelper.GetSingle(SqlHelper.LocalSqlServer, strSql.ToString(), parameters)) > 0 ? true : false;
jsoner 2009-07-14
  • 打赏
  • 举报
回复
行 27: if (bllca.Add(model))
行 28: Response.Write("添加成功");

你的Add(model)的返回值是INT型,而IF里,需要的是bool型.
所以转不成功了.
  • 打赏
  • 举报
回复
bllca.Add(model) 这个方法返回的是个整数,而if语句判断条件需要是布尔式,所以将 if (bllca.Add(model)) 改成 if (bllca.Add(model) > 0) 即可
peterb 2009-07-14
  • 打赏
  • 举报
回复
行 27: if (bllca.Add(model)) //这里错误,看看它已什么数值当作添加不成功标记,一般-1之类
行 28: Response.Write("添加成功");

看方法应该是返回新增记录的ID值了,书籍一般都有介绍这方面

111,115

社区成员

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

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

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