Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

配置iBatis.NET DataMapper

Wednesday, April 18, 2007

一. 在iBatis.Net的官方网站下载iBatis.NET DataMapper 1.6.1。这个是今年4月6号的最新版本,也是快一年以来的第一次更新。

下载的Release的版本解压缩以后看起来很杂乱,dll、xml和xsd等一大堆。

二. 为了能够在Visual中编写映射文件和配置文件时使用智能提示和校验,首先把provider.xsd、SqlMap.xsd和SqlMapConfig.xsd拷贝到[VS安装目录]\Xml\Schemas,这个路径是相对于VS 2005的,其他版本参看文档4.2.4节。

注:iBatis.NET的文档是需要单独下载的,不在Release包里面,下面提到的文档均是1.6.1版本的文档。

三. 使用iBatis.NET之前需要在VS的工程中添加对IBatisNet.DataMapper.dll引用,添加它的同时,它会自动将IBatisNet.Common.dll和Castle.DynamicProxy.dll添加到工程的bin目录下。

四. 编写SqlMap.config文件,并将其置于跟目录下,一般是Web.config(WEB应用程序)或者App.config(winform)所在的文件夹。下面是一个简单示例,修改自iBatis.NET文档。

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <sqlMapConfig xmlns="http://ibatis.apache.org/dataMapper"
   3: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
   4:   <properties resource="properties.config"/>
   5:   <settings>
   6:     <setting useStatementNamespaces="true"/>
   7:     <setting cacheModelsEnabled="true"/>
   8:     <setting validateSqlMap="false"/>
   9:   </settings>
  10:  
  11:   <database>
  12:     <provider name="sqlServer2.0"/>
  13:     <dataSource name="iBatisExt"  connectionString="user id=sa;password=sa;data source=10.14.91.244;database=northwind;"/>
  14:   </database>
  15:  
  16:   <sqlMaps>
  17:     <sqlMap resource="${root}Maps/Categories.xml"/>
  18:   </sqlMaps>
  19: </sqlMapConfig>

其中首先指定Properties文件的位置,此处的Properties.config与SqlMap.config位于相同位置,Properties文件一般用来定义“名称-值”对,例如下面使用到的${root}就是在Properties.config中定义的,这里使用的Properties文件如下,很简单:

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <settings>
   3:   <add key="root" value="./" />
   4: </settings>

Setting用于给定ibatis.net的一些属性,例如useStatementNamespaces是是否允许使用map文件中的命名空间,文档中的示例为false,注意把它改成true,因为下面的程序使用了map文件中的命名空间。

database用于配置映射的数据源,Provider我改成了sqlServer2.0。

五. 把定义数据源的providers.config拷贝到与SqlMap.config相同的目录下,打开providers.config,修改其中的

   1: <provider
   2:     name="sqlServer2.0"
   3:     enabled="false"

   1: <provider
   2:     name="sqlServer2.0"
   3:     enabled="true"

这里默认情况下,也就是从Release文件夹拷贝过来的providers.config是不打开sqlServer2.0,这个地方害我修改了半天。

六.  编写映射文件、实体类就完成了iBatis的配置。如果你不想自己编写映射文件、实体类,Go on

七.  我找到了一个iBatis.NET的CodeSmith模板,名字叫做IBatisNetGen,个人觉得很好用,它直接生成了映射文件、实体类、DAO接口和DAO层。注意在使用时制定他们的命名空间,免得生成后自己去修改。

八.  使用上述模板注意有一个地方需要修改,就是在把DAO模板里面需要把SqlMapperFmt属性改为你自己的Mapper类,如本文中使用iBatis.NET默认的Mapper.Instance()。

1

如果使用到了多数据库,要用不同的SqlMap文件,并编写不同的Mapper类,如这里完成了一个使用AnotherSqlMap.config文件的AnotherSqlMapper类(参考文档Example 4.4),注意红色的行与iBatis.net中文档4.4.1.1节有一点区别,4.4.1.1节的语句不能直接用于替换文档中的Example 4.4的对应语句,必须做红色标示的修改。奇怪一下文档为什么不前后一致呢?

   1: using IBatisNet.Common.Utilities;
   2: using IBatisNet.DataMapper;
   3: using IBatisNet.DataMapper.Configuration;
   4:  
   5: namespace Demo
   6: {
   7:     public class AnotherSqlMapper
   8:     {
   9:         private static volatile ISqlMapper _mapper = null;
  10:  
  11:         protected static void Configure(object obj)
  12:         {
  13:             _mapper = null;
  14:         }
  15:  
  16:         protected static void InitMapper()
  17:         {
  18:             ConfigureHandler handler = new ConfigureHandler(Configure);
  19:             DomSqlMapBuilder builder = new DomSqlMapBuilder();
  20:             _mapper = builder.ConfigureAndWatch("AnotherSqlMap.config",handler);
  21:         }
  22:  
  23:         public static ISqlMapper Instance()
  24:         {
  25:             if (_mapper == null)
  26:             {
  27:                 lock (typeof(SqlMapper))
  28:                 {
  29:                     if (_mapper == null)
  30:                     {
  31:                         InitMapper();
  32:                     }
  33:                 }
  34:             }
  35:             return _mapper;
  36:         }
  37:  
  38:         public static ISqlMapper Get()
  39:         {
  40:             return Instance();
  41:         }
  42:     }
  43: }

九 配置完成,以Northwind中的Categories表为例,实体类Categories。C#获取全部记录的调用语句。

   1: ICategoriesDao catDao=new CategoriesDao();
   2:         IList<Categories> cats = catDao.FindAll();
   3:         gvCategories.DataSource = cats;
   4:         gvCategories.DataBind();

......

[阅读全文]

利用PI-SDK读取和写入snapshot或者archive的示例

Thursday, March 29, 2007

一个很简单的程序。实现PI-SDK读取和写入snapshot或者archive。以测试读取和写入int和float的数据,bool和string类型没有测试,估计需要修改,但是原理是一样的。界面如图,从上到下,从左到右操作就可以了。

sdkdemo

 

很简单的版权说明,呵呵

×××××××××××××××××××××××××××××××××××××××××××××

利用PI-SDK读取和写入snapshot或者archive的示例

(c) 皮生 (http://sacranto.blogspot.com) 2007

最后更新 - 3/29/2007 20:55

These code are provided AS IS. You can copy it, use it, and distribute it as long as this statement are not removed.

×××××××××××××××××××××××××××××××××××××××××××××

 

下载地址:这里 或者右边共享文件里面的PISDKDemo.rar

......

[阅读全文]

PI-SDK错误-2147220424的解决方法

最近有不少人通过IM问我关于PI-SDK开发的问题,正好我也好久没有看了,把原来学习的时候写的一个利用PI-SDK读取snapshot和archive的例子拿出来重温了一下。调试运行结果竟然不通过,记得原来这个例子是OK的,看了以下错误出在Server对象的Open方法上,这个方法是用于连接数据库的。仔细看了SDK的文档,连接字符串的写法、用户名和密码均正确,但是就是连不上,每运行到这里就跑出异常-2147220424。异常提示“ServerID returned from server does not match that currently configured on this machine. The server at the path for the opened handle may have changed. ”我理解的大意是服务器的句柄变化,返回的ServerID和机器的当前配置不同。记起前几天把实验用的PI数据库重装过,难道是这个原因不成。但是怎么解决呢?文档和Google均没有找到好的答案。

首先,利用PI-API连接数据库,在权限允许的情况下,读写均没有问题,虽然PI-API和PI-SDK使用两套不同的登录方式,但是这样至少说明我的机器和数据库的连接是没有问题的。

接着,按照Troubleshooting PI client connection problems上的方法使用AboutPI-SDK对话框测试登录,结果仍然可以。但是回到程序就是不行。

最后,另外写了一个程序重新连接数据库,仍然报相同的错误,可能是因为Com组件的信息是保存在注册表,登录时仍然使用了以前保存的信息。既然猜想登录信息保存在注册表,那么直接去注册表中更改一下信息不是就好了,可惜不知道改哪里。

正在这是灵光一现,想着干嘛这么复杂。直接利用AboutPI-SDK对话框新建一个连接,再把原来的那个连接给删掉不是应该就可以删除注册表中的原来连接信息了吗?搞定。这个时候相同的程序,open方法使用相同的语句登录上了数据库。

估计这个是一个SDK的小BUG,如果以前连接过PI数据库、在PI数据库重装以后,必须更新本机的SDK在注册表中信息,更新方法很简单。使用AboutPI-SDK对话框新建一个连接,再把原来的那个连接给删掉。

......

[阅读全文]

Tip On Distributing PI-ActiveView Automated ActiveX Installation

Wednesday, December 6, 2006

In previous environment, I always use PI-ActiveView in the machines which has already installed PI-ActiveView and PI-ProcessBook, So I had never care distributing problem until I want to navigate the website use PI-ActiveView to see PDI files embed in it. the result is web page can not display it.

I remember someone ever told that this problem can be solved by automated ActiveX installation because of in the fact it's a ActiveX Control. Open the document of it is my choice and select the setting up automated web installation. Follow the instruction of it. so easy, isn't it?

Open the IE (not firefox for using ActiveX) and modify the security option about "unsigned ActiveX control" to "prompt" because ActiveX file we made has not signed. this is a temporary solution. I suggest u modify it back to prevent malicious ie plugin. This Time I got the ActiveX installation prompt, However when I click sure to install it, there is no response just displaying nothing.

All process seems right following the document. Finally I spent 2 hours to fix the problem and this is the tip what I want to give. Because I placed .cab files in the sub-directory of home directory,  Pay attention to this procedure of cabwizard

acview

Remember to add sub-folder that your cab file placed in to the Full path. What caused me waste 2 hours is here.  In fact, the notion above is clear but I do not carefully read it.

......

[阅读全文]

About PI-API Error -10401

Thursday, November 30, 2006

Today I wanted to insert data to PI by using a .net class which boxed PI-API I created it before. I used function pisn_putsnapshotx, but it doesn't work with error code -10401.

In PI_API Documents, there is no comment about this error code. so I had to found the reason by myself. I used default point "sinusoid" as test point and try to insert data into it. it works Fine. Then I compare tag property of "sinusoid" and my problem tag, I notice defference on DataAccess.

sinusoid: o:rw g:rw w:rw Problem tag: o:rw g:r w:r

OK, I guess this is the reason, I chaged Problem tag's property to o:rw g:rw w:rw, then I use pisn_putsnapshotx function to insert data, works well.

So error code -10401 is a access level problem.

there is two way to solve it now.

1. As said above, change property to o:rw g:rw w:rw.

2. Add login information in your code by using pilg_login or piut_login

PS: I can't use pilg_login because no entry point in piapi32.dll when I P/Invoke it. I do not know why. But I found piut_login is always OK.

more article about PI development visit My Former Chinese Techinical Blog!

......

[阅读全文]

在windows 2003中使用PI Activeview

Wednesday, November 29, 2006

今天在搞把PDI文件用PI Activeview嵌到网页里面去,搞了半天都不行,使用同样的方法在其他的电脑上面都是OK的,而且我的电脑浏览其他主机上面嵌有PDI的网页也不能显示。分析原因,其他的电脑都是XP或者2000的系统,只有我的是2003,难道是操作系统的原因,搜索了一下,果然在OSISOFT的支持网站上面有这样一片文章Using PI ActiveView and Windows Server 2003 Copy一下解决方法粘在这里,很简单就不翻译了

1. In IIS Manager, right-click on the level of hierarchy for which to change the setting and select "Properties." 2. Go to the tab "HTTP Headers" and click the "MIME Types..." button.

3. Select New and enter the following information for Extension: .PDI for MIME type: application/octet-stream

4. You should now see the .PDI under the Registered MIME Types list.

5. Click OK.

6. Stop and restart your IIS service for the changes to take effect.

......

[阅读全文]