从表中删除记录 [英] delete a record from a table

查看:100
本文介绍了从表中删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码运行时没有任何错误.但是仍然不会删除表中的特定记录.代码有什么问题?请帮助我找到错误!!

this code runs with out any errors.but still it doesn''t delete the specific record in the table. what is wrong with the code?please help me to find the error!!

private string dbconstr = "Data Source=.\SQLEXPRESS;AttachDbFilename=path.mdf;Integrated Security=True;User Instance=True";
        private SqlConnection con;
        private SqlCommand cmd;
        private SqlDataReader dr;
        string query = "";

try
   {
    con = new SqlConnection(dbconstr);
    con.Open();
    query = "delete from test_table where name=''"+textBox1.Text+"''";
    cmd = new SqlCommand(query, con);
    dr = cmd.ExecuteReader();
    con.Close();
    MessageBox.Show("AAA");
   }
catch 

following is the updated code,

try
   {
    con = new SqlConnection(dbconstr);
    con.Open();
    query = "delete from test_table where name=''"+textBox1.Text+"''";
    cmd = new SqlCommand(query, con);
    int rowsDeleted = cmd.ExecuteNonQuery();
    MessageBox.Show(rowsDeleted + " rows deleted");
    //cmd.ExecuteNonQuery();
    con.Close();
    //MessageBox.Show("AAA");
   }
catch { }

and the test_table definition is,

column name    Data type
id_image         int
name             nvarchar(50)
party            nchar(10)
party_im         image
name_im          image


can''t we delete a row as usual when there is an image type data is existing in the table??

推荐答案

简单错误...

使用:

simple error...

use :

int num=cmd.ExecuteNonQuery();



不是读者...

reader用于从数据库检索数据...



not reader...

reader is used to retrieve data from the database...


您必须使用ExecuteNonQuery而不是<code> ExecuteReader:
立即尝试:
You have to use ExecuteNonQuery instead of <code>ExecuteReader:
Try now:
try
   {
    con = new SqlConnection(dbconstr);
    con.Open();
    query = "delete from test_table where name='"+textBox1.Text+"'";
    cmd = new SqlCommand(query, con);
    //dr = cmd.ExecuteReader();
    int rowsDeleted = cmd.ExecuteNonQuery();
    MessageBox.Show(rowsDeleted + " rows deleted");
    con.Close();
    //MessageBox.Show("AAA");
   }


http://msdn.microsoft.com/zh-CN/library/ms233823%28v=VS.100%29.aspx [

这篇关于从表中删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆