为什么我的Ajax数据无法发送到我的SQL Server

时间:2019-07-08 13:58:58

标签: c# sql-server ajax

我正在尝试将数据从网页获取到服务器,而无需刷新页面。 js引入数据;但是,它(数据)没有到达我的数据库,我是ajax的新手,不确定自己是否正确执行了操作

JS

function insert() {
    let batchid = document.getElementById("batchID").value;
    let batchname = document.getElementById("batchName").value;
    let totenum = document.getElementById("toteNum").value;
    let flower = document.getElementById("flowers").value;
    let trim = document.getElementById("trim").value;
    let wastemat = document.getElementById("wasteMaterial").value;
    let empint = document.getElementById("empInital").value;

    let xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET", "insert.cshtml?bid=" + batchid + "&bn" + batchname + "&tn" + totenum + "&fl" + flower + "&tr=" + trim + "&wm" + wastemat + "&ei" + empint , false);
    xmlhttp.send(null);

    alert(" Record inserted successfully");
}

C#

SqlConnection con = new SqlConnection(Server);

string batchID, batchName, toteNum, flowers, trim, wasteMaterial, empInital;

public void OnGet()
{
    batchID = Request.Query["bid"].ToString();
    batchName = Request.Query["bn"].ToString();
    toteNum = Request.Query["tn"].ToString();
    flowers = Request.Query["fl"].ToString();
    trim = Request.Query["tr"].ToString();
    wasteMaterial = Request.Query["wm"].ToString();
    empInital = Request.Query["ei"].ToString();

    con.Open();

    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "INSERT INTO HarvestedCannabis VALUES ('" + batchID.ToString() + "', '" + batchName.ToString() + "', " +
                                                                    "'" + toteNum.ToString() + "', '" + flowers.ToString() + "', " +
                                                                    "'" + trim.ToString() + "', '" + wasteMaterial.ToString() + "', " +
                                                                    "'" + empInital.ToString() + "')";
cmd.ExecuteNonQuery();
con.Close();

数据没有添加到我的数据库中,我不确定丢失了什么

0 个答案:

没有答案