使用HTTP POST从Excel中插入SQL Server数据库的数据 [英] Insert data in SQL Server database from excel using HTTP Post

查看:116
本文介绍了使用HTTP POST从Excel中插入SQL Server数据库的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据插入到SQL Server数据库,当我单击Excel插入按钮。

I want to insert data into SQL Server database when I click "Insert" button in excel.

中的数据是在单元格A2和B2,这里是背后Excel中的插入按钮,code:

The data is in Cells A2 and B2 and here is the code behind the "Insert" button in excel:

Dim HttpReq As New WinHttp.WinHttpRequest

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

HttpReq.Send "jsmith112"

下面是我的code在VS控制器动作:

Here is my code for the Controller action in VS:

 [HttpPost]
    public ActionResult Insert(string id)
    {

        try
        {

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

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

这似乎并不奏效,任何人都可以引导我进入完成这个?

This doesn't seem to be working, could anyone guide me into finishing this?

在此先感谢

推荐答案

就在POST更改为

"http://localhost:11121/Student/Insert/" + id

然后映射路径

    routes.MapRoute(
        "InsertStudent",
        "Student/Insert/{id}",
        new { controller = "Student", action = "Insert", id = "" }
    );

,然后在你的控制器,你可以检查是否ID是空的,如果没有那么做一个新用户

and then in your controller you can check if id is empty, if not then make a new user

此外,有时我做这样的事情。

also, sometimes I do things like this

    routes.MapRoute(
        "Resource",
        "{resource}/{operation}/{id}",
        new { controller = "Resource", action = "Index", resource = "", operation = "" id = "" }
    );

然后你可以解析出的东西是什么。

and then you could parse out what the things are..

作为一个侧面说明,如果你正在为你服务使更多的终点,你可以考虑使用GET,POST,PUT,DELETE,而不是实际上有URI中的插入。 REST。

as a side note and if you were making more end points for your service, you might consider using GET, POST, PUT, DELETE instead of actually having "Insert" in the URI. REST.

这篇关于使用HTTP POST从Excel中插入SQL Server数据库的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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