恢复执行前的每个SQL语句

时间:2018-12-18 08:17:04

标签: c# ado.net .net-2.0

我的C#应用​​程序需要一些帮助。

现在它可以正常工作,但是客户的新要求之一是在程序启动之前使用任何可选的用户模式来实现基础数据库(MS SQL或Oracle)。

我的应用程序内部大约有 150个DataCommand对象

因此,我需要在应用程序内部捕获每个SQL表达式,然后再执行它,并更改目标对象的位置。例如更改:

SELECT APPLICATION_NAME FROM **MY_SCHEMA**.APPLICATIONS

SELECT APPLICATION_NAME FROM **NEW_SCHEMA**.APPLICATIONS

如我所见,这个问题的解决方案是将代码插入到应用程序中的每个IDbCommand中,但无法想象起点。

请提出任何建议。

更新

今天的解决方法:

从配置文件中读取新的架构所有者:

System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
String strSchemaOwner = ((string)(configurationAppSettings.GetValue("DatabaseOwner", typeof(string))));

每个表单都有一个由Command对象填充的数据库命令列表(类似于Form Controls)。

在表单加载事件中,我将此数组传递给确实替换每个人命令文本的方法:

foreach(IDbCommand pCommand in arrCollection)
{
    pCommand.CommandText = pCommand.CommandText.Replace("MY_SCHEMA", strSchemaOwner);
}

1 个答案:

答案 0 :(得分:0)

今天的解决方法:

从配置文件中读取新架构所有者:

System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
String strSchemaOwner = ((string)(configurationAppSettings.GetValue("DatabaseOwner", typeof(string))));

每个表单都有一个数据库命令列表(类似于表单控件),其中填充了命令对象。

FormLoad 事件中,我将此数组传递给替换每个人命令文本的方法:

foreach(IDbCommand pCommand in arrCollection)
{
    pCommand.CommandText = pCommand.CommandText.Replace("MY_SCHEMA", strSchemaOwner);
}