且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法在C#来访问远程数据库

更新时间:2022-11-25 11:26:47

我已经添加了从我的项目之一的Web配置文件,并且(应该)被检查的所有字段的连接字符串。这是&LT内,是connectionStrings> 标记。你需要让你的默认连接,你只能有一个连接字符串作为默认连接。

 <添加名称=DefaultConnection的providerName =System.Data.SqlClient的
    的connectionString =数据源= Providedbyhost;综合
    安全=假;初始目录= DB_NAME;用户ID =用户ID,密码=密码;
    Trusted_Connection = FALSE; TrustServerCertificate = TRUE;
    加密= TRUE; MultipleActiveResultSets = TRUE/>

如果你有你的数据库环境:

 公共TedalsContext()
    :基地(DefaultConnection)

切换一切DefaultConnection,直到你得到它的工作,那么你就可以专注于更改名称。注释掉本地数据库连接字符串。如果需要,甚至从项目中删除它(并保存在其他地方),而你正在测试这一点。

我还添加了我的文件夹目录的屏幕截图,显示我修改的Web配置文件夹中。看到它的蓝色突出显示。

无法在C#来访问远程数据库

另外这个屏幕快照显示你在哪里可以导航到测试VS.内连接字符串

无法在C#来访问远程数据库

我建议你看看这里:

如何:访问SQL Server使用Windows集成安全性

什么将是MSSQL Windows身份验证的ConnectionString

和这样的:

Connection使用字符串的Windows身份验证

我在讨论建议。我还建议联系网络托管服务提供商,我以前也有这个问题,它不会连接,它已经在主机端的问题。

如果您需要更多的帮助,喊出来。

i have changed the connection string to point to a database in the remote server. But when I execute the project the program still points to the local db.

<entityFramework>
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>


<connectionStrings> 
<add name="TEDALS_Ver01.DAL.TedalsContext" 
  connectionString="Data Source=FE0VMC0643\SQLEXPRESS;
  AttachDbFilename=|DataDirectory|\TeDaLSdev.mdf;
  Integrated Security=False" 
  providerName="System.Data.SqlClient" 
/>
<!--<add
   name="TEDALS_Ver01.DAL.TedalsContext"
   connectionString="Server=FE0VMC0643; Database=TeDaLSdev; "
   providerName="System.Data.SqlClient" />-->

<!--<add name="TEDALS_Ver01.DAL.TedalsContext"
     providerName="System.Data.SqlClient" 
     connectionString="Server=FE0VMC0643;Data Source=FE0VMC0643;
     Initial Catalog=TeDaLSdev;
     Integrated Security=True;
     Connect Timeout=15;
     Encrypt=False;
     TrustServerCertificate=False;" />-->

</connectionStrings> 

Comments are the different connection strings i have treid so far. i never had a connections string when I was using the LocalDB.

Constructor for Connection

public class TedalsContext : DbContext
{
    public TedalsContext()
        : base("TedalsContext")
    {
        //Database.SetInitializer<TedalsContext>(null);
    }
}

i am using SQL Server Express as my database. I have also tried changing the name of the parameter for base in constructor as the name of the Database. But it did not change anything.

I have already tried if I have access to the database through SSMS. I am able to create tables but I am unable to rename the database as such(I do not have access rights to rename the database TeDalSdev).

Are there any other work around i could try? Should the name of the remote database and the local database should be the same to avoid changing a lot of code?

UPDATE

Controller

public class LsystemFamiliesController : Controller
{
    private TedalsContext db = new TedalsContext();
    //Code goes here
}

I've added a connection string from a web config file of one of my projects and all the fields that (should) be checked. This is within the <connectionStrings> tags. You need to make it your default connection and you can only have one connection string as the default connection.

<add name="DefaultConnection" providerName="System.Data.SqlClient" 
    connectionString="Data Source=Providedbyhost;integrated 
    security=false;Initial Catalog=DB_Name;User Id=UserId;Password=password;
    Trusted_Connection=false; TrustServerCertificate=true;
    Encrypt=true;MultipleActiveResultSets=True" />

Where you have your DB Context:

public TedalsContext()
    : base("DefaultConnection")

Switch everything to DefaultConnection, until you get it working, then you can focus on changing names. Comment out your local db connection strings. Even remove it from the project if you need to (and save it elsewhere), while you are testing this.

I've also added a screen shot of the my folder directory, to show which web config folder I am modifying. See it's highlighted in blue.

Also this screen shot shows you where to navigate to test your connection string within VS.

I suggest you look here:

How to: Access SQL Server Using Windows Integrated Security

What will be the connectionstring for mssql windows authentication

and this:

Connection string using Windows Authentication

As I suggested in the discussion. I recommend also contacting web hosting provider, as I have had this problem where it will not connect and it's been a problem at the hosting end.

If you need more help, shout out.