我的整个删除方法()成功执行并且没有错误,但是记录没有被删除

本文关键字:删除 有错误 记录 方法 执行 成功 我的 | 更新日期: 2023-09-27 18:31:48

应用调试器时,查询行 textBox1.Text.my 代码中不显示任何数据:

namespace SeparateConnection
{

 class clsGridView
{
    Connection co2 = new Connection();
    //display our global varaible for connection
    SqlConnection myconn3 = new SqlConnection("data source=M-SULEMAN-PC;initial catalog=dbmsLogin;integrated security=sspi");
    public void Delete()
    {
        co2.setconn();
        try
        {
            DialogResult result = MessageBox.Show("Are you sure you want to delete ?", "Message",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                //myconn3.Open();
                DataTable table2 = new DataTable();
                frmGridView gd = new frmGridView();
                SqlDataAdapter myadd2 = new SqlDataAdapter("Delete from tblLogin where UserName ='" + gd.textBox1.Text + "'", myconn3);
                myadd2.Fill(table2);
                //Sqlcommandbulider to allow changes to database
                SqlCommandBuilder mybuild = new SqlCommandBuilder(myadd2);
                //Update the database
                myadd2.Update(table2);
                //Close the connection
                myconn3.Close();
            }
            else
                return;
        }
        catch (Exception error)
        {
            MessageBox.Show(error.ToString());
        }
    }

----------当我使用相同的类来调用和定义方法时。没有问题

我的整个删除方法()成功执行并且没有错误,但是记录没有被删除

要删除数据,必须遵循以下模式:

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Delete from tblLogin where UserName = @param";
cmd.Parameters.Add("@param", gd.textBox1.Text);
cmd.Connection = conn;
cmd.ExecuteNonQuery();

你为什么不试试这样的东西呢?

string connetionString = null;
            SqlConnection connection ;
            SqlDataAdapter adapter = new SqlDataAdapter();
            string sql = null;
            connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
            connection = new SqlConnection(connetionString);
            sql = "delete product where Product_name ='Product6'";
            try
            {
                connection.Open();
                adapter.DeleteCommand = connection.CreateCommand();
                adapter.DeleteCommand.CommandText = sql;
                adapter.DeleteCommand.ExecuteNonQuery();
                MessageBox.Show ("Row(s) deleted !! ");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }