Functions and usage of SqlParameter

Source: Internet
Author: User

Generally, if you do not useSqlParameterWhen the entered SQL statement is ambiguous, for example, if the string contains single quotes, the program will encounter errors, and others can easily concatenate SQL statements to launch injection attacks.

 
 
  1. String SQL = "update Table1 set name = 'pudding' where ID = '1'"; // SqlParameter is not used
  2. SqlConnection conn = new SqlConnection ();
  3. Conn. connectionString = "Data Source =. \ SQLExpress; Integrated Security = true; AttachDbFilename = | DataDirectory | \ Database. mdf; User Instance = true "; // The connection string is related to the database.
  4. SqlCommand cmd = new SqlCommand (SQL, conn );
  5. Try
  6. {
  7. Conn. Open ();
  8. Return (cmd. ExecuteNonQuery ());
  9. }
  10. Catch (Exception)
  11. {
  12. Return-1;
  13. Throw;
  14. }
  15. Finally
  16. {
  17. Conn. Close ();
  18. }

The preceding Code does not use SqlParameter. In addition to security issues, this method cannot solve binary stream updates and file fragments. SqlParameter can be used to solve the above problem. There are two common methods to use: Add method and AddRange method.

I. Add Method

 
 
  1. SqlParameter sp = new SqlParameter("@name", "Pudding");   
  2. cmd.Parameters.Add(sp);   
  3. sp = new SqlParameter("@ID", "1");   
  4. cmd.Parameters.Add(sp);  

This method can only add one SqlParameter at a time. The function of the above Code is to update the name of the field with the ID value equal to 1 to Pudding ).

Ii. AddRange Method

 
 
  1. SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@name", "Pudding"), new SqlParameter("@ID", "1") };   
  2. cmd.Parameters.AddRange(paras);  

Obviously, the Add method is inconvenient when multiple SqlParameter values are added. In this case, you can use the AddRange method.

The following code stores and reads images from the database using SqlParameter.

 
 
  1. View sourceprint? 01 public int SavePhoto (string photourl)
  2. {
  3. FileStream fs = new FileStream (photourl, FileMode. Open, FileAccess. Read); // creates a FileStream object for writing a byte data stream to BinaryReader
  4. BinaryReader br = new BinaryReader (fs); // creates a BinaryReader object to write the following byte array
  5. Byte [] photo = br. ReadBytes (int) fs. Length); // creates a byte array and writes data in the br.
  6. Br. Close (); // remember to Close the br
  7. Fs. Close (); // There are also fs
  8. String SQL = "update Table1 set photo = @ photo where ID = '0 '";
  9. SqlConnection conn = new SqlConnection ();
  10. Conn. ConnectionString = "Data Source =. \ SQLExpress; Integrated Security = true; AttachDbFilename = | DataDirectory | \ Database. mdf; User Instance = true ";
  11. SqlCommand cmd = new SqlCommand (SQL, conn );
  12. SqlParameter sp = new SqlParameter ("@ photo", photo );
  13. Cmd. Parameters. Add (sp );
  14. Try
  15. {
  16. Conn. Open ();
  17. Return (cmd. ExecuteNonQuery ());
  18. }
  19. Catch (Exception)
  20. {
  21. Return-1;
  22. Throw;
  23. }
  24. Finally
  25. {
  26. Conn. Close ();
  27. }
  28. }
  29. Public void ReadPhoto (string url)
  30. {
  31. String SQL = "select photo from Table1 where ID = '0 '";
  32. SqlConnection conn = new SqlConnection ();
  33. Conn. ConnectionString = "Data Source =. \ SQLExpress; Integrated Security = true; AttachDbFilename = | DataDirectory | \ Database. mdf; User Instance = true ";
  34. SqlCommand cmd = new SqlCommand (SQL, conn );
  35. Try
  36. {
  37. Conn. Open ();
  38. SqlDataReader reader = cmd. ExecuteReader (); // use SqlDataReader to read data
  39. If (reader. Read ())
  40. {
  41. Byte [] photo = reader [0] as byte []; // write 0th columns of data to the byte array
  42. FileStream fs = new FileStream (url, FileMode. CreateNew); creates a FileStream object for writing data to the byte data stream.
  43. Fs. Write (photo, 0, photo. Length); // Write data in the byte array to fs
  44. Fs. Close (); // Close fs
  45. }
  46. Reader. Close (); // Close reader
  47. }
  48. Catch (Exception ex)
  49. {
  50. Throw;
  51. }
  52. Finally
  53. {
  54. Conn. Close ();
  55. }
  56. }
  57. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.