-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIndex.cshtml.cs
55 lines (49 loc) · 1.67 KB
/
Index.cshtml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using MySql.Data.MySqlClient;
namespace MySQLBackupNetCore.Demo.Pages
{
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly IConfiguration _configuration;
private readonly IWebHostEnvironment _env;
public IndexModel(ILogger<IndexModel> logger, IConfiguration configuration, IWebHostEnvironment env)
{
_logger = logger;
_configuration = configuration;
_env = env;
}
public void OnGet()
{
}
public IActionResult OnPost()
{
var path = Path.Combine(_env.ContentRootPath, "Content", "backup.sql");
//Server=localhost;Database=db_name;User=usr;Password=pwd;CharSet=utf8;convertzerodatetime=true;
var connStr = _configuration.GetConnectionString("DbConnection");
using (MySqlConnection conn = new MySqlConnection(connStr))
{
using (MySqlCommand cmd = new MySqlCommand())
{
using (MySqlBackup mb = new MySqlBackup(cmd))
{
cmd.Connection = conn;
conn.Open();
mb.ExportToFile(path);
conn.Close();
}
}
}
return Page();
}
}
}