首页 新闻 赞助 找找看

企业库ExecuteSqlStringAccessor直接返回一个业务对象数据

0
悬赏园豆:10 [已解决问题] 解决于 2012-04-01 20:02

请教哪位能帮忙改写下面一段代码,转换成泛型,要传送不同的实体对象。

public static China.Model.About GetModel(string strSQL)
{
Database db = DatabaseFactory.CreateDatabase();
var accessor=db.ExecuteSqlStringAccessor<China.Model.About>(string);
return accessor.FirstOrDefault<China.Model.About>();
}

以上代码直如指明了要返回的实体类型,编译时没有问题,我改写之后的代码如下,提示“找不到类型或名称空间T,(是否缺少using引用或程序集引用)”,

public static T GetModel(string strSQL)
{
Database db = DatabaseFactory.CreateDatabase();
var accessor = db.ExecuteSqlStringAccessor<T>(strSQL);
return accessor.FirstOrDefault<T>();
}

望高手能指教一二,不甚感激。

智伟的主页 智伟 | 菜鸟二级 | 园豆:266
提问于:2012-03-15 00:01
< >
分享
最佳答案
1

回答博主,我表示泛型学的不是很好,但是对于博主的问题,无非是要返回不同的数据类型的数据,而在需要的时候转化为对应的类型,可以用object吧,自己写了一把测试,不知道对博主有用没。

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace testMany
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 //获取返回的对象
13 object o1, o2, o3;
14 o1 = ReturnData1();
15 o2 = ReturnData2();
16 o3 = ReturnData3();
17
18 //在需要的时候强制转换为需要的类型
19 Console.WriteLine("第一个:{0}",(o1 as Person1).Name1);
20 Console.WriteLine("第二个:{0}",(o2 as Person2).Name2);
21 Console.WriteLine("第三个:{0}",(o3 as Person3).Name3);
22
23 Console.ReadKey();
24 }
25
26 //模拟数据库返回不同对象
27 static object ReturnData1()
28 {
29 return new Person1 { Name1 = "1" };
30 }
31 static object ReturnData2()
32 {
33 return new Person2 { Name2 = "2" };
34 }
35 static object ReturnData3()
36 {
37 return new Person3 { Name3 = "3" };
38 }
39 }
40
41 //定义不同的数据类型
42 public class Person1
43 {
44 public string Name1 { get; set; }
45 }
46 public class Person2
47 {
48 public string Name2 { get; set; }
49 }
50 public class Person3
51 {
52 public string Name3 { get; set; }
53 }
54 }
收获园豆:10
WaitingSky | 菜鸟二级 |园豆:300 | 2012-03-15 09:03

不是我想要结果,不过还是挺感谢你的,谢谢。

智伟 | 园豆:266 (菜鸟二级) | 2012-03-15 13:10

@智伟: 今天在看wp7的时候无意间看到了关于泛型的东西,试着改了一下,是实现了。测试代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testMany
{
class Program
{
static void Main(string[] args)
{
//获取返回的对象
Person1 p1 = ReturnData<Person1>(1);
Person2 p2 = ReturnData<Person2>(2);
Person3 p3 = ReturnData<Person3>(3);

//在需要的时候强制转换为需要的类型
Console.WriteLine("第一个:{0}",p1.Name1);
Console.WriteLine("第二个:{0}",p2.Name2);
Console.WriteLine("第三个:{0}",p3.Name3);

Console.ReadKey();
}

//模拟数据库返回不同对象
static T ReturnData<T>(int i)
{
object test;
switch (i)
{
case 1:
test = new Person1 { Name1="1"};
return (T)test;
case 2:
test = new Person2 { Name2="2"};
return (T)test;
case 3:
test = new Person3 { Name3="3"};
return (T)test;
default:
return default(T);
}
}
}

//定义不同的数据类型
public class Person1
{
public string Name1 { get; set; }
}
public class Person2
{
public string Name2 { get; set; }
}
public class Person3
{
public string Name3 { get; set; }
}
}

在你的代码中,public static T GetModel(string strSQL)这个要在方法名后写明<T>,所以应该为:public static T GetModel<T>(string strSQL)。

WaitingSky | 园豆:300 (菜鸟二级) | 2012-03-18 13:04

@WaitingSky: 非常感谢你的解答,功能是实现了,不过后面还要加上泛型约束 where T:new(),非常感谢。

智伟 | 园豆:266 (菜鸟二级) | 2012-04-01 19:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册