无法将类型“IBatisNet.DataMapper.ISqlMapper”隐式转换为“IBatisNet.DataMapper.SqlMapper”。

a_sha 2007-02-05 11:32:18
用Editplus整理编辑完IBatisNet官网的例子Tutorial-1.2.1,使用IBatisNet.DataMapper-bin-1.5.1类库。编译命令如下:
-----------------------------------------------------
csc /r:../bin/IBatisNet.DataMapper.dll /r:../bin/nunit.framework.dll /r:../bin/IBatisNet.Common.dll /target:library /out:iBatisTutorial.Model.dll *.cs
-------------------------------------------------------
错误内容:
------------------------------------------------------

无法将类型“IBatisNet.DataMapper.ISqlMapper”隐式转换为“IBatisNet.DataMapper.SqlMapper”。
存在一个显式转换(是否缺少强制转换?)

---------------------------------------------------------
源码如下:

Helper.cs
---------------------------------------------
using IBatisNet.DataMapper;

namespace iBatisTutorial.Model
{
/// <summary>
/// Base class for Helper objects (*Helper).
/// Provides shared utility methods.
/// </summary>
public abstract class Helper
{
public SqlMapper Mapper ()
{
return IBatisNet.DataMapper.Mapper.Instance ();
}
}
}
------------------------------------
PersonHelper.cs
------------------------------------
using System.Collections;

namespace iBatisTutorial.Model
{
/// <summary>
/// Helper class for Person entities.
/// </summary>
public class PersonHelper : Helper
{
public Person Select (int id)
{
return (Person) Mapper ().QueryForObject ("Select", id);
}

public IList SelectAll ()
{
return Mapper ().QueryForList ("Select", null);
}

public int Insert (Person person)
{
Mapper ().Insert ("Insert", person);
// Insert is designed so that it can return the new key
// but we are not utilizing that feature here
return 1;
}

public int Update (Person person)
{
return Mapper ().Update ("Update", person);
}

public int Delete (int id)
{
return Mapper ().Delete ("Delete", id);
}

}
}
------------------------------------
Helpers.cs
------------------------------------
namespace iBatisTutorial.Model
{
/// <summary>
/// Singleton "controller" for Helper classes.
/// </summary>
public class Helpers
{
private static volatile PersonHelper _PersonHelper = null;

public static PersonHelper Person ()
{
if (_PersonHelper == null)
{
lock (typeof (PersonHelper))
{
if (_PersonHelper == null) // double-check
_PersonHelper = new PersonHelper ();
}
}
return _PersonHelper;
}
}
}
------------------------------------
Person.cs
------------------------------------
using System;

namespace iBatisTutorial.Model
{

/// <summary>
/// Business object representing a Person
/// entity in our problem domain.
/// </summary>
public class Person
{
/*
private string _Property;
public string Property
{
get { return _Property; }
set { _Property = value; }
}
*/

private int _Id;
public int Id
{
get { return _Id; }
set { _Id = value; }
}

private string _FirstName;
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}

private string _LastName;
public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}

private DateTime _BirthDate;
public DateTime BirthDate
{
get { return _BirthDate; }
set { _BirthDate = value; }
}

private double _WeightInKilograms;
public double WeightInKilograms
{
get { return _WeightInKilograms; }
set { _WeightInKilograms = value; }
}

private double _HeightInMeters;
public double HeightInMeters
{
get { return _HeightInMeters; }
set { _HeightInMeters = value; }
}

}
}
------------------------------------
请各位大虾帮帮忙。小弟感激不尽!
------------------------------------
------------------------------------

...全文
777 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
angel8714 2011-06-01
  • 打赏
  • 举报
回复
angel8714 2011-06-01
  • 打赏
  • 举报
回复
http://www.baidu.com去百度一下吧!~
wshuangminlg 2007-02-05
  • 打赏
  • 举报
回复
帮LZ顶

http://user.qzone.qq.com/31767702
a_sha 2007-02-05
  • 打赏
  • 举报
回复
新问题如下:恕小弟愚笨,谁帮我看看,解决后追加分!
--------------------------------------------------------------------------------
“/WebView”应用程序中的服务器错误。
--------------------------------------------------------------------------------

未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 22: private void List_Load ()
行 23: {
行 24: dgList.DataSource = Helpers.Person().SelectAll();
行 25: dgList.DataBind ();
行 26: }


源文件: d:\zyt_web\donet\tutorial\WebView\Forms\Person.aspx.cs 行: 24

堆栈跟踪:


[NullReferenceException: 未将对象引用设置到对象的实例。]
IBatisNet.DataMapper.Configuration.DomSqlMapBuilder.ParseGlobalProperties() +96
IBatisNet.DataMapper.Configuration.DomSqlMapBuilder.Initialize() +258
IBatisNet.DataMapper.Configuration.DomSqlMapBuilder.Build(XmlDocument document, DataSource dataSource, Boolean useConfigFileWatcher, Boolean isCallFromDao) +234

[ConfigurationException:
- The error occurred while Validate SqlMap config.]
IBatisNet.DataMapper.Configuration.DomSqlMapBuilder.Build(XmlDocument document, DataSource dataSource, Boolean useConfigFileWatcher, Boolean isCallFromDao) +288
IBatisNet.DataMapper.Configuration.DomSqlMapBuilder.ConfigureAndWatch(String resource, ConfigureHandler configureDelegate) +209
IBatisNet.DataMapper.Mapper.InitMapper() +71
IBatisNet.DataMapper.Mapper.Instance() +62
iBatisTutorial.Model.Helper.Mapper() +6
iBatisTutorial.Model.PersonHelper.SelectAll() +4
iBatisTutorial.Web.Forms.PersonPage.List_Load() in d:\zyt_web\donet\tutorial\WebView\Forms\Person.aspx.cs:24
iBatisTutorial.Web.Forms.PersonPage.Page_Load(Object sender, EventArgs e) in d:\zyt_web\donet\tutorial\WebView\Forms\Person.aspx.cs:88
System.Web.UI.Control.OnLoad(EventArgs e) +80
System.Web.UI.Control.LoadRecursive() +49
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3745




--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.42; ASP.NET 版本:2.0.50727.42
---------------------------------------------------------------------------------


配置文件:

sqlmap.config 位于根目录
---------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<sqlMapConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="SqlMapConfig.xsd">

  <properties resource="properties.config"/>
 
<settings>
<setting useStatementNamespaces="false"/>
<setting cacheModelsEnabled="true"/>
</settings>

<database>
<provider name="${provider}"/>
<dataSource name="iBatisTutorial" connectionString="${connectionString}"/>
</database>

<sqlMaps>
<sqlMap resource="${root}Map/PersonHelper.xml"/>
</sqlMaps>

</sqlMapConfig>
------------------------------------------------------------------------------------
properties.config 位于根目录
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<settings>
<!-- User application and configured property settings go here.-->
<!-- Example: <add key="settingName" value="settingValue"/> -->
<add key="provider" value="OleDb1.1" />
<add
key="connectionString"
value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data/iBatisTutorial.mdb" />
<add key="root" value="./" />
</settings>
---------------------------------------------------------------------------------

PersonHelper.xml 位于根目录下 Map文件夹

---------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>

<sqlMap
namespace="Person"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="SqlMap.xsd">

<!-- XML "behind" document for the People service class. -->

<alias>
<typeAlias alias="Person" type="iBatisTutorial.Model.Person, iBatisTutorial.Model" />
</alias>

<resultMaps>
<resultMap id="SelectResult" class="Person">
<result property="Id" column="PER_ID" />
<result property="FirstName" column="PER_FIRST_NAME" />
<result property="LastName" column="PER_LAST_NAME" />
<result property="BirthDate" column="PER_BIRTH_DATE" />
<result property="WeightInKilograms" column="PER_WEIGHT_KG" />
<result property="HeightInMeters" column="PER_HEIGHT_M" />
</resultMap>
</resultMaps>

<statements>

<select id="Select" parameterClass="int" resultMap="SelectResult">
select
PER_ID,
PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M
from PERSON
<dynamic prepend="WHERE">
<isParameterPresent>
PER_ID = #value#
</isParameterPresent>
</dynamic>
</select>

<insert id="Insert" parameterClass="Person">
insert into PERSON
(PER_ID, PER_FIRST_NAME, PER_LAST_NAME,
PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M)
values
(#Id#, #FirstName#, #LastName#,
#BirthDate#, #WeightInKilograms#, #HeightInMeters#)
</insert>

<update id="Update" parameterClass="Person">
update PERSON set
PER_FIRST_NAME = #FirstName#,
PER_LAST_NAME = #LastName#,
PER_BIRTH_DATE = #BirthDate#,
PER_WEIGHT_KG = #WeightInKilograms#,
PER_HEIGHT_M = #HeightInMeters#
where PER_ID = #Id#
</update>

<delete id="Delete" parameterClass="int">
delete from PERSON
where PER_ID = #value#
</delete>

</statements>

</sqlMap>
----------------------------------------------------------------------------------

a_sha 2007-02-05
  • 打赏
  • 举报
回复
bestshl(快乐的Coder) ,异常感谢,就是那个问题,已通过编译。
bestshl 2007-02-05
  • 打赏
  • 举报
回复
return IBatisNet.DataMapper.Mapper.Instance ();
应该是这里的问题吧。Mapper 属性是SqlMapper类型的,你查一下 Instance ()返回的类型,应该是一个接口类型ISqlMapper,把这句改成:
return (SqlMapper)IBatisNet.DataMapper.Mapper.Instance ();

110,570

社区成员

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

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

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