mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Add MongoDb Csproj
This commit is contained in:
parent
a425f4d66b
commit
eb6a041b0f
11
Src/Asp.NetCore2/MongoDb.Ado.data/MongoDb.Ado.data.csproj
Normal file
11
Src/Asp.NetCore2/MongoDb.Ado.data/MongoDb.Ado.data.csproj
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="MongoDB.Driver" Version="3.3.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
109
Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbConnection.cs
Normal file
109
Src/Asp.NetCore2/MongoDb.Ado.data/MongoDbConnection.cs
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Data.Common;
|
||||||
|
using System.Linq;
|
||||||
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
|
||||||
|
namespace MongoDb.Ado.data
|
||||||
|
{
|
||||||
|
public class MongoDbConnection : DbConnection
|
||||||
|
{
|
||||||
|
private readonly string _originalConnectionString;
|
||||||
|
private MongoClient _client;
|
||||||
|
private IMongoDatabase _database;
|
||||||
|
private ConnectionState _state = ConnectionState.Closed;
|
||||||
|
private string _databaseName;
|
||||||
|
|
||||||
|
public override string ConnectionString
|
||||||
|
{
|
||||||
|
get => _originalConnectionString;
|
||||||
|
set => throw new NotSupportedException("Setting ConnectionString after construction is not supported.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string Database => _databaseName;
|
||||||
|
|
||||||
|
public override string DataSource => _client?.Settings?.Server?.ToString() ?? "";
|
||||||
|
|
||||||
|
public override string ServerVersion => "MongoDB_" + (_client?.Cluster?.Description?.ClusterId.ToString() ?? "Unknown");
|
||||||
|
|
||||||
|
public override ConnectionState State => _state;
|
||||||
|
|
||||||
|
public MongoDbConnection(string connectionString)
|
||||||
|
{
|
||||||
|
_originalConnectionString = connectionString;
|
||||||
|
ParseAndConnect(connectionString);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ParseAndConnect(string connStr)
|
||||||
|
{
|
||||||
|
if (connStr.TrimStart().StartsWith("mongodb://", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
var mongoUrl = new MongoUrl(connStr);
|
||||||
|
_client = new MongoClient(mongoUrl);
|
||||||
|
_databaseName = mongoUrl.DatabaseName ?? "test";
|
||||||
|
_database = _client.GetDatabase(_databaseName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var dict = ParsePgStyleConnectionString(connStr);
|
||||||
|
var host = dict.GetValueOrDefault("Host", "localhost");
|
||||||
|
var port = dict.GetValueOrDefault("Port", "27017");
|
||||||
|
_databaseName = dict.GetValueOrDefault("Database", "test");
|
||||||
|
var username = dict.GetValueOrDefault("Username", "");
|
||||||
|
var password = dict.GetValueOrDefault("Password", "");
|
||||||
|
|
||||||
|
var mongoConnStr = string.IsNullOrEmpty(username)
|
||||||
|
? $"mongodb://{host}:{port}"
|
||||||
|
: $"mongodb://{Uri.EscapeDataString(username)}:{Uri.EscapeDataString(password)}@{host}:{port}/{_databaseName}";
|
||||||
|
|
||||||
|
var mongoUrlParsed = new MongoUrl(mongoConnStr);
|
||||||
|
_client = new MongoClient(mongoUrlParsed);
|
||||||
|
_database = _client.GetDatabase(_databaseName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Dictionary<string, string> ParsePgStyleConnectionString(string connStr)
|
||||||
|
{
|
||||||
|
var builder = new DbConnectionStringBuilder { ConnectionString = connStr };
|
||||||
|
return builder.Cast<KeyValuePair<string, object>>()
|
||||||
|
.ToDictionary(kv => kv.Key, kv => kv.Value.ToString(), StringComparer.OrdinalIgnoreCase);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Open()
|
||||||
|
{
|
||||||
|
// MongoClient 实际上在操作集合时才连接,我们只改变状态
|
||||||
|
_state = ConnectionState.Open;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Close()
|
||||||
|
{
|
||||||
|
_state = ConnectionState.Closed;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override DbCommand CreateDbCommand()
|
||||||
|
{
|
||||||
|
throw new NotSupportedException("MongoDB does not support SQL-style DbCommand.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void ChangeDatabase(string databaseName)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(databaseName))
|
||||||
|
throw new ArgumentException("Database name cannot be null or empty.", nameof(databaseName));
|
||||||
|
|
||||||
|
_database = _client.GetDatabase(databaseName);
|
||||||
|
_databaseName = databaseName;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
|
||||||
|
{
|
||||||
|
throw new NotSupportedException("MongoDB does not support transactions via DbTransaction.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public IMongoDatabase GetDatabase() => _database;
|
||||||
|
|
||||||
|
public MongoClient GetClient() => _client;
|
||||||
|
|
||||||
|
public override string ToString() => _originalConnectionString;
|
||||||
|
}
|
||||||
|
}
|
28
Src/Asp.NetCore2/MongoDbTest/AdoTest.cs
Normal file
28
Src/Asp.NetCore2/MongoDbTest/AdoTest.cs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
using MongoDb.Ado.data;
|
||||||
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MongoDbTest
|
||||||
|
{
|
||||||
|
public class AdoTest
|
||||||
|
{
|
||||||
|
public static void Init()
|
||||||
|
{
|
||||||
|
//开发中
|
||||||
|
var client = new MongoClient("");
|
||||||
|
var database = client.GetDatabase("test");
|
||||||
|
// 获取当前数据库中的所有集合
|
||||||
|
var collections = database.ListCollections();
|
||||||
|
|
||||||
|
|
||||||
|
MongoDbConnection connection = new MongoDbConnection("");
|
||||||
|
connection.Open();
|
||||||
|
connection.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
Src/Asp.NetCore2/MongoDbTest/MongoDbTest.csproj
Normal file
14
Src/Asp.NetCore2/MongoDbTest/MongoDbTest.csproj
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\MongoDb.Ado.data\MongoDb.Ado.data.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
4
Src/Asp.NetCore2/MongoDbTest/Program.cs
Normal file
4
Src/Asp.NetCore2/MongoDbTest/Program.cs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
using MongoDbTest;
|
||||||
|
//开发中
|
||||||
|
AdoTest.Init();
|
||||||
|
Console.WriteLine("执行完成");
|
@ -98,6 +98,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlSugar.DuckDBCore", "SqlS
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DuckDBTest", "DuckDBTest\DuckDBTest.csproj", "{F8DBA187-9A40-4AE5-972F-230835FEAEE4}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DuckDBTest", "DuckDBTest\DuckDBTest.csproj", "{F8DBA187-9A40-4AE5-972F-230835FEAEE4}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MongoDb", "MongoDb", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDb.Ado.data", "MongoDb.Ado.data\MongoDb.Ado.data.csproj", "{9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDbTest", "MongoDbTest\MongoDbTest.csproj", "{C6547EB1-B5AE-426A-A04D-3E2685AD7114}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -636,6 +642,30 @@ Global
|
|||||||
{F8DBA187-9A40-4AE5-972F-230835FEAEE4}.Release|ARM32.Build.0 = Release|Any CPU
|
{F8DBA187-9A40-4AE5-972F-230835FEAEE4}.Release|ARM32.Build.0 = Release|Any CPU
|
||||||
{F8DBA187-9A40-4AE5-972F-230835FEAEE4}.Release|x86.ActiveCfg = Release|Any CPU
|
{F8DBA187-9A40-4AE5-972F-230835FEAEE4}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{F8DBA187-9A40-4AE5-972F-230835FEAEE4}.Release|x86.Build.0 = Release|Any CPU
|
{F8DBA187-9A40-4AE5-972F-230835FEAEE4}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Debug|ARM32.ActiveCfg = Debug|Any CPU
|
||||||
|
{9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Debug|ARM32.Build.0 = Debug|Any CPU
|
||||||
|
{9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Release|ARM32.ActiveCfg = Release|Any CPU
|
||||||
|
{9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Release|ARM32.Build.0 = Release|Any CPU
|
||||||
|
{9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{9D7D900A-01ED-4E7C-A97A-1D3325F67AA2}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Debug|ARM32.ActiveCfg = Debug|Any CPU
|
||||||
|
{C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Debug|ARM32.Build.0 = Debug|Any CPU
|
||||||
|
{C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Release|ARM32.ActiveCfg = Release|Any CPU
|
||||||
|
{C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Release|ARM32.Build.0 = Release|Any CPU
|
||||||
|
{C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{C6547EB1-B5AE-426A-A04D-3E2685AD7114}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@ -655,6 +685,8 @@ Global
|
|||||||
{F706204F-2FC4-5112-646F-28D498E56AF4} = {88992AAF-146B-4253-9AD7-493E8F415B57}
|
{F706204F-2FC4-5112-646F-28D498E56AF4} = {88992AAF-146B-4253-9AD7-493E8F415B57}
|
||||||
{15CB3CFF-E99D-4A79-8F6F-D9175CC2F7CB} = {88992AAF-146B-4253-9AD7-493E8F415B57}
|
{15CB3CFF-E99D-4A79-8F6F-D9175CC2F7CB} = {88992AAF-146B-4253-9AD7-493E8F415B57}
|
||||||
{7AF1C5DF-AEA1-47E2-92CA-621330F68860} = {88992AAF-146B-4253-9AD7-493E8F415B57}
|
{7AF1C5DF-AEA1-47E2-92CA-621330F68860} = {88992AAF-146B-4253-9AD7-493E8F415B57}
|
||||||
|
{02EA681E-C7D8-13C7-8484-4AC65E1B71E8} = {88992AAF-146B-4253-9AD7-493E8F415B57}
|
||||||
|
{9D7D900A-01ED-4E7C-A97A-1D3325F67AA2} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {230A85B9-54F1-41B1-B1DA-80086581B2B4}
|
SolutionGuid = {230A85B9-54F1-41B1-B1DA-80086581B2B4}
|
||||||
|
Loading…
Reference in New Issue
Block a user