执行sql语句函数(query表示sql查询语句,tablename也可以理解为表名,但实际的作用是映射到相对应的实体类):
1
private
IList ExecuteSQL(
string
query,
string
tablename)
2 {
3 IList result = new ArrayList();
4 ISessionFactoryImplementor s = (ISessionFactoryImplementor)_cfg.BuildSessionFactory();
5 IDbCommand cmd = s.ConnectionProvider.Driver.CreateCommand();
6 cmd.CommandText = query;
7 IDbConnection conn = s.OpenConnection();
8 try
9 {
10 cmd.Connection = conn;
11 IDataReader rs = cmd.ExecuteReader();
12 int j = 0 ;
13 while ( rs.Read() )
14 {
15 int fieldCount = rs.FieldCount;
16 object[] values = new Object[ fieldCount ];
17 for ( int i = 0; i < fieldCount; i ++ )
18 values[i] = rs.GetValue(i);
19 chooseEntity(result,tablename,values);
20 }
21 }
22 finally
23 {
24 s.CloseConnection(conn);
25 }
26 return result;
27 }
2 {
3 IList result = new ArrayList();
4 ISessionFactoryImplementor s = (ISessionFactoryImplementor)_cfg.BuildSessionFactory();
5 IDbCommand cmd = s.ConnectionProvider.Driver.CreateCommand();
6 cmd.CommandText = query;
7 IDbConnection conn = s.OpenConnection();
8 try
9 {
10 cmd.Connection = conn;
11 IDataReader rs = cmd.ExecuteReader();
12 int j = 0 ;
13 while ( rs.Read() )
14 {
15 int fieldCount = rs.FieldCount;
16 object[] values = new Object[ fieldCount ];
17 for ( int i = 0; i < fieldCount; i ++ )
18 values[i] = rs.GetValue(i);
19 chooseEntity(result,tablename,values);
20 }
21 }
22 finally
23 {
24 s.CloseConnection(conn);
25 }
26 return result;
27 }
在以上的代码中,函数chooseEntity就是对查询的结的转换,如下:
1
private
void
chooseEntity(IList result,
string
tablename,
object
[] values)
2 {
3 switch(tablename)
4 {
5 case "tbl_b_AcceptConiteminfo":
6 AcceptConIteminfo accConitem = new AcceptConIteminfo(values);
7 result.Add( accConitem );
8 break;
9 }
10 }
2 {
3 switch(tablename)
4 {
5 case "tbl_b_AcceptConiteminfo":
6 AcceptConIteminfo accConitem = new AcceptConIteminfo(values);
7 result.Add( accConitem );
8 break;
9 }
10 }
在实际类的构造函数中,将数组values的值赋值到实体类中,如下:
实体类实例
1public class AcceptConIteminfo
2 {
3
4 private string _OrigItemId = "";
5 private string _ItemName = "";
6 private string _ItemLoc = "";
7 private string _IsnewItem = "";
8 public AcceptConIteminfo(object[] values)
9 {
10 //
11 // TODO: 在此处添加构造函数逻辑
12 //
13 _OrigItemId = values[0].ToString() ;
14 _ItemName = values[1].ToString() ;
15 _ItemLoc = values[2].ToString() ;
16 _IsnewItem = values[3].ToString() ;
17 }
18
19
20 public string OrigItemId
21 {
22 get{return _OrigItemId;}
23 set{_OrigItemId= value ;}
24 }
25
26 public string ItemName
27 {
28 get{return _ItemName;}
29 set{_ItemName= value ;}
30 }
31
32 public string ItemLoc
33 {
34 get{return _ItemLoc;}
35 set{_ItemLoc= value ;}
36 }
37
38 public string IsnewItem
39 {
40 get{return _IsnewItem;}
41 set{_IsnewItem= value ;}
42 }
43 }
ok,经过以上的处理,可以直接用来绑定数据源,当然,这样处理有不好的地方,只不过也给出了一个解决问题的方法。
1public class AcceptConIteminfo
2 {
3
4 private string _OrigItemId = "";
5 private string _ItemName = "";
6 private string _ItemLoc = "";
7 private string _IsnewItem = "";
8 public AcceptConIteminfo(object[] values)
9 {
10 //
11 // TODO: 在此处添加构造函数逻辑
12 //
13 _OrigItemId = values[0].ToString() ;
14 _ItemName = values[1].ToString() ;
15 _ItemLoc = values[2].ToString() ;
16 _IsnewItem = values[3].ToString() ;
17 }
18
19
20 public string OrigItemId
21 {
22 get{return _OrigItemId;}
23 set{_OrigItemId= value ;}
24 }
25
26 public string ItemName
27 {
28 get{return _ItemName;}
29 set{_ItemName= value ;}
30 }
31
32 public string ItemLoc
33 {
34 get{return _ItemLoc;}
35 set{_ItemLoc= value ;}
36 }
37
38 public string IsnewItem
39 {
40 get{return _IsnewItem;}
41 set{_IsnewItem= value ;}
42 }
43 }