2017-06-07 65 views
4

我有一个问题,我无法自己解决。我是新来编程,我将不胜感激,如果你能帮助我解决这个问题:
我有一个类,我想继承:如何在C#中将对象从一个类调用到另一个类#

namespace rsDeployer.Common.SQLServerCommunication   
{ 
    public class RSDatabaseConnectionCreator: LoggerBase 
    { 
     public RSProfile profile; 
     public RSDatabaseConnectionCreator(RSProfile profile) 
     { 
      this.profile = profile; 
     } 

     public SqlConnection CreateConnection(RSDatabaseNames DatabaseName, bool UseWindowsAuthentication, bool testConnection = false) 
     { 
      var connectionString = BuildRSDatabaseConnectionString(DatabaseName, UseWindowsAuthentication);    
      if (testConnection) 
      { 
       return IsConnectionAvailable(connectionString) ? new SqlConnection(connectionString) : null; 
      } 
      return new SqlConnection(connectionString); 
     } 
    } 
} 

,我想调用的CreateConnection()在另一类注入方法让我打开连接,然后执行脚本。
编辑1级我想注入它。

 public void QueryExecution(string SQLQuery) 
    { 

     //here's where I would like to inject it 
     SqlCommand command = new SqlCommand(SQLQuery, conn); 
     var file = new StreamWriter(@"D:\Project\rsDeployer\ImportedQuery.txt"); 
     file.WriteLine(command); 
     file.Close(); 
    } 

如果这个问题是愚蠢的,应该得到答案你会指出我应该阅读的方向吗?

我希望这个问题能够很好地被问到和清楚。 在此先感谢。

+1

你应该寻找一个对象的创建实例。 – Berkay

+0

你能告诉你如何期待使用它吗?消费者级别是什么样子。 – Juan

回答

2

这样,

public void QueryExecution(string SQLQuery) 
{ 
    RSProfile profile = new RSProfile(); 
    RSDatabaseConnectionCreator instance = new RSDatabaseConnectionCreator(profile); 
    SqlConnection conn = instance.CreateConnection(...); 
    SqlCommand command = new SqlCommand(SQLQuery, conn); 
    var file = new StreamWriter(@"D:\Project\rsDeployer\ImportedQuery.txt"); 
    file.WriteLine(command); 
    file.Close(); 
    conn.Close(); 
} 

还告诉你想从这个类继承,这里是另一种方法,

public class RSDatabaseConnectionCreator : LoggerBase 
{ 
    public virtual object CreateConnection() // by virtual you can override it. 
    { 
     return new object(); 
    } 
} 

public class AnotherClass : RSDatabaseConnectionCreator { 

    public AnotherClass() { 
     CreateConnection(); // by inheriting RSDatabaseConnectionCreator , you can reach public functions. 
    } 

    public override object CreateConnection() // or you can override it 
    { 
     // here might be some user Login check 
     return base.CreateConnection(); // then you open connection 
    } 
} 

希望帮助,

+0

也许使用'using conn = instance.CreateConnection();',所以连接将被自动处置... –

+0

你是对的,但这只是一个创建对象实例的示例:) @MartinVerjans – Berkay

1

这是你怎么做它。在先前的答案中对于一个重大错误道歉。这个有一个使用环绕的连接。

namespace rsDeployer.Common.SQLServerCommunication   
    { 
     public class ConsumerClass 
     { 
      public void QueryExecution(string SQLQuery) 
      { 

      var profile = new RsProfile(); 
      var rsConnectionCreator = new RSDatabaseConnectionCreator(profile); 
       using(var sqlConnection = rsConnectionCreator.CreateConnection(...Parameters here...)){ 
         SqlCommand command = new SqlCommand(SQLQuery, sqlConnection); 


       } 
       var file = new StreamWriter(@"D:\Project\rsDeployer\ImportedQuery.txt"); 
         file.WriteLine(command); 
         file.Close(); 
      } 
     } 
    } 
+0

也许使用'使用conn = instance.CreateConnection();',所以连接将自动处理... –

1

希望这是你正在请求什么

public class ClassX 
{ 
    private RSProfile _rsprofile; 
    RSDatabaseConnectionCreator _dbConnectionCreator; 
    private SqlConnection _sqlConnection; 

     public ClassX() 
     { 
      _rsProfile = xxx; // Get the RSProfile object 
      _dbConnectionCreator = new RSDatabaseConnectionCreator (_rsProfile); 
      RSDatabaseNames databaseName = yyy; // get the RSDatabaseNames 
      var useWindowsAuthentication = true; 
      var testConnection = false; 

      _sqlConnection = _dbConnectionCreator.CreateConnection(databaseName,useWindowsAuthentication ,testConnection); 
     } 

} 
+0

非常感谢你们,我非常感谢你的努力。我解决了这个问题!而我并没有因为我的问题的语法而跺脚(我是如此fab:P);) –

1

你可以通过构造函数注入连接产生者进入消费类。

public class Consumer 
{ 
    private RSDatabaseConnectionCreator _connectionCreator; 

    // Constructor injection 
    public Consumer (RSDatabaseConnectionCreator connectionCreator) 
    { 
     _connectionCreator = connectionCreator; 
    } 

    public void QueryExecution(string SQLQuery) 
    { 
     using (var conn = _connectionCreator.CreateConnection(dbName, true, true)) { 
      if (conn != null) { 
       ... 
      } 
     } 
    } 
} 

注意:using语句自动关闭连接。

使用

var connectionCreator = new RSDatabaseConnectionCreator(profile); 
var consumer = new Consumer(connectionCreator); 
consumer.QueryExecution(sqlQuery); 

如果要注入在QueryExecution每次呼叫连接产生者,可以直接注射入法作为附加参数,来代替。

public void QueryExecution(string SQLQuery, RSDatabaseConnectionCreator connectionCreator) 
{ 
    using (var conn = connectionCreator.CreateConnection(dbName, true, true)) { 
     if (conn != null) { 
      ... 
     } 
    } 
} 

使用

var connectionCreator = new RSDatabaseConnectionCreator(profile); 
var consumer = new Consumer(); 
consumer.QueryExecution(sqlQuery, connectionCreator); 
相关问题