从Restful C#服务连接到SQL Server [英] Connecting to SQL server from Restful C# service

查看:238
本文介绍了从Restful C#服务连接到SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发与Microsoft SQL数据库连接的静态C#服务。据我了解,我应该在以下代码中连接到SQL Server:

  using System; 
使用System.Collections.Generic;
使用System.Data.Entity;
使用System.Linq;
使用System.Web;
使用System.Data;
使用System.Data.SqlClient;
命名空间Webserver.Models
{
公共类WebserverContext:DbContext
{


public WebserverContext():base( name = WebserverContext )
{

}

public System.Data.Entity.DbSet< Webserver.Models.ApiModels.SecondlyReading>其次阅读{组; }
}
}

我面临的问题是不知道该怎么做。我之前已经在C#ASP.NET中完成了数据库连接,并且该数据库连接类似于:


SqlConnection conn = new SqlConnection(数据源= localhost; +初始
目录=作者; user = myuser;密码= mypassword)


我尝试实现它,但它不起作用。我还从以下链接中引用了C#Restful Service中的数据库连接:



链接



不胜感激。

解决方案

这可能是一个有用的开始: https://msdn.microsoft.com/en -us / library / jj729737%28v = vs.113%29.aspx?f = 255& MSPPError = -2147217396 ,但可能缺少的步骤是在Web.Config中包含连接字符串。



ASP.Net实体框架将尝试连接到其Web.Config中的连接字符串名称与您的上下文名称匹配的数据库实例(例如,在上面的示例中为WebserverContext)。 / p>

在您的Web.Config中,您的ConnectionStrings元素将具有s

 < connectionStrings> 
<添加名称= WebserverContext connectionString =数据源=本地主机;初始目录= myWebserverContextDb;用户ID = someuser;密码= somepassword providerName = System.Data.SqlClient />
< / connectionStrings>

您的DbContext类构造函数需要设置相同的名称,以便它可以查找连接字符串。本教程也可能有帮助: https://www.tutorialspoint.com/entity_framework/entity_framework_dbcontext.htm


I am trying to develop a restful C# service with a connection to Microsoft SQL database. From what I understand, I am supposed to do the connection to the SQL server inside the following code:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace Webserver.Models
{
    public class WebserverContext : DbContext
    {


        public WebserverContext() : base("name=WebserverContext")
        {

        }

        public System.Data.Entity.DbSet<Webserver.Models.ApiModels.SecondlyReading> SecondlyReadings { get; set; }
    }
}

The issue that I am facing is that I am not sure how to do it. I have done db connection in C# ASP.NET before and the db connection is something like:

SqlConnection conn=new SqlConnection("Data source=localhost;"+"Initial Catalog=authors;user=myuser;password=mypassword")

I tried implementing it but it does not work. I have also referenced from the following link for db connection in C# Restful Service:

Link

Would appreciate any help.

解决方案

This may be a helpful start: https://msdn.microsoft.com/en-us/library/jj729737%28v=vs.113%29.aspx?f=255&MSPPError=-2147217396 but probably the step you are missing is having the connection string in your Web.Config.

ASP.Net Entity Framework will try to connect to a database instance whose connection string name in Web.Config matches the name of your context (e.g. WebserverContext, in your example above).

In your Web.Config, your ConnectionStrings element will have something along the following lines:

  <connectionStrings>
    <add name="WebserverContext" connectionString="Data Source=localhost;Initial Catalog=myWebserverContextDb;User ID=someuser;Password=somepassword" providerName="System.Data.SqlClient" />
  </connectionStrings>

Your DbContext class constructor needs to have that same name set so that it can look the connection string up. This tutorial might also help: https://www.tutorialspoint.com/entity_framework/entity_framework_dbcontext.htm

这篇关于从Restful C#服务连接到SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆