一、安装hubble.net(安装后会自动开启一个hubble.net的服务,)
二、首先是hubble.net的升级(目前hubble提供的版本都没有直接带有mongoAdapter)
首先 我们暂停hubble.net服务
其次 从hubble的官网http://hubbledotnet.codeplex.com/上面下载源代码进行编译。我的源代码下载到D:\软件\Hubble.Net\中了,解压后找到c#文件夹(D:\软件\Hubble.Net\hubbledotnet-100319\C#)找到c#文件夹中的src文件夹D:\软件\Hubble.Net\hubbledotnet-100319\C#\src,对解决方案进行编译(以release方式进行编译)。
再次 回到c#文件夹,找到bin文件夹下的D:\软件\Hubble.Net\hubbledotnet-100319\C#\Bin\Release文件夹,将其中的所有文件复制,复制到我们安装目录下的default文件夹下,并全部选择覆盖。
最后 开启服务,打开我们的hubble.net 就会发现我们的hubble上面已经有mongoAdapter了。
三、决定使用什么样的方式和索引MongoDB
1、主动模式(主动模式就是hubble完全控制数据库)
这种方式是最简单的方案,比较适合于即时性比较高的工作。
在此种情况下我们只要在hubble创建一个表,则同时mongodb中也会创建一个同样的表,整个表的结构完全相同,此时我们所有的操作都是直接去操作hubble即可。
2、被动模式(被动模式情况下我们需要自己手动的去操作数据库)
在这种情况下hubble表可以和mongo表不同(就是hubble表中可以只对需要索引的字段进行处理)
插入:先插入数据库再插入hubble
删除:先删除hubble再删除数据库中的记录
更新:顺序无所谓
3、sqlserver做主表,mongodb做镜像表
在这种情况下MongoDB也是完全受hubble控制的。
此时hubble与镜像表的关系是主动模式连接的,与主表的关系是被动模式连接的。
所以此时的操作规则如下
插入:先插入主表再插入hubble
删除:先删除hubble再删除主表中的记录
更新:顺序无所谓
四、代码实现
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using Hubble.SQLClient; 6 using System.Data; 7 using System.Configuration; 8 9 namespace HubbleIndex 10 { 11 public class HubbleHelper 12 { 13 //现在的方式是mongo库和表都要在hubble中手动的去建。 14 private readonly static string connStr =System.Configuration.ConfigurationSettings.AppSettings["HubbleConn"];//连接字符串 15 /// <summary> 16 /// 增删改的操作的方法 17 /// </summary> 18 /// <param name="hubbleSql">hubble的sql语句</param> 19 /// <param name="parameters">参数</param> 20 public static int ExecuteNonQuery(string hubbleSql, HubbleParameterCollection parameters) 21 { 22 int flag=0; 23 using (HubbleAsyncConnection conn = new HubbleAsyncConnection(connStr)) 24 { 25 conn.Open(); 26 HubbleCommand matchCmd =new HubbleCommand(hubbleSql, conn); 27 if (parameters!=null||parameters.Count!=0) 28 { 29 for (int i = 0; i < parameters.Count; i++) 30 matchCmd.Parameters.Add(parameters[i].ParameterName, parameters[i].Value); 31 } 32 try {flag= matchCmd.ExecuteNonQuery(); } 33 catch (Exception e) 34 { 35 flag = -1; 36 throw e; 37 } 38 } 39 return flag; 40 } 41 /// <summary> 42 /// 查询方法 43 /// </summary> 44 /// <param name="hubbleSql">hubble的sql语句</param> 45 /// <param name="parameters">参数</param> 46 /// <param name="count">查询的数量</param> 47 /// <param name="cacheTimeout">缓存过期时间</param> 48 /// <returns></returns> 49 public static DataSet ExecuteSelect(string hubbleSql,int cacheTimeout,out int count, HubbleParameterCollection parameters) 50 { 51 DataSet ds = new DataSet(); count = 0; 52 using (HubbleAsyncConnection conn = new HubbleAsyncConnection(connStr)) 53 { 54 conn.Open(); 55 HubbleDataAdapter adapter = new HubbleDataAdapter(); 56 adapter.SelectCommand = new HubbleCommand(hubbleSql, conn); 57 //int cacheTimeout = 10; 58 adapter.SelectCommand.CacheTimeout = cacheTimeout;//秒 59 if (parameters!=null||parameters.Count!=0) 60 { 61 for (int i = 0; i < parameters.Count; i++) 62 adapter.SelectCommand.Parameters.Add(parameters[i].ParameterName, parameters[i].Value); 63 } 64 65 HubbleCommand cmd = adapter.SelectCommand; 66 try 67 { 68 ds = cmd.Query(cacheTimeout); 69 count = ds.Tables[0].MinimumCapacity; 70 } 71 catch (Exception e) { throw e; } 72 } 73 return ds; 74 } 75 } 76 }
添加的引用
转载请注明出处:http://www.cnblogs.com/huyulin/p/3480198.html