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

时间:2022-12-04 16:29:22

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.

注释是我到目前为止所遇到的不同连接字符串。我在使用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.

我使用SQL Server Express作为我的数据库。我也尝试在构造函数中更改base的参数名称作为数据库的名称。但它没有改变任何东西。

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).

如果我可以通过SSMS访问数据库,我已经尝试过了。我能够创建表,但我无法重命名数据库(我没有重命名数据库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

UPDATE

Controller

调节器

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

2 个解决方案

#1


4  

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.

我已经从我的一个项目的Web配置文件和(应该)检查的所有字段添加了一个连接字符串。这在 标记内。您需要将其设置为默认连接,并且只能将一个连接字符串作为默认连接。

<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.

将所有内容切换到DefaultConnection,直到它正常工作,然后您可以专注于更改名称。注释掉本地数据库连接字符串。如果您需要(甚至将其保存在其他地方),甚至在测试时将它从项目中删除。

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.

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

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

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

此屏幕截图还显示了在VS中测试连接字符串的导航位置。

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

I suggest you look here:

我建议你看看这里:

How to: Access SQL Server Using Windows Integrated Security

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

What will be the connectionstring for mssql windows authentication

mssql windows身份验证的连接字符串是什么

and this:

和这个:

Connection string using Windows Authentication

连接字符串使用Windows身份验证

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.

如果您需要更多帮助,请大声喊出来。

#2


0  

Under your TedalsContext constructor, use : base(connectionStringName).

在您的TedalsContext构造函数下,使用:base(connectionStringName)。

Otherwise, the context will treat it as code first and create a TedalContext inside your SQLExpress Local DB.

否则,上下文将首先将其视为代码,并在SQLExpress Local DB中创建TedalContext。

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

#1


4  

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.

我已经从我的一个项目的Web配置文件和(应该)检查的所有字段添加了一个连接字符串。这在 标记内。您需要将其设置为默认连接,并且只能将一个连接字符串作为默认连接。

<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.

将所有内容切换到DefaultConnection,直到它正常工作,然后您可以专注于更改名称。注释掉本地数据库连接字符串。如果您需要(甚至将其保存在其他地方),甚至在测试时将它从项目中删除。

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.

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

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

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

此屏幕截图还显示了在VS中测试连接字符串的导航位置。

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

I suggest you look here:

我建议你看看这里:

How to: Access SQL Server Using Windows Integrated Security

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

What will be the connectionstring for mssql windows authentication

mssql windows身份验证的连接字符串是什么

and this:

和这个:

Connection string using Windows Authentication

连接字符串使用Windows身份验证

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.

如果您需要更多帮助,请大声喊出来。

#2


0  

Under your TedalsContext constructor, use : base(connectionStringName).

在您的TedalsContext构造函数下,使用:base(connectionStringName)。

Otherwise, the context will treat it as code first and create a TedalContext inside your SQLExpress Local DB.

否则,上下文将首先将其视为代码,并在SQLExpress Local DB中创建TedalContext。

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