Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用HTTP Post从excel在SQL Server数据库中插入数据_C#_Asp.net_Excel_Entity Framework_Http Post - Fatal编程技术网

C# 使用HTTP Post从excel在SQL Server数据库中插入数据

C# 使用HTTP Post从excel在SQL Server数据库中插入数据,c#,asp.net,excel,entity-framework,http-post,C#,Asp.net,Excel,Entity Framework,Http Post,我想在excel中单击“插入”按钮时将数据插入SQL Server数据库 数据位于单元格A2和B2中,以下是excel中“插入”按钮后面的代码: Dim HttpReq As New WinHttp.WinHttpRequest HttpReq.Open "POST", "http://localhost:11121/Student/Insert/", False HttpReq.Send "jsmith112" 以下是我在VS中控制器操作的代码: [HttpPost] publ

我想在excel中单击“插入”按钮时将数据插入SQL Server数据库

数据位于单元格A2和B2中,以下是excel中“插入”按钮后面的代码:

Dim HttpReq As New WinHttp.WinHttpRequest

HttpReq.Open "POST", "http://localhost:11121/Student/Insert/", False

HttpReq.Send "jsmith112"
以下是我在VS中控制器操作的代码:

 [HttpPost]
    public ActionResult Insert(string id)
    {

        try
        {

            student.AddToStudents(id);
            student.SaveChanges();

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
这似乎不起作用,谁能指导我完成这件事


提前感谢

我认为您的控制器操作不会看到这种情况下的数据。它无法知道您发送的“jsmith112”是否应该与string id参数相对应。在控制器操作内部,使用Request.InputStream对象获取发布的数据并将其发送到数据库中

一种更好的方法是将其作为url编码的表单数据发送(因此post正文将为'id=jsmith112'),或者将请求更改为GET(或者PUT,如果您希望正确地使用RESTful),然后点击以下url:

http://localhost:11121/Student/Insert/jsmith112
在这种情况下,它应该由string id参数提取


另外,在控制器操作中放置一个断点,以确保您实际命中了它,然后使用调试器验证您的web服务是否具有所需的数据。

只需将POST更改为

"http://localhost:11121/Student/Insert/" + id
然后绘制路线图

    routes.MapRoute(
        "InsertStudent",
        "Student/Insert/{id}",
        new { controller = "Student", action = "Insert", id = "" }
    );
然后在控制器中,您可以检查id是否为空,如果不是,则创建一个新用户

还有,有时候我会做这样的事情

    routes.MapRoute(
        "Resource",
        "{resource}/{operation}/{id}",
        new { controller = "Resource", action = "Index", resource = "", operation = "" id = "" }
    );
然后你就可以分析出这些东西是什么


作为一个旁注,如果你为你的服务做了更多的结束,你可以考虑使用get、POST、PUT、Delphi,而不是实际上在URI中有“INSERT”。休息。

什么不起作用?这是一个错误吗?它甚至会击中你的插入方法吗?故障发生在哪里?您正在尝试对SQL Server执行HTTPPost?这是真的吗?他正在向一个ASP.NET MVC web服务发送HTTP POST,该服务反过来使用EF更新数据库。感谢Evan的帮助,所以现在我在Excel中“插入”按钮后面的代码是:Dim HttpReq As New WinHttp.WinHttpRequest HttpReq.Open“POST”,False HttpReq.Send“id=jsmith112”如何使用Request.InputStream获取发布的数据并将其插入数据库?在本例中,您将其视为表单数据,因此它应显示为Request.form集合的成员。您可能需要使用HttpReq.SetRequestHeader来告诉它将内容类型头设置为“application/x-www-form-urlencoded”,谢谢!再一次。现在我可以将数据从Excel发布到ASP.NET MVC web服务。下面是我的代码:Sub SendData()Dim HttpReq As Object,url As String Set HttpReq=CreateObject(“MSXML2.ServerXMLHTTP”)url=”“HttpReq.Open“POST”,url,False HttpReq.SetRequestHeader“Content Type”,“application/x-www-form-urlcoded”HttpReq.Send“Student=jsmith112”Debug.Print HttpReq.ResponseText End-Now,我想要一个ControllerAction“Insert”,它将在SQLServer数据库中插入发布的数据。我需要做什么来执行此控制器操作?您已经在上面给出了控制器操作。代码没有命中它吗?您可以调试控制器操作吗?我在以下行中收到错误:“student.AddToStudents(id);”错误消息:错误:“STU.Models.StudentEntities.AddToStudent(STU.Models.student)”的最佳重载方法匹配具有一些无效参数。是因为我在“AddToStudent”中传递字符串,而不是“实体”吗?