简体   繁体   English

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

[英]How to access connection string & connect to database

So I'm working on a project involving an external API and a database. 所以我正在开发一个涉及外部API和数据库的项目。 This API uses an API specific project type called an Add In and is compiled as a library, then added to the API's source program. 此API使用称为Add In的API特定项目类型,并编译为库,然后添加到API的源程序中。 This way, I can create self-created 'actions' within in the Add in for the program and call them remotely from a MVC web project. 这样,我就可以在Add in for program中创建自己创建的“actions”,并从MVC web项目远程调用它们。 This action contains code also related to updating a database uploaded on a server. 此操作包含的代码也与更新服务器上载的数据库有关。 When I tried to call this action remotely, I would get this error within the application the add in was added to: 当我尝试远程调用此操作时,我会在添加的应用程序中将此错误添加到:

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) 

I've gotten help with my mentor and troubleshooted the connection string inside the Add Ins app.config. 我已经得到了我的导师的帮助,并对Add Ins app.config中的连接字符串进行了故障排除。 It's verified to be correct (I can access the database directly from the web MVC project). 它被验证是正确的(我可以直接从Web MVC项目访问数据库)。 I tried reading the connection string using var connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; 我尝试使用var connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;来读取连接字符串var connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; in the project, and it just can't be found. 在项目中,它是无法找到的。 When the action is called I get a null object reference error. 当调用该操作时,我得到一个空对象引用错误。

Here's my app config for the Add In: 这是我添加的应用配置:

<?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's my code related to the database inside my add In project: 这是我的add in项目中与数据库相关的代码:

//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);

  }

Using what Mason recommended below you can simply hard code your connection string instead of pulling it from the app.config if it is giving you issues.. Here is an example: 使用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);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM