ASP.NET MongoDB資料庫操作類
1、Web.config檔案中配置資料庫連線資訊,如下程式碼:
<appSettings>
<!--連線MongoDB資料庫連線字串開始-->
<add key="MongoIP" value="192.168.33.162" />
<add key="MongoDatabase" value="ADS5_HNXY" />
<!--MongoDB叢集名稱,單臺MongoDB時請註釋掉下面行的配置-->
<!--<add key="ReplicaSetName" value="atrepl"/>-->
<add key="MongoUser" value=""/>
<add key="MongoPassword" value=""/>
<!--連線MongoDB資料庫連線字串結束 -->
</appSettings>
2、MongoDBHelper操作類,如下程式碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Linq;
using AT.Business.IDAO;
using AT.Business.DAL;
using AT_DataShiftService;
namespace BAL.DBHelper
{
/// <summary>
/// MongoDB資料庫操作類
/// </summary>
public class MongoData
{
#region 屬性列表
private static string ip = System.Configuration.ConfigurationManager.AppSettings["MongoIP"];
private static string dbname = System.Configuration.ConfigurationManager.AppSettings["MongoDatabase"];
private static string user = System.Configuration.ConfigurationManager.AppSettings["MongoUser"];
private static string pwd = System.Configuration.ConfigurationManager.AppSettings["MongoPassword"];
private static string myReplicaSetName = System.Configuration.ConfigurationManager.AppSettings["ReplicaSetName"];
private static ReadPreference myReadPreference = ReadPreference.SecondaryPreferred;
//兩個不同的表名
private const string _ADS5 = "ads5";
private const string _POWERPARAMETERS = "PowerParameters";
private static AT_System_IDAO systemidao = new AT_System_Dal();
#endregion
#region MongoDB許可權認證
/// <summary>
/// MongoDB許可權認證
/// </summary>
/// <returns></returns>
public static MongoDatabase getDatabase()
{
MongoClientSettings setting = new MongoClientSettings();
if (!string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(pwd))
{
//Logger.Log.Info("MongoDB開啟許可權訪問 " + user + ":" + pwd);
List<MongoCredential> lstCredential = new List<MongoCredential>();
lstCredential.Add(MongoCredential.CreateCredential(dbname, user, pwd));
setting.Credentials = lstCredential;
}
setting.Server = new MongoServerAddress(ip);
if (!string.IsNullOrEmpty(myReplicaSetName))
{
//Logger.Log.Info("MongoDB開啟叢集模式 " + myReplicaSetName);
setting.ConnectionMode = ConnectionMode.ReplicaSet;
setting.ReplicaSetName = myReplicaSetName;
setting.ReadPreference = myReadPreference;
}
//MongoClient client = new MongoClient(QJBL.MongoDBConn);
var client = new MongoClient(setting);
MongoServer server = client.GetServer();
MongoDatabase database = server.GetDatabase(dbname);
return database;
}
#endregion
#region 獲取MySQL中對應表具列表資訊 2017-05-17
/// <summary>
/// 獲取MySQL中對應表具列表資訊
/// </summary>
/// <returns></returns>
public static DataTable GetMeterList()
{
return systemidao.GetMeterList();
}
#endregion
#region 獲取每個整點需要上傳的表具讀數 2017-05-17
/// <summary>
/// 獲取每個整點需要上傳的表具讀數
/// </summary>
/// <param name="datetime"></param>
/// <returns></returns>
public static DataTable AT_Up_EnergyValue4WJW(string datetime)
{
DataTable dt = GetMeterList();
DataTable newDT = new DataTable();
newDT.Columns.Add("BuildID", typeof(string));
newDT.Columns.Add("CollectionID", typeof(string));
newDT.Columns.Add("MeterID", typeof(string));
newDT.Columns.Add("EnergyValue", typeof(double));
foreach (DataRowView drv in dt.DefaultView)
{
DataRow row = newDT.NewRow();
if (drv["F_MeasureClassify"].ToString() == "04" || drv["F_MeasureClassify"].ToString() == "14")
{
//從MongoDB中的PowerParameters中取數
row["BuildID"] = drv["BuildID"].ToString();
row["CollectionID"] = drv["CollectionID"].ToString();
row["MeterID"] = drv["MeterID"].ToString();
double rawValue = GetDataFromPowerParameters(drv["F_MeterID"].ToString(), DateTime.Parse(datetime), drv["F_ValueType"].ToString());
//這裡去除的可能是負數,要做絕對值轉換
double absValue = Math.Abs(rawValue);
row["EnergyValue"] = double.Parse(drv["F_Ratio"].ToString()) * absValue * 0.0036;
WriteLog(drv["F_MeterID"].ToString() + "\t" + datetime + "\t" + (double.Parse(drv["F_Ratio"].ToString()) * absValue * 0.0036) + "\r");
newDT.Rows.Add(row);
}
else
{
//從MongoDB中的ADS5中取數
row["BuildID"] = drv["BuildID"].ToString();
row["CollectionID"] = drv["CollectionID"].ToString();
row["MeterID"] = drv["MeterID"].ToString();
double rawValue = GetDataFromADS5(drv["F_TagName"].ToString(), DateTime.Parse(datetime));
double absValue = Math.Abs(rawValue);
row["EnergyValue"] = double.Parse(drv["F_Ratio"].ToString()) * absValue;
WriteLog(drv["F_TagName"].ToString() + "\t" + datetime + "\t" + double.Parse(drv["F_Ratio"].ToString()) * absValue + "\r");
newDT.Rows.Add(row);
}
}
return newDT;
}
#endregion
#region 當F_MeasureClassify不為‘04’和‘14’時,從ADS5表中獲取表頭示數 2017-08-03
/// <summary>
/// F_CaclType = 0 ,從ADS5表中獲取資料
/// </summary>
/// <param name="TagName"></param>
/// <param name="DateTime"></param>
/// <returns></returns>
public static double GetDataFromADS5(string TagName, DateTime DateTime)
{
MongoDatabase db = MongoData.getDatabase();
//獲得Users集合,如果資料庫中沒有,先新建一個
MongoCollection col = db.GetCollection(_ADS5);
var query = Query.And(
Query.EQ("TagName", TagName),
Query.EQ("DateTime", DateTime)
);
List<BsonDocument> documents = col.FindAs<BsonDocument>(query).ToList();
//做異常處理
if (documents.Count != 0)
{
BsonElement element = documents[0].GetElement("Value");
return double.Parse(element.Value.ToString());
}
else
{
//直至可以獲取到上一個有資料的時刻 2017-08-03
int index = 0;
do
{
index++;
DateTime NewDateTime = DateTime.AddHours(-1 * index);
query = Query.And(
Query.EQ("TagName", TagName),
Query.EQ("DateTime", NewDateTime)
);
documents = col.FindAs<BsonDocument>(query).ToList();
} while (documents.Count == 0);
BsonElement element = documents[0].GetElement("Value");
return double.Parse(element.Value.ToString());
}
}
#endregion
#region 當F_MeasureClassify為‘04’或‘14’時,從PowerParameters表中獲取表頭示數 2017-08-03
/// <summary>
/// 從PowerParameters表中獲取資料
/// </summary>
/// <param name="TagName"></param>
/// <param name="DateTime"></param>
/// <returns></returns>
public static double GetDataFromPowerParameters(string MeterID, DateTime DateTime, string ValueType)
{
MongoDatabase db = MongoData.getDatabase();
//獲得Users集合,如果資料庫中沒有,先新建一個
MongoCollection col = db.GetCollection(_POWERPARAMETERS);
var query = Query.And(
Query.EQ("MeterID", MeterID),
Query.EQ("DateTime", DateTime)
);
List<BsonDocument> documents = col.FindAs<BsonDocument>(query).ToList();
//做異常處理
if (documents.Count != 0)
{
BsonElement element = documents[0].GetElement(ValueType);
return double.Parse(element.Value.ToString());
}
else
{
//直至可以獲取到上一個有資料的時刻 2017-08-03
int index = 0;
do
{
index ++;
DateTime NewDateTime = DateTime.AddHours(-1 * index);
query = Query.And(
Query.EQ("MeterID", MeterID),
Query.EQ("DateTime", NewDateTime)
);
documents = col.FindAs<BsonDocument>(query).ToList();
} while (documents.Count == 0);
BsonElement element = documents[0].GetElement(ValueType);
return double.Parse(element.Value.ToString());
}
}
#endregion
#region 日誌記錄
/// <summary>
///
/// </summary>
/// <param name="log"></param>
public static void WriteLog(string log)
{
string spath1 = System.AppDomain.CurrentDomain.BaseDirectory + @"\GetInterupt.Log";
string spath = System.AppDomain.CurrentDomain.BaseDirectory + @"\" + DateTime.Now.ToString("yyyy") + @"\GetInterupt" + DateTime.Now.ToString("MM") + ".Log";
if (!FileC.IsExistFile(spath))
{
FileC.CreateDirectory(FileC.GetDirectoryName(spath));
FileC.CreateFile(spath);
FileC.WriteText(spath1, "");
}
FileC.AppendText(spath, log + "\r\n");
FileC.AppendText(spath1, log + "\r\n");
}
#endregion
}
}
相關文章
- mongodb資料庫操作MongoDB資料庫
- MongoDB 資料庫操作MongoDB資料庫
- python操作mongodb資料庫PythonMongoDB資料庫
- Python 資料庫騷操作 -- MongoDBPython資料庫MongoDB
- Python資料庫MongoDB騷操作Python資料庫MongoDB
- Python操作MongoDB文件資料庫PythonMongoDB資料庫
- MongoDB資料庫的基本操作梳理MongoDB資料庫
- MongoDB資料庫操作詳解:基礎篇MongoDB資料庫
- node 使用 monk 工具操作 mongodb 資料庫學習MongoDB資料庫
- MongoDB資料庫MongoDB資料庫
- ASP.NET Core使用EF Core操作MySql資料庫ASP.NETMySql資料庫
- Eolink Apikit :資料字典功能上線、支援 MongoDB 資料庫操作...APIMongoDB資料庫
- python資料庫-mongoDB的高階查詢操作(55)Python資料庫MongoDB
- Python3資料庫操作基本類Python資料庫
- 常見MongoDB資料庫操作產生的鎖總結MongoDB資料庫
- php簡單操作mysql資料庫的類PHPMySql資料庫
- .NET關於資料庫操作的類-囊括所有的操作資料庫
- mongodb資料庫中插入資料MongoDB資料庫
- MongoDB資料庫安裝MongoDB資料庫
- 快速掌握 MongoDB 資料庫MongoDB資料庫
- Mongodb資料庫連線MongoDB資料庫
- MongoDB資料庫簡介MongoDB資料庫
- 初試MongoDB資料庫MongoDB資料庫
- 資料庫_SQL-MongoDB資料庫SQLMongoDB
- mongodb 如何建立資料庫MongoDB資料庫
- MongoDB資料庫入門MongoDB資料庫
- 學習MongoDB資料庫MongoDB資料庫
- mongoDB資料庫之聚合MongoDB資料庫
- MongoDB資料庫備份MongoDB資料庫
- TSPython操作MySQL MongoDB Oracle三大資料庫深入對比oeePythonMySqlMongoDBOracle大資料資料庫
- 資料庫學習與複習筆記--資料庫概念和不同類資料庫CRUD操作(1)資料庫筆記
- 資料庫操作資料庫
- 資料庫操作·資料庫
- SpringBoot整合Mongodb文件資料庫Spring BootMongoDB資料庫
- mongodb資料庫如何建立索引?MongoDB資料庫索引
- MongoDB資料庫中查詢資料(下)MongoDB資料庫
- MongoDB 資料庫管理和開發:Navicat for MongoDB macMongoDB資料庫Mac
- mongodb資料庫備份與恢復(資料庫資料遷移)MongoDB資料庫
- 【Falsk 使用資料庫】---- 資料庫基本操作資料庫