簡體   English   中英

如何使用網絡服務從 gridview 和 SQL 中刪除一行?

[英]How do I delete a row from a gridview and SQL using a webservice?

當我單擊刪除按鈕時,我希望能夠從 gridview 中刪除一行。 但出於某種原因,該代碼僅刪除 gridview 中的行,而不是 SQL 數據庫中的行。

這是因為我沒有正確使用 web 方法 eliminarTarea?

我究竟做錯了什么? 非常感謝!

按鈕方式

            try
            {
                if (MessageBox.Show("¿Está seguro de querer eliminar esta nota?","Message",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    int rows = DataGVTareas.RowCount;
                    for(int i = rows - 1; i >= 0; i--)
                    {
                        if (DataGVTareas.Rows[i].Selected) 
                        {
                            ServiceMantenedorCliente.WebServiceMantenedorClienteSoapClient auxNegocio = new ServiceMantenedorCliente.WebServiceMantenedorClienteSoapClient();
                            auxNegocio.webEliminarTarea(titulotareasDataGridViewTextBoxColumn.ToString());
                            tareasBindingSource.RemoveAt(DataGVTareas.Rows[i].Index);
                        }
                    }
                     

                    
                }
                //ServiceMantenedorCliente.WebServiceMantenedorClienteSoapClient auxNegocio = new ServiceMantenedorCliente.WebServiceMantenedorClienteSoapClient();
                if (String.IsNullOrEmpty(this.DataGVTareas.ToString()))
                {
                    MessageBox.Show("No hay datos por eliminar ", "NoteIt", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }
                else
                {
                    //auxNegocio.webEliminarCliente(DataGVTareas.Rows[i]);
                    
                    MessageBox.Show("Los datos han sido eliminados", "NoteIt", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Se ha producido un error inesperado, por favor reinicie la aplicación e intente nuevamente" + ex.Message, "NoteIt", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

應用代碼

        public void eliminarTarea(String titulo_tareas)
        {
            this.ConexionTareas();
            this.Conec1.CadenaSQL = "DELETE FROM " + this.Conec1.NombreTabla
                                    + " WHERE titulo_tareas = '" + titulo_tareas + "';";
            this.Conec1.EsSelect = false;
            this.Conec1.Conectar();
        }

網絡服務方法

        [WebMethod]
        public void webEliminarTarea(String titulo_tareas)
        {
            NegocioNotas auxNegocio = new NegocioNotas();
            auxNegocio.eliminarCliente(titulo_tareas);
        }

我不讀西班牙語,所以我希望我能了解您的控件是如何連接的要點...您在哪里調用BindingSource.EndEdit()實際寫回數據庫?

另外,正如@Larnu 提到的,修復您的 SQL 查詢:


public void eliminarTarea(String titulo_tareas)
{
    this.ConexionTareas();

    //You'll have to fix this up because I don't know what "Conec1" is.
    //This demonstrates putting your text values into a parameter instead
    //of string concatenation.
    //Concatenation of NombreTabla is also a vulnerability but I don't know how to fix that one 
    //because it is a table name and I don't know where NobreTabla is sourced from.
    string sqlTxt = "DELETE FROM " + this.Conec1.NombreTabla
        + " WHERE titulo_tareas = @TituloTareas;";
    SqlCommand cmd = new SqlCommand([sqlConnection here], sqlTxt);
    cmd.Parameters.AddWithValue("@TituloTareas", titulo_tareas);
    this.Conec1.SqlCommand = cmd;  //Does this even work with Conec1?

    this.Conec1.EsSelect = false;
    this.Conec1.Conectar();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM