简体   繁体   English

C#.NET远程数据库连接

[英]C# .NET remote DB connection

So I have this Team Services shared project which I've pulled locally. 因此,我有这个Team Services共享项目,该项目已在本地进行。 I am connecting to a remote DB which requires credentials in order to do anything with it. 我正在连接到需要使用凭据才能执行任何操作的远程数据库。 Everything works fine, and my connectionString is as follows: 一切正常,我的connectionString如下:

<add name="PluginSchedulerContext"
   connectionString="metadata=res://*/PluginSchedulerDataModel.csdl|res://*/PluginSchedulerDataModel.ssdl|res://*/PluginSchedulerDataModel.msl;provider=System.Data.SqlClient;

     provider connection string=&quot;
     data source=xxx.xxx.xxx.xxx;
     initial catalog=TestDB;
     persist security info=True;
     user id=xxx;
     password=xxx;
     MultipleActiveResultSets=True;
     App=EntityFramework&quot;"

   providerName="System.Data.EntityClient" />

I was wondering if there is a smarter way to do this and to omit the password field in the string since app.config as I see is not part of the .gitignore . 我想知道是否有更聪明的方法来执行此操作并忽略字符串中的password字段,因为我所看到的app.config不属于.gitignore一部分。 (I am migrating from Laravel PHP) (我正在从Laravel PHP迁移)

The better way is that, you can change connectionstring to connect to another database (test database), then you can replace connectionstring through Replace Token task to connect to actual database when you deploy app to the server through build or release . 更好的方法是,当您通过buildrelease将应用程序部署到服务器时,可以更改connectionstring以连接到另一个数据库(测试数据库),然后可以通过Replace Token任务替换connectionstring以连接到实际数据库。

If you just want to use an database, can specify connectionstring programmatically. 如果只想使用数据库,可以以编程方式指定连接字符串。 (Encrypt password, then store the encrypted password to your app, then decrypt password and combine connectionstring with actual password and specify it to entity context. The decrypt local could be in additional assembly.) (加密密码,然后将加密的密码存储到您的应用中,然后解密密码,并将连接字符串与实际密码结合在一起,然后将其指定给实体上下文。解密本地地址可能在其他程序集中。)

var encryptedPassword="xxx";
var actualPassword=[decrypt password];
var connstring = [combine connectionstring with actual password];
var estringnew = new EntityConnectionStringBuilder(connstring);
estringnew.Metadata = XXX
var context = new BAEntities(estringnew.ToString());
var query =
    from con in context.Contacts
    where con.Addresses.Any((a) => a.City == "Seattle")
    select con;

More information, you can refer to this article. 更多信息,你可以参考这个文章。 For this way, developers can debug application to know actual password. 这样,开发人员可以调试应用程序以了解实际密码。

If you don't want to let developers to know the actual password, you can encrypt connectionstring 如果您不想让开发人员知道实际的密码,则可以加密连接字符串

If you use Windows Authentication (client and database access using same Windows login) you don't need the username and password in your source code. 如果您使用Windows身份验证(使用相同的Windows登录名访问客户端和数据库),则不需要源代码中的用户名和密码。

Or you'll need to edit the configuration when you deploy. 或在部署时需要编辑配置。

Otherwise the credentials are in your project's configuration in your VCS. 否则,凭据将在VCS中的项目配置中。

Try to use the Entity Framework. 尝试使用实体框架。 It's much easier! 容易得多! https://www.asp.net/entity-framework https://www.asp.net/entity-framework

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

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