-1

应用调试器时,查询行在 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());
        }
    }

----------当我使用同一个类来调用和定义方法时..没有问题

4

2 回答 2

1

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

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Delete from tblLogin where UserName = @param";
cmd.Parameters.Add("@param", gd.textBox1.Text);
cmd.Connection = conn;
cmd.ExecuteNonQuery();
于 2012-04-19T07:52:06.037 回答
1

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

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());
            }
于 2012-04-19T07:53:59.573 回答