我在TFS中有3个不同的文件夹:表格,脚本和对象。 我想循环遍历三个文件夹以获取文件内容,并使用C#在订单表,脚本和对象中的SQL服务器中执行它们。想要进行配置。 有人可以帮我解决这个问题吗?
代码:
class Program
{
public void DownloadFiles()
{
string teamProjectCollectionUrl = "";
string serverPath = ConfigurationManager.AppSettings["key2"];
string localPath = ConfigurationManager.AppSettings["key1"];
TfsTeamProjectCollection teamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(teamProjectCollectionUrl));
VersionControlServer versionControlServer = teamProjectCollection.GetService<VersionControlServer>();
foreach (Item item in versionControlServer.GetItems(serverPath, VersionSpec.Latest, RecursionType.Full, DeletedState.NonDeleted, ItemType.File, true).Items)
{
string target = Path.Combine(localPath, item.ServerItem.Substring(2));
if (item.ItemType == ItemType.Folder && !Directory.Exists(target))
{
Directory.CreateDirectory(target);
Console.WriteLine("Created the target folder");
}
else if (item.ItemType == ItemType.File)
{
item.DownloadFile(target);
}
}
Console.WriteLine("Downloaded files from TFS");
ExecuteFiles();
}
public void ExecuteFiles()
{
var connectionString = ConfigurationManager.ConnectionStrings["Conn1"].ToString();
using (SqlConnection conn = new SqlConnection(connectionString))
{
foreach (string file in Directory.GetFiles(ConfigurationManager.AppSettings["key1"], "*.sql", SearchOption.AllDirectories))
{
string contents = File.ReadAllText(file);
Console.WriteLine(file);
Console.WriteLine(contents);
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = contents;
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
}
static void Main(string[] args)
{
Program obj = new Program();
obj.DownloadFiles();
Console.ReadLine();
}
}
配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="key1" value="C:\downloads"/>
<add key="key2" value="$/App1/Application"/>
</appSettings>
<connectionStrings>
<add name="Conn1" connectionString="Data Source=Server_Name;Initial Catalog=db_Name;Integrated Security=True"/>
</connectionStrings>
</configuration>