我是Visual Studio的新手.我正在创建一个登录表单.
我有这个代码.
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
try
{
using (OdbcConnection connect = new OdbcConnection(connectionString))
{
connect.Open();
OdbcCommand cmd = new OdbcCommand("SELECT username, password FROM receptionist", connect);
OdbcDataReader reader = cmd.ExecuteReader();
if (username_login.Text == username && password_login.Text == password)
{
this.Hide();
MessageBox.Show("Invalid User", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
else
MessageBox.Show("Invalid User", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
connect.Close();
}
}
catch (OdbcException ex)
{
MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Run Code Online (Sandbox Code Playgroud)
但每当我尝试输入用户名和密码时,都会出现一个错误,称为配置系统无法初始化.我只是想知道这是什么问题,我怎么能解决这个问题?
请帮忙.
我在NET Core2.0 App中有以下类.
// required when local database does not exist or was deleted
public class ToDoContextFactory : IDesignTimeDbContextFactory<AppContext>
{
public AppContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<AppContext>();
builder.UseSqlServer("Server=localhost;Database=DbName;Trusted_Connection=True;MultipleActiveResultSets=true");
return new AppContext(builder.Options);
}
}
Run Code Online (Sandbox Code Playgroud)
当Database不存在时,在Core 2.0中需要进行迁移,并且必须在运行update-database时创建.
升级到ASP.NET Core 2.0后无法创建迁移
我想在2个地方(这里和appsettings.json)没有ConnectionString,但只在.json,所以我试图更换
"Server=localhost;Database=DbName;Trusted_Connection=True;MultipleActiveResultSets=true"
Run Code Online (Sandbox Code Playgroud)
同
ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我得到空值.
更新1:
请注意,在Core 2中明确添加.json是不必要的,所以问题不在于文件.
https://andrewlock.net/exploring-program-and-startup-in-asp-net-core-2-preview1-2/
更新2:
此外,我已经使用Configuration从.json向Context发送ConnectionString:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); …Run Code Online (Sandbox Code Playgroud) 我创建了一个.Net Core API,我引用了一个.Net框架应用程序.引用的Application连接到数据库,其连接字符串存储在web.config文件中:
string CONNSTR =ConfigurationManager.ConnectionStrings["SHOPPINGCNN"].ConnectionString;
Run Code Online (Sandbox Code Playgroud)
.Net Core应用程序使用appsettings.json而不是web.config文件.当我运行API并尝试使用引用的应用程序资源时,上面的代码将被触发并返回null,因为.net Core应用程序中不存在web.config文件.解决此问题的最佳解决方案是什么
我正在将我们的 Visual Studio 解决方案之一迁移到 .Net Core 5。它有两个项目:Visual Basic 中的 WinForms 应用程序(旧版)和 C# 中的类库。
我注意到为 WinForms 应用程序生成的 .config 文件的名称与以前的版本不同:以前是 {WinForms 应用程序名称}.exe.config,现在是 {WinForms 应用程序名称}.dll.config。
我尚未找到有关此名称更改的文档。
我担心的是我们的版本更新过程。在我们软件的每个新版本中,我们都会向 IT 操作人员提供 .exe 和 .dll 文件,并由他们进行设置以供使用。如果 .config 文件发生更改,我们还会为其提供注释,告诉他们要添加、删除或更新哪些节点。
现在,我不知道可执行文件是否可以与旧名称格式的配置文件一起正常工作。所以我进行了实验。首先,我更改 .dll.config 文件中某个键的值并直接启动 .exe。通常情况下,这些变化都会被考虑在内。然后我再次将 .dll.config 文件重命名为 .exe.config,并重新启动 .exe。这次,即使我再次更改 .exe.config 文件,应用程序也会使用初始值。
我还尝试手动重命名配置文件:简单地(并且可预见地)构建解决方案会创建一个新的 .dll.config 文件。
我正在编写一个.NET Core控制台应用程序(不是 ASP.NET Core Web应用程序)。在.NET Framework中,我将有一个App.config和App.Debug.config和一个App.Release.config文件,后者2用于转换前者。这使我可以根据创建调试还是发布版本来更改各种设置。
现在,使用.NET Core,您可以appsettings.json改用。很好,但是我该如何调试和发布转换呢?我已经读过一些有关使用环境变量来确定您是处于发行版环境还是调试环境的信息,但我确实不想这样做,并且它实际上也不适合控制台应用程序。那么,如何以与.NET Framework中类似的方式转换配置文件?
使用.Net Framework 时,我们有以下几行app.config:
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
</startup>
Run Code Online (Sandbox Code Playgroud)
我想知道在移植到.NET Core时我们应该如何处理这些行?
我们应该删除它们还是用其他东西替换它们?
我刚刚开始学习ASP.NET Core 2 MVC应用程序。
接受新项目后,在解决方案资源管理器中看不到任何web.config文件。
有什么帮助吗?
抱歉,这是一个愚蠢的问题。
我正在编写一个.Net Core C#控制台程序.在测试时,使用项目属性中定义的环境变量很方便:
我写了这样简单的代码:
string rdsDatabase = System.Environment.GetEnvironmentVariable("RDSDatabase");
Run Code Online (Sandbox Code Playgroud)
我刚刚了解到没有.exe创建,你必须使用"DotNet"来运行.net核心控制台程序(https://blogs.msdn.microsoft.com/benjaminperkins/2017/03/07/net-core -application-where-my-exe-how-to-publish /).在那种情况下,我在哪里将这些环境变量放在运行时?我知道有一些替代方法,如本文所述:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration?tabs=basicconfiguration.我可以保持简单并使用GetEnvironmentVariable吗?
c# ×4
.net-core ×3
asp.net-core ×2
.net ×1
.net-5 ×1
app-config ×1
appsettings ×1
login ×1
mysql ×1
web-config ×1
winforms ×1