/// <summary> /// 读取host表 /// </summary> /// <param name="dnsPack"></param> /// <param name="cache"></param> /// <returns></returns> public bool ReadHost(DNSProxy.DnsPack dnsPack, out byte[] cache) { try { if (!isInit) { throw new Exception(); } OleDbCommand cmd; cmd = conn.CreateCommand(); cmd.CommandText = "select QueryDns from dnsHost where QueryName = ? and QueryType = ?"; cmd.Parameters.Add("@QueryName", OleDbType.VarChar).Value = dnsPack.QueryRecords[0].QueryName; cmd.Parameters.Add("@QueryType", OleDbType.VarChar).Value = dnsPack.QueryRecords[0].QueryType; string Value = (string)cmd.ExecuteScalar(); if (Value != null && Value != string.Empty) { switch (dnsPack.QueryRecords[0].QueryType) { case DNSProxy.QueryType.A: IPAddress iP; if (!IPAddress.TryParse(Value, out iP)) { break; } string[] ips = iP.ToString().Split('.'); byte[] ipBtye = new byte[] { byte.Parse(ips[0]), byte.Parse(ips[1]), byte.Parse(ips[2]), byte.Parse(ips[3]) }; dnsPack.ResouceRecords.Add(new DNSProxy.ResouceRecord { QueryClass = 1, QueryType = DNSProxy.QueryType.A, QueryName = dnsPack.QueryRecords[0].QueryName, Datas = ipBtye, TTL = 0 }); break; default: break; } cache = dnsPack.ToBytes(); return(cache != null); } throw new Exception(); } catch (Exception) { cache = new byte[0]; return(false); } }
/// <summary> /// 写入缓存到数据库 /// </summary> /// <param name="dataTable"></param> /// <returns></returns> public bool WriteCache(DNSProxy.DnsPack dnsPack) { try { if (!isInit) { throw new Exception(); } bool result; OleDbCommand cmd = null; OleDbTransaction transaction = null; try { #region 保存总账单 // 开始事务 transaction = conn.BeginTransaction(); cmd = conn.CreateCommand(); cmd.Transaction = transaction; cmd.Connection = conn; cmd.CommandText = "insert into dnscache(QueryName, QueryType, QueryClass, QueryDns) values(?, ?, ?, ?)"; cmd.Parameters.Add("@QueryName", OleDbType.VarChar).Value = dnsPack.QueryRecords[0].QueryName; cmd.Parameters.Add("@QueryType", OleDbType.VarChar).Value = dnsPack.QueryRecords[0].QueryType; cmd.Parameters.Add("@QueryClass", OleDbType.VarChar).Value = dnsPack.QueryRecords[0].QueryClass; cmd.Parameters.Add("@ImageBinary", OleDbType.Binary, dnsPack.ToBytes().Length).Value = dnsPack.ToBytes(); if (cmd.ExecuteNonQuery() < 0) { throw new Exception(); } #endregion //提交事务 transaction.Commit(); result = true; } catch { //异常时回滚事务 result = false; transaction.Rollback(); } finally { cmd.Dispose(); transaction.Dispose(); } return(result); } catch { return(false); } }