c# - How to use multiple parameters in a SQL queries -
i came across statement on how prevent sql injection, changed code (commented out old codes):
namee = txtname.text; //sqlcode = "select * [db].[dbo].[tablepdftest] [name] = '" + namee + "'"; sqlcode = "select * [db].[dbo].[tablepdftest] [name] = @name"; using (sqlcommand command = new sqlcommand(sqlcode, conn)) { //command.commandtype = commandtype.text; command.parameters.addwithvalue("name", namee); using (reader = command.executereader()) { // action goes here... } }
how can same multiple parameters?
my code using function padding 2 parameters variable function:
public void writedata(string k, string c) { conn = new sqlconnection(cstring); conn.open(); //messagebox.show(k); //messagebox.show(c); var pdfpath = path.combine(server.mappath("~/pdftemplates/fw9.pdf")); // form fields pdf , fill them in! var formfieldmap = pdfhelper.getformfieldnames(pdfpath); //if more multiple entries, verify name , last 4 ssn //sqlcode = "select * [db].[dbo].[tablepdftest] [name] = '" + k + "' , [ssn3] = " + c + ""; sqlcode = "select * [db].[dbo].[tablepdftest] [name] = @name2 , [ssn3] = @ssnnum"; //messagebox.show("" + sqlcode.tostring()); using (sqlcommand command = new sqlcommand(sqlcode, conn)) { //command.commandtype = commandtype.text; command.parameters.addwithvalue("name2", k); command.parameters.addwithvalue("ssnnum", c); using (reader = command.executereader()) { if (reader.hasrows) { if (reader.read()) { messagebox.show(reader.getvalue(0).tostring()); /*formfieldmap["topmostsubform[0].page1[0].f1_01_0_[0]"] = reader.getvalue(0).tostring(); formfieldmap["topmostsubform[0].page1[0].f1_02_0_[0]"] = reader.getvalue(1).tostring(); formfieldmap["topmostsubform[0].page1[0].f1_04_0_[0]"] = reader.getvalue(2).tostring(); formfieldmap["topmostsubform[0].page1[0].f1_05_0_[0]"] = reader.getvalue(3).tostring(); formfieldmap["topmostsubform[0].page1[0].f1_07_0_[0]"] = reader.getvalue(4).tostring(); formfieldmap["topmostsubform[0].page1[0].social[0].textfield1[0]"] = reader.getvalue(5).tostring(); formfieldmap["topmostsubform[0].page1[0].social[0].textfield2[0]"] = reader.getvalue(6).tostring(); formfieldmap["topmostsubform[0].page1[0].social[0].textfield2[1]"] = reader.getvalue(7).tostring(); formfieldmap["topmostsubform[0].page1[0].social[0].textfield2[2]"] = reader.getvalue(8).tostring(); formfieldmap["topmostsubform[0].page1[0].social[0].textfield2[3]"] = reader.getvalue(9).tostring();*/ } } } } // requester's name , address (hard-coded) //formfieldmap["topmostsubform[0].page1[0].f1_06_0_[0]"] = "medical group\n27 west ave\npurchase, ny 10577"; //var pdfcontents = pdfhelper.generatepdf(pdfpath, formfieldmap); //pdfhelper.returnpdf(pdfcontents, "completed-w9.pdf"); }
you can add parammeter did before. how code loke like:
sqlcode = "select * [db].[dbo].[tablepdftest] [name] = @name , [ssn3] =@ssn3"; using (sqlcommand command = new sqlcommand(sqlcode, conn)) { //command.commandtype = commandtype.text; command.parameters.addwithvalue("@name", namee); command.parameters.addwithvalue("@ssn3", c); using (reader = command.executereader()) { // action goes here... } }
Comments
Post a Comment