17906

Can someone make this a single query?

Question:

So I was just searching how to do an "INSERT INTO" query and found this:

sql="INSERT INTO Customers (ID,firstName," sql=sql & "lastName)" sql=sql & " VALUES " sql=sql & "('" & Request.Form("id") & "'," sql=sql & "'" & Request.Form("firstname") & "'," sql=sql & "'" & Request.Form("lastname") & "')"

I know it works but I want to make it a single query without all the sql=sql's

Answer1:

You took the route of a quick hack as stated in your comments by doing rhis:

sql="INSERT INTO Customers (ID,firstName,lastName) VALUES ('" & Request.Form("id") & "','" & Request.Form("fistname") & "','" & Request.Form("lastname") & "')"

Let me persist in stating that to prevent several issues (sql injection being one of them) you could leverage the use of paramterized queries.<br /> I assume you have an ADO command somewhere after your sql statement. It is much safer if you use command parameters to send parameters from the website to the query.

command.CommandText = "INSERT INTO Customers (ID,firstName,lastName) VALUES (?,?,?)" Set param = command.CreateParameter ("id", adInteger, adParamInput) param.value = Request.Form("id") command.Parameters.Append param Set param2 = command.CreateParameter ("firstname", adVarWChar, adParamInput, 50) param2.value = Request.Form("firstname") command.Parameters.Append param2 Set param3 = command.CreateParameter ("lastname", adVarWChar, adParamInput, 50) param3.value = Request.Form("lastname") command.Parameters.Append param3 command.Execute

Have a look at <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms675101%28v=vs.85%29.aspx" rel="nofollow">Command Object Parameters</a> for more background.

Answer2:

You can do like this:

string sql = string.Format("INSERT INTO Customers(Id,FirstName,LastName) VALUES({0},'{1}','{2}')", param0, param1, param2);

It Works! But be careful this way have SQL Injection issues.

Recommend

  • RESTful web services and Struts 2
  • Concurrency using Qt and SQLite databases
  • How to store custom token attribute in Lucene Index
  • how to install standard modem in windows xp
  • Difference between @injectMocks and @Autowired usage in mockito?
  • Get the file name without file extension in Vim function.
  • How to retrieve 'repository root' id/children from CMIS repository?
  • How to ignore validation annotation if field is blank?
  • Get all substrings inside parentheses in Golang regexp
  • Why shouldn't I use rabbitmq topic exchanges for everything?
  • FakeItEasy - Having an interface fake inherit from abstract while both share same interface inherita
  • Dynamic languages - which one should I choose?
  • How to compress an .APK file say by Winrar(.rar) and then convert it back to .APK?
  • Null Values in ProcedureCall
  • How to group by multiple columns and multiple values in mongodb
  • How can I iterate over Pandas pivot table? (A multi-index dataframe?)
  • Get OneDrive Item Id for Current Word doc
  • MongoDB capped collection and monotically increasing index
  • FullTextHibernateSessionProxy is not visible from class loader
  • applying background to first, and every third row in a dynamically generated table
  • How to get or calculate size of Azure File/Share or Service
  • Can I use jquery to blank textarea fields or ajax like input boxes?
  • JPA CascadeType.REMOVE not working
  • Quickly or concisely determine the longest string per column in a row-based data collection
  • How To Delete All Words After X Characters
  • Whats the right place for testhelper-classes? (phpunit/best practise)
  • GitHub default README markup
  • Laravel 4 routing not working due to .htaccess file?
  • Iron Router: How do I send data to the layout?
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • How to autopopulate a field in SugarCRM form
  • Android changing fragment order inside FragmentPagerAdapter
  • Do I need to reset a Perl hash index?
  • azure media services - The request body is too large and exceeds the maximum permissible limit
  • Use of this Javascript
  • Bug in WPF DataGrid
  • How can I use Kendo UI with Razor?
  • WPF Applying a trigger on binding failure
  • How does Linux kernel interrupt the application?
  • git trying to push non-existent file … after clearing cache