【发布时间】:2020-02-13 23:10:28
【问题描述】:
您好,我正在尝试在 C# 控制台应用程序中读取 sql 脚本。抱歉,如果这是超级基本但我遇到问题,因为它生成的文件路径总是从项目的 bin 文件夹开始。
public static void ApiResources(IConfiguration config, string testUrlExtension)
{
try
{
var azureDatabaseUrl = String.Format(config["SqlDatabase:BaseUrl"], $"test{testUrlExtension}");
SqlConnectionStringBuilder connBuilder = new SqlConnectionStringBuilder();
connBuilder.DataSource = azureDatabaseUrl;
connBuilder.UserID = config["SqlDatabase:ZupaKeyReleaseUserName"];
connBuilder.Password = config["SqlDatabase:ZupaKeyReleasePassword"];
connBuilder.InitialCatalog = "zupaauthentication";
using (SqlConnection connection = new SqlConnection(connBuilder.ConnectionString))
{
using (SqlCommand command = connection.CreateCommand())
{
connection.Open();
var GetLocalPathToProject = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Split("Zupa.ReleaseDeploymentAutoConfigure")[0];
var routeToApiResourseSqlScript = $"{GetLocalPathToProject}Zupa.ReleaseDeploymentAutoConfigure\\Zupa.ReleaseDeploymentAutoConfigure\\Sql\\Scripts\\";
var apiResourcesFileName = "AddApiResorces.sql";
var fullPathToSqlScript = $"{routeToApiResourseSqlScript}{apiResourcesFileName}";
command.CommandText = File.ReadAllText(fullPathToSqlScript);
command.ExecuteNonQuery();
connection.Close();
}
}
}
catch (SqlException e)
{
Console.WriteLine(e.InnerException);
}
}
我收到的错误如下:
出了点问题,请再次尝试配置版本。 System.IO.IOException:文件名、目录名或卷标语法不正确。 :
'C:\Zupa_Source_Code\Zupa.ReleaseDeploymentAutoConfigure\Zupa.ReleaseDeploymentAutoConfigure\bin\Debug\netcoreapp3.1\file:\C:\Zupa_Source_Code\Zupa.ReleaseDeploymentAutoConfigure\Zupa.ReleaseDeploymentAutoConfigure\Sql\Scripts\AddApiResorces.sql'
正确的路径被添加到 bin 目录的末尾,即
文件:\C:\Zupa_Source_Code\Zupa.ReleaseDeploymentAutoConfigure\Zupa.ReleaseDeploymentAutoConfigure\Sql\Scripts\AddApiResorces.sql
提前致谢, 克里斯。
【问题讨论】:
-
这还真不是问问题的阶段,还是调试问题……单步执行这段代码,检查变量,找出问题,尝试修复
-
好吧,我知道问题 dot net 正在从 bin 文件夹调试项目,因此当调用 .getWrokingDirectory 时,它会将我的文件路径添加到项目的随机工作目录中它在哪里运行。
-
`file:` 对我来说看起来不正确,这可能无济于事。也许你的意思是file:///?
标签: c# sql file console-application readfile