c# - 如何访问连接字符串并连接到数据库

标签 c# sql-server entity-framework api connection-string

所以我正在从事一个涉及外部 API 和数据库的项目。此 API 使用称为加载项的 API 特定项目类型并编译为库,然后添加到 API 的源程序中。这样,我可以在程序的加载项中创建自己创建的“ Action ”,并从 MVC 网络项目远程调用它们。此操作包含还与更新上传到服务器的数据库相关的代码。当我尝试远程调用此操作时,我会在添加插件的应用程序中收到此错误:

A network-related or instance-specific error occurred while establishing a connection to SQL server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) 

我在导师的帮助下解决了加载项 app.config 中的连接字符串问题。它被验证是正确的(我可以直接从 web MVC 项目访问数据库)。我尝试在项目中使用 var connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; 读取连接字符串,但找不到。调用操作时,我收到空对象引用错误。

这是我的插件应用配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=PROD4W\PROD4W;Initial Catalog=EplanInterfaceDb;user id = SomeID; password=SomePassword;Integrated Security=False" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

这是我添加项目中与数据库相关的代码:

//Here is where my mentor tried to pull the connection string from the //app.config.This is where the object reference error
var connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
//Output the connection string to the screen on outside app
new Decider().Decide(EnumDecisionType.eOkDecision, "Connection: " + connectionString, "", EnumDecisionReturn.eOK, EnumDecisionReturn.eOK);
//Access the database and add a test
using (var context = new DataContext())
  {
    //Throws network related error mentioned above
    context.Macros.Add(new Macro { Name = "test" });                    
    context.SaveChanges();
    //Output to the screen if the savechanges was successful
    new Decider().Decide(EnumDecisionType.eOkDecision, "Save Changes Called ", "", EnumDecisionReturn.eOK, EnumDecisionReturn.eOK);

  }

最佳答案

使用下面 Mason 推荐的内容,您可以简单地对连接字符串进行硬编码,而不是在出现问题时将其从 app.config 中拉出。这是一个示例:

var connectionString = "Hard code connection string";
//Make sure your DataContext has a constructor that takes in a connection string
using (var context = new DataContext(connectionString)) 
  {
    context.Macros.Add(new Macro { Name = "test" });                    
    context.SaveChanges();
    //Output to the screen if the savechanges was successful
    new Decider().Decide(EnumDecisionType.eOkDecision, "Save Changes Called ", "", EnumDecisionReturn.eOK, EnumDecisionReturn.eOK);
  }

关于c# - 如何访问连接字符串并连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57062161/

相关文章:

sql-server - 为什么 SSIS vb 脚本在 VS 中执行时正确检测到文件存在,而不是从 SQL Server 中执行

sql - 防止 MS-SQL 表中的循环引用

c# - 如何使用和 C# 更新集合中的项目

C# Entityframework - 如何将导航属性从 HashSet 转换为 SortedSet

c# - 是否可以在 C# 中的方法中定义局部结构?

c# - OrderedDictionary、ListDictionary 和 HybridDictionary 需要什么?`

SQL 排序顺序,最后为空值

c# - 找不到 Entity Framework ADO.NET Sql.Data.Client 提供程序

c# - Apple 的 Windows 版 Bonjour SDK

c# - 使用 Google Drive Api v2 选择特定字段