Use parameterized TSQL queries#52326
Conversation
BillWagner
left a comment
There was a problem hiding this comment.
This LGTM. Do we need to update the accompanying article so explain why parameterized queries are preferred?
There was a problem hiding this comment.
Pull request overview
Updates the TPL Dataflow “batch database” C# and Visual Basic snippets to avoid string-concatenated SQL by switching to parameterized SQL queries.
Changes:
- Replaced
string.Format-composed SQL inGetEmployeeIDwith parameterized queries. - Updated insert/select parameter creation code paths (including some formatting/field renames).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| samples/snippets/visualbasic/VS_Snippets_Misc/tpldataflow_batchdatabase/vb/dataflowbatchdatabase.vb | Parameterized GetEmployeeID query and updated insert parameter setup. |
| samples/snippets/csharp/VS_Snippets_Misc/tpldataflow_batchdatabase/cs/dataflowbatchdatabase.cs | Parameterized GetEmployeeID query; changed insert parameter setup to AddWithValue. |
Comments suppressed due to low confidence (1)
samples/snippets/visualbasic/VS_Snippets_Misc/tpldataflow_batchdatabase/vb/dataflowbatchdatabase.vb:85
- In
InsertEmployees, parameters are re-created on every loop iteration (Clear + Add). This adds overhead and can skew the batching performance demo. Consider adding the parameters once before the loop and only updating their.Valueinside the loop.
For i As Integer = 0 To employees.Length - 1
' Set parameters.
command.Parameters.Clear()
command.Parameters.Add("@lastName", SqlDbType.NVarChar).Value = employees(i).LastName
command.Parameters.Add("@firstName", SqlDbType.NVarChar).Value = employees(i).FirstName
' Execute the command.
command.ExecuteNonQuery()
Next i
You can also share your feedback on Copilot code review. Take the survey.
| for (int i = 0; i < employees.Length; i++) | ||
| { | ||
| // Set parameters. | ||
| command.Parameters.Clear(); | ||
| command.Parameters.Add("@lastName", employees[i].LastName); | ||
| command.Parameters.Add("@firstName", employees[i].FirstName); | ||
| command.Parameters.AddWithValue("@lastName", employees[i].LastName); | ||
| command.Parameters.AddWithValue("@firstName", employees[i].FirstName); |
| command.Parameters.Add("@lastName", System.Data.SqlDbType.NVarChar).Value = lastName; | ||
| command.Parameters.Add("@firstName", System.Data.SqlDbType.NVarChar).Value = firstName; |
| command.Parameters.Add("@lastName", SqlDbType.NVarChar).Value = employees(i).LastName | ||
| command.Parameters.Add("@firstName", SqlDbType.NVarChar).Value = employees(i).FirstName |
| command.Parameters.Add("@lastName", lastName) | ||
| command.Parameters.Add("@firstName", firstName) |
| Imports System.Data.SqlClient | ||
| Imports System.Data.SqlServerCe | ||
| Imports System.Diagnostics | ||
| Imports System.IO |
| command.Parameters.Clear(); | ||
| command.Parameters.Add("@lastName", employees[i].LastName); | ||
| command.Parameters.Add("@firstName", employees[i].FirstName); | ||
| command.Parameters.AddWithValue("@lastName", employees[i].LastName); | ||
| command.Parameters.AddWithValue("@firstName", employees[i].FirstName); |
I don't think so. They were already used elsewhere on the page. |
No description provided.