如何使用ReactJS插入SQL Server

2lpgd968  于 2022-12-28  发布在  React
关注(0)|答案(1)|浏览(172)

我试着用下面的代码插入到SQL Server中,但是它不起作用。如果我手动地给出值而不是?,那么它就可以正常工作。

app.post("/outwrkwip/insert",(req,res)=>{

    sql.connect(config,function(err){

        if(err){
            console.log(err);
        }

        var request = new sql.Request();

        var values = ["MTRT421PV42FOG","11-MAY-22","4249.1","MTBI","16954073"];

        var sqlqry = `INSERT INTO dbo.outworkwip_Tb_KK (Order_Number,prod_Target_Date,Line_No,Order_Type,Batch_Number)  VALUES (?,?,?,?,?);`                        

        request.query(sqlqry, [values], (err,recordset)=>{           
                
            if(err){                
                console.log(err)
            } else{
                res.send({message : "Successfully Data Inserted"});
            }
        });
    });

});
njthzxwz

njthzxwz1#

下面说代码工作正常...

app.post("/outwrkwip/insert",(req,res)=>{

    sql.connect(config,function(err){

        if(err){
            console.log(err);
        }

        var request = new sql.Request();       

        
        request.input('Order_Number', sql.VarChar, 'MTRT421PV42FOG');
        request.input('prod_Target_Date', sql.VarChar, '11-MAY-22');
        request.input('Line_No', sql.VarChar, '4249.1');
        request.input('Order_Type', sql.VarChar, 'MTBI');
        request.input('Batch_Number', sql.VarChar, '16954073');

        let stmt1 =  `INSERT INTO dbo.outworkwip_Tb_KK(Order_Number,prod_Target_Date,Line_No,Order_Type,Batch_Number)  
        VALUES (@Order_Number,@prod_Target_Date,@Line_No,@Order_Type,@Batch_Number);`

        
        request.query(stmt1, (err, results) => {

            if (err) {
              return console.error(err.message);
            }

            res.send({ message : "Successfully Registered"}); 

        });
    });

});

相关问题