一个表单要显示,但代码隐藏在两个不同的地方 [英] One form to be displayed, but code behind in two separate places
问题描述
我正在使用网络服务,我想将来自该服务的数据添加到数据库中。
I'm using a webservice and I want to add the data, coming in from that service, to a database.
---
所以"步骤1"是从我已经拥有代码的webservice获取所有数据。此外,在此步骤中,我需要将所有数据插入数据库。
So "step 1" is to get all the data from the webservice which I already have the code for. Also in this step I need to insert all the data into a database.
然后在"步骤2"中。我想从数据库中获取所有数据并将其添加到GridView中以显示在表单上。
Then in "step 2" I want to get all the data from the database and add it to a GridView to be displayed on the form.
---
所以所有处理网络服务数据的代码是很多代码。
So all the code from processing the webservice data is a lot of code.
是否可以拥有"步骤1"。在一个文件中,例如"Form1.cs"和"步骤2"。在"Form2.cs"中?
Is it possible to have "step 1" in one file like "Form1.cs", and "step 2" in "Form2.cs"?
我仍然只需要一个Form1.cs [Design]文件。
I still only need one Form1.cs[Design] file.
我想这不会是这是正确的方法,但只需要问。
I guess this wouldn't be the right way to do it, but just need to ask.
否则,我可以采取另一种方式来做另类课程吗?
Otherwise could I do it another way like having another class or something?
最重要的是以某种方式分离代码,以便在"步骤2"中进行分离。我只关注与从数据库中检索数据相关的方法。
The most important thing is to have the code separated somehow, so that in "step 2" I could focus only at the methods related to retrieving the data from the database.
推荐答案
是的,你可以做的简单方法,使用Partial关键字创建两个不同的同类文件
Yes ,simple way you can do , create two different file with same class using Partial keyword
Form1.CS
public partial
class MyClass
{
public
void ReadDataFromWS()
; {
}
}
DB.cs
public 部分
class MyClass
{
  public
void ReadDataFromDB()
; {
}
}
public partial
class MyClass
{
public
void ReadDataFromWS()
{
}
}
DB.cs
public partial
class MyClass
{
public
void ReadDataFromDB()
{
}
}
这篇关于一个表单要显示,但代码隐藏在两个不同的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!