Pregunta

Tengo mi propia DLL (para la capa de acceso a datos). Yo uso escapar caracteres técnica para evitar los errores de entrada de usuario, pero recientemente decidí mejorar mis clases mediante el uso de consultas con parámetros para evitar todos los posibles errores. Será la modificación ser fácil o difícil?

Cómo convertir las consultas para utilizar las consultas con parámetros?

Por favor, dame algo de código de ejemplo para aclarar la idea.

¿Fue útil?

Solución

Esta es la forma en que lo haría en C # .NET y SQL Server.

  string sQuery = @"INSERT INTO [UserJob]
                                      (
                                        [SIJCJOBID],
                                        [SIJCCHDID],
                                        [UserID],
                                        [SageDatabaseID],
                                        [MaxLineValue],
                                        [MaxAuthorisationValue],
                                        [UpdatedDate],
                                        [UpdatedUser]
                                      )
                                      VALUES
                                      (
                                        @SIJCJOBID,
                                        @SIJCCHDID,
                                        @UserID,
                                        @SageDatabaseID,
                                        @MaxLineValue,
                                        @MaxAuthorisationValue,
                                        @UpdatedDate,
                                        @UpdatedUser
                                      )
                                      SELECT SCOPE_IDENTITY() AS 'ID'";

                    using (SqlCommand oSqlCommand = new SqlCommand(sQuery))
                    {
                        oSqlCommand.Parameters.AddWithValue("@SIJCJOBID", this.SIJCJOBID);
                        oSqlCommand.Parameters.AddWithValue("@SIJCCHDID", this.SIJCCHDID);
                        oSqlCommand.Parameters.AddWithValue("@UserID", this.UserID);
                        oSqlCommand.Parameters.AddWithValue("@SageDatabaseID", this.SageDatabaseID);
                        oSqlCommand.Parameters.AddWithValue("@MaxLineValue", this.MaxLineValue);
                        oSqlCommand.Parameters.AddWithValue("@MaxAuthorisationValue", this.MaxAuthorisationValue);
                        oSqlCommand.Parameters.AddWithValue("@UpdatedDate", DateTime.Now);
                        oSqlCommand.Parameters.AddWithValue("@UpdatedUser", StaticStore.CurrentUser != null ? StaticStore.CurrentUser.UserName : "SYSTEM");

                        using (DataTable dt = DataTier.ExecuteQuery(oSqlCommand))
                        {
                            if (dt.Rows.Count == 1)
                            {
                                int.TryParse(dt.Rows[0]["ID"].ToString(), out m_UserJobID);
                            }
                        }
                }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top