首页 > 数据库 >dotnet 连接多个数据库

dotnet 连接多个数据库

时间:2024-01-18 19:45:48浏览次数:37  
标签:SqliteToOracle get Column 数据库 public TypeName set dotnet 连接

SqliteToOracle\global.json

{
  "sdk": {
    "version": "7.0.401"
  }
}

SqliteToOracle\SqliteToOracle.sln


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqliteToOracle.App", "SqliteToOracle.App\SqliteToOracle.App.csproj", "{F31078FC-6326-4CDA-AA8A-DB3AAF33D029}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqliteToOracle.Oracle", "SqliteToOracle.Oracle\SqliteToOracle.Oracle.csproj", "{12650F95-B263-402B-A8AF-337DD82072F5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqliteToOracle.StandardBattery", "SqliteToOracle.StandardBattery\SqliteToOracle.StandardBattery.csproj", "{7298432C-9458-4360-A5FF-8938D5B76C01}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{F31078FC-6326-4CDA-AA8A-DB3AAF33D029}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{F31078FC-6326-4CDA-AA8A-DB3AAF33D029}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{F31078FC-6326-4CDA-AA8A-DB3AAF33D029}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{F31078FC-6326-4CDA-AA8A-DB3AAF33D029}.Release|Any CPU.Build.0 = Release|Any CPU
		{12650F95-B263-402B-A8AF-337DD82072F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{12650F95-B263-402B-A8AF-337DD82072F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{12650F95-B263-402B-A8AF-337DD82072F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{12650F95-B263-402B-A8AF-337DD82072F5}.Release|Any CPU.Build.0 = Release|Any CPU
		{7298432C-9458-4360-A5FF-8938D5B76C01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{7298432C-9458-4360-A5FF-8938D5B76C01}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{7298432C-9458-4360-A5FF-8938D5B76C01}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{7298432C-9458-4360-A5FF-8938D5B76C01}.Release|Any CPU.Build.0 = Release|Any CPU
	EndGlobalSection
EndGlobal

SqliteToOracle\SqliteToOracle.App\Program.cs

using SqliteToOracle.Oracle.Entities;
using SqliteToOracle.StandardBattery.StandardBatteryContext;

var dbContextStandardBattery = new StandardBatteryContext();


// create dbContextStandardBattery
await dbContextStandardBattery.Database.EnsureCreatedAsync();

var yesterday = DateTime.Today.AddDays(-1);
var yesterdayStart = yesterday; // 00:00:00
var yesterdayEnd = yesterday.AddDays(1).AddSeconds(-1); // 23:59:59

var batteries = dbContextStandardBattery.Batteries
    .Where(b => b.EndTime >= yesterdayStart && b.EndTime <= yesterdayEnd) // filter by date range
    .ToList(); // convert to list


foreach (var battery in batteries)
{
    var batNew = new BatteryOracle { Ip = battery.Ip, Name = battery.Name, StartLevel = battery.StartLevel, StartVoltage = battery.StartVoltage, StartCycle = battery.StartCycle, StartTime = battery.StartTime, Charging = battery.Charging };
    break;
}



// close dbContextStandardBattery
await dbContextStandardBattery.DisposeAsync();





SqliteToOracle\SqliteToOracle.App\SqliteToOracle.App.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <ItemGroup>
    <ProjectReference Include="..\SqliteToOracle.Oracle\SqliteToOracle.Oracle.csproj" />
    <ProjectReference Include="..\SqliteToOracle.StandardBattery\SqliteToOracle.StandardBattery.csproj" />
  </ItemGroup>

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <RestorePackagesPath>packages</RestorePackagesPath>
  </PropertyGroup>

</Project>

SqliteToOracle\SqliteToOracle.Oracle\OracleContext.cs

using Microsoft.EntityFrameworkCore;
using SqliteToOracle.Oracle.Entities;


namespace SqliteToOracle.Oracle.StandardBatteryContext;

public class OracleContext : DbContext
{
    public DbSet<BatteryOracle> Batteries { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            //使用UseOracle方法来指定使用Oracle数据库,并传入连接字符串
            //连接字符串中需要指定数据库用户,密码,服务名等信息
            //使用UseOracleSQLCompatibility方法来指定数据库版本
            optionsBuilder.UseOracle(
                "User Id=<username>;Password=<password>;Data Source=<host>/<port>",
                b => b.UseOracleSQLCompatibility("11"));
        }
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<BatteryOracle>().ToTable("STANDARD_BATTERY");
    }
}




SqliteToOracle\SqliteToOracle.Oracle\SqliteToOracle.Oracle.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Oracle.EntityFrameworkCore" Version="7.21.13" />
    <PackageReference Include="Oracle.ManagedDataAccess.Core" Version="3.21.130" />
  </ItemGroup>

</Project>


SqliteToOracle\SqliteToOracle.Oracle\Entities\Battery.cs

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;


namespace SqliteToOracle.Oracle.Entities;

public class BatteryOracle
{
      // 使用Guid类型作为主键,并生成一个新的uuid
    [Key]
    [Column("ID")]
    public Guid Id { get; set; } = Guid.NewGuid();

    [Required]
    [Column("IP", TypeName = "VARCHAR2(20)")]
    public string? Ip { get; set; }

    [Column("NAME", TypeName = "VARCHAR2(20)")]
    public string? Name { get; set; }

    [Column("START_LEVEL", TypeName = "FLOAT")]
    public float? StartLevel { get; set; }

    [Column("END_LEVEL", TypeName = "FLOAT")]
    public float? EndLevel { get; set; }

    [Column("START_TEMP", TypeName = "FLOAT")]
    public float?  StartTemp { get; set; }

    [Column("END_TEMP", TypeName = "FLOAT")]
    public float?  EndTemp { get; set; }

    [Column("START_VOLTAGE", TypeName = "FLOAT")]
    public float?  StartVoltage { get; set; }

    [Column("END_VOLTAGE", TypeName = "FLOAT")]
    public float?  EndVoltage { get; set; }

    [Column("START_CYCLE", TypeName = "NUMBER")]
    public int?  StartCycle { get; set; }

    [Column("END_CYCLE", TypeName = "NUMBER")]
    public int?  EndCycle { get; set; }

    [Column("START_TIME", TypeName = "DATE")]
    public DateTime?  StartTime { get; set; }

    [Column("END_TIME", TypeName = "DATE")]
    public DateTime?  EndTime { get; set; }

    [Column("DURATION", TypeName = "NUMBER")]
    public int?  Duration { get; set; }

    [Column("INTERRUPT", TypeName = "NUMBER")]
    public int?  Interrupt { get; set; }

    [Column("CHARGING", TypeName = "NUMBER")]
    public bool Charging { get; set; }
    
}



SqliteToOracle\SqliteToOracle.StandardBattery\SqliteToOracle.StandardBattery.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

</Project>

SqliteToOracle\SqliteToOracle.StandardBattery\StandardBatteryContext.cs

using Microsoft.EntityFrameworkCore;
using SqliteToOracle.StandardBattery.Entities;


namespace  SqliteToOracle.StandardBattery.StandardBatteryContext;
public class StandardBatteryContext : DbContext
{
    public DbSet<Battery> Batteries { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlite(@"Data Source=F:\song\dotnet_miniapi-main_learn\dotnet_miniapi-main\Dbs\Games.db");
        }
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Battery>().ToTable("Batteries");
    }
}




SqliteToOracle\SqliteToOracle.StandardBattery\Entities\Battery.cs

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;


namespace SqliteToOracle.StandardBattery.Entities;

public class Battery
{
    [Key]
    [Column("id")]
    public int Id { get; set; }

    [Required]
    [Column("ip", TypeName = "varchar(20)")]
    public required string Ip { get; set; }

    [Column("name", TypeName = "varchar(20)")]
    public string? Name { get; set; }

    [Column("start_level", TypeName = "float")]
    public float? StartLevel { get; set; }

    [Column("end_level", TypeName = "float")]
    public float? EndLevel { get; set; }

    [Column("start_temp", TypeName = "float")]
    public float?  StartTemp { get; set; }

    [Column("end_temp", TypeName = "float")]
    public float?  EndTemp { get; set; }

    [Column("start_voltage", TypeName = "float")]
    public float?  StartVoltage { get; set; }

    [Column("end_voltage", TypeName = "float")]
    public float?  EndVoltage { get; set; }

    [Column("start_cycle", TypeName = "int")]
    public int?  StartCycle { get; set; }

    [Column("end_cycle", TypeName = "int")]
    public int?  EndCycle { get; set; }

    [Column("start_time", TypeName = "datetime")]
    public DateTime?  StartTime { get; set; }

    [Column("end_time", TypeName = "datetime")]
    public DateTime?  EndTime { get; set; }

    [Column("duration", TypeName = "int")]
    public int?  Duration { get; set; }

    [Column("interrupt", TypeName = "bit")]
    public bool?  Interrupt { get; set; }

    [Column("charging", TypeName = "bit")]
    public bool Charging { get; set; }
    
}


cmd 命令记录导出

doskey /history > history.txt

当前项目的命令记录

dotnet new globaljson

dotnet new sln --output SqliteToOracle
cd SqliteToOracle
dotnet new console --name SqliteToOracle.App
dotnew sln list
dotnet sln add SqliteToOracle.App
dotnet sln list

dotnet new list
dotnet new classlib --name SqliteToOracle.StandardBattery


dotnet new classlib --name SqliteToOracle.Oracle
dotnet sln list

dotnet sln add SqliteToOracle.Oracle
dotnet sln add SqliteToOracle.StandardBattery
dotnet sln list

dotnet add SqliteToOracle.App\SqliteToOracle.App.csproj reference SqliteToOracle.Oracle\SqliteToOracle.Oracle.csproj
dotnet add SqliteToOracle.App\SqliteToOracle.App.csproj reference SqliteToOracle.StandardBattery\SqliteToOracle.StandardBattery.csproj

标签:SqliteToOracle,get,Column,数据库,public,TypeName,set,dotnet,连接
From: https://www.cnblogs.com/zhuoss/p/17973241

相关文章

  • aws 服务器 ssh 通过密码连接配置
    背景通过信用卡薅了一年aws服务器的羊毛,1cpu30g固态硬盘,默认服务器不允许root用户使用密码进行ssh连接,因此需要以下的步骤通过密码远程ssh连接到服务器。步骤使用AWS控制台进行网页登录创建root密码sudopasswdroot切换到root用户suroot修改sshd_co......
  • 快速监控 Oracle 数据库
    Oracle数据库在行业内应用广泛,通常存放的非常重要的数据,监控是必不可少的,本文使用Cprobe采集Oracle监控数据,极致简单,分享给大家。安装配置Oracle简单起见,我使用Docker启动Oracle,命令如下:dockerrun-d--nameoracle-p1022:22-p18080:8080-p1521:1521wnamel......
  • jmeter使用jdbc连接SQL server,执行SQL报错处理
    前置环境参数:jdk-8u391-windows-x64,驱动:sqljdbc4.jar备注:这是解决后的截图,将就用问题一:使用jmeter5.5,使用jdbc连接SQLserver,执行SQL报错处理,如下图 报错信息:java.lang.UnsupportedClassVersionError:com/microsoft/sqlserver/jdbc/SQLServerDriverhasbeencompiledby......
  • Xshell通过跳板机连接服务器
    一、GitSSH密钥生成1.打开命令行工具,输入以下命令:$ssh-keygen-trsa-C"[email protected]"其中,“-t”指定密钥类型,可以是“rsa”、“dsa”等;“-C”指定注释信息,一般为邮箱地址。2.生成密钥的存储路径:Whatisthefileinwhichtosavethekey?(/c/Users/you/.......
  • 数据库索引和索引优化
    索引和索引优化MysqlInnoDB使用B+树作为索引,如下图,是一个简化的B+数:使用B+树作为索引有点非常明显的优点1、B+树的数据都保存在叶子节点中,非叶子节点只保存指针,这样可以极大的减少数的阶数。如图如果每一阶可以存储1000个值,那么3阶树即可以存储1000*1000*1000=10亿个数据。而根......
  • Ubuntu和windows连接串口设备方法
    Ubuntu和windows连接串口设备方法一、Ubuntu连接串口1.下载并安装minicom工具sudoapt-getinstallminicom2.串口板连接主机和电脑后执行命令进入串口界面sudominicom-b115200二、windows连接串口1.下载并安装putty工具https://www.putty.org2.打开PUTTY工具,进入对应界面,如......
  • 阿里云 PolarDB 开发者大会首度召开,让数据库开发像“搭积木”一样简单
    1月17日,首届阿里云PolarDB开发者大会在京举办,中国首款自研云原生数据库PolarDB发布“三层分离”全新版本,基于智能决策实现查询性能10倍提升、节省50%成本。面向开发者,阿里云全新推出数据库场景体验馆、训练营等系列新举措,广大开发者可率先免费体验PolarDB数据库核心特......
  • 服务器ssh连接提示 服务器拒绝了密码 再试一次
    应该是sshd的设置不允许root用户用密码远程登录 1、修改vim/etc/ssh/sshd_config 找到#Authentication:LoginGraceTime120PermitRootLoginwithoutpasswdStrictModesyes 改成 #Authentication:LoginGraceTime120PermitRootLoginyesStrictModesyes2、......
  • springboot配置分页插件pageHelper和数据库方言的几种方式
    方式一:启动类配置分页插件(Application.java)1/**2*pageHelper分页插件3*/4@Bean5publicPageHelperByMyselfpageHelper(){6PageHelperByMyselfpageHelper=newPageHelperByMyself();7Propertiesproperties=newPr......
  • 2024年1月中国数据库排行榜: OPOT 组合续写贺新年,达梦、腾讯发力迎升势
    2024年开局,墨天轮中国数据库流行度排行火热出炉,292个国产数据库齐聚榜单。整体来看,榜单前十整体变化不大,“O-P-O”格局稳固,前五位名次未发生变动。但新年伊始,各家数据库热度上升迅猛,分数差距也逐渐缩小,这微妙的波动折射出激烈的竞争态势,行业内涌动充分活力。本月排行榜解读文章......