简体   繁体   English

如何使用C#代码导出表的'Sql文件'

[英]How to export 'Sql File' of a Table using C# code

I have developed a school management system, which is connected to a database. 我已经开发了一个学校管理系统,该系统连接到数据库。 Now, I want to take backup of tables. 现在,我要备份表。

My idea is to generate an SQL file of each table that will later be used for backup. 我的想法是为每个表生成一个SQL文件,以后将用于备份。 I achieved this goal manually in Oracle SQL Developer (attached a screen shot), first exporting the SQL file and then importing those files. 我在Oracle SQL Developer中手动实现了此目标(如屏幕快照所示),首先导出SQL文件,然后导入这些文件。 Now I want to do this programmatically using C#. 现在,我想使用C#以编程方式进行此操作。 I have searched a lot on Google, but found nothing useful. 我在Google上进行了很多搜索,但没有发现任何有用的信息。

oracle sql开发人员的屏幕截图

Basically, if you want to take full backup including table data, stored proc, views etc., You could go for SQL script 基本上,如果要进行完整备份,包括表数据,存储的proc,视图等,则可以使用SQL脚本

BACKUP DATABASE School_Management TO DISK = 'D:\SQL Backup\SchoolManagement\SM_20180314.bak'    WITH FORMAT,  
  MEDIANAME = 'SM_Backups',  
  NAME = 'Full Backup of School_Management';  

You could run the above script from your C# code, passing the backup path as a parameter. 您可以通过C#代码运行上述脚本,并将备份路径作为参数传递。 Let me know if you need more detail 让我知道您是否需要更多细节

Although NOT a proper backup, a simple EXP utility (yes, the original export, not Data Pump) might do the job just fine. 尽管不是适当的备份,但是简单的EXP实用程序(是, 原始导出,而不是Data Pump)可能做得很好。

Why the original EXP? 为什么是原始 EXP? Because you can run it on your computer, it will export the whole user (tables, procedures, packages, ...) and create a file on your computer. 因为您可以在计算机上运行它,所以它将导出整个用户(表,过程,程序包等)并在计算机上创建文件。 Data Pump, a modern export utility is much more powerful, but - in order to use it - you have to have access to the database server (ie its directory which stores exported files. Read more about all that stuff in documentation). Data Pump是一种现代的导出实用程序,功能强大得多,但是-要使用它,您必须有权访问数据库服务器(即,其目录中存储了导出的文件。有关文档的所有内容,请阅读更多信息)。

So, a small demonstration of how it looks like - I'm going to export my SCOTT user: 因此,对它的外观进行了小幅演示-我将导出我的SCOTT用户:

c:\Temp>exp scott/tiger@xe file=scott.dmp

Export: Release 11.2.0.2.0 - Production on Sri O×u 14 20:13:30 2018

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.


Connected to: Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
Export done in EE8MSWIN1250 character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user SCOTT
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user SCOTT
About to export SCOTT's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definitions
. about to export SCOTT's tables via Conventional Path ...
. . exporting table                           DEPT          4 rows exported
. . exporting table                            EMP         12 rows exported

<snip>

. exporting synonyms
. exporting views
. exporting stored procedures
. exporting operators
. exporting referential integrity constraints
. exporting triggers
. exporting indextypes
. exporting bitmap, functional and extensible indexes
. exporting posttables actions
. exporting materialized views
. exporting snapshot logs
. exporting job queues
. exporting refresh groups and children
. exporting dimensions
. exporting post-schema procedural objects and actions
. exporting statistics
Export terminated successfully with warnings.

c:\Temp>

c:\Temp>dir scott.dmp
 Volume in drive C is OSDisk
 Volume Serial Number is 7635-F892

 Directory of c:\Temp

14.03.2018.  20:13         1.310.720 scott.dmp
               1 File(s)      1.310.720 bytes
               0 Dir(s)  346.085.478.400 bytes free

c:\Temp>

The resulting .DMP file can now be transferred to some other database, imported (using the IMP command) and you'll have it up & running in a matter of minutes (OK, that depends on whether the environment on the new database is friendly to user you imported - for example, non-existing users you access via database links, etc. might spoil the joy, but that's another story). 现在可以将生成的.DMP文件传输到其他数据库,导入(使用IMP命令),然后在几分钟之内启动并运行它(确定,这取决于新数据库上的环境是否友好)对于您导入的用户-例如,您通过数据库链接访问的不存在的用户等可能会破坏喜悦,但这是另外一回事了。

Apart from that, investigate usage of RMAN (Recovery MANager): 除此之外,请调查RMAN(恢复管理器)的用法:

Recovery Manager (RMAN) is an Oracle Database client that performs backup and recovery tasks on your databases and automates administration of your backup strategies. Recovery Manager(RMAN)是一个Oracle数据库客户端,可以在数据库上执行备份和恢复任务,并自动管理备份策略。 It greatly simplifies backing up, restoring, and recovering database files. 它大大简化了备份,还原和恢复数据库文件。

which is a proper way to backup your database; 这是备份数据库的正确方法; though, might be an overkill if you just want to "save" one user's objects. 但是,如果您只想“保存”一个用户的对象,则可能是一个过大的杀伤力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM