注册 登录
编程论坛 ASP.NET技术论坛

哪为 高手帮我看看下面代码怎么错了 老是删除失败!

liu7719575 发布于 2009-08-22 10:29, 2087 次点击
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string sql = "deletefrom book where k_id='" +Convert.ToInt32( this.TextBox1.Text)
            + "'and name='"
            + this.TextBox2.Text.ToString() + "'";
      
        if (DB.EN(sql)>=1)
        {
            Response.Write("删除成功");
        }
        else
        {
            Response.Write("删除失败");
        }

    }
}
DB类
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;

/// <summary>
///DB 的摘要说明
/// </summary>
public class DB
{
    public DB()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    public static int EN(string sqlstr)
    {
        int a=0;
        string str ="Data Source=7164BD0790E2444\\LIU;Initial Catalog=text;Integrated Security=True";
        SqlConnection con = new SqlConnection(str);
        SqlCommand cmd = new SqlCommand(sqlstr, con);
        try
        {
            a = cmd.ExecuteNonQuery();
            
        }
        catch (Exception e)
        {
         
        }
        finally
        {
            con.Close();
           
        }
        return a;
20 回复
#2
liu77195752009-08-22 11:41
是不是连接字符串问题啊?
#3
cn92682009-08-22 11:55
sql语句写错了 :在这 deletefrom book  看到没?这里的 deletefrom book 沾在一起了 delete book where .....就可以啦
#4
liu77195752009-08-22 11:58
回复 3楼 cn9268
哦 谢谢  先试试
#5
liu77195752009-08-22 16:59
还是不对啊   !!!!
#6
flyingcloude2009-08-22 17:09
+ "'and name='"+ this.TextBox2.Text.ToString() + "'";
是不是空格的缘故;加上trim()试试
#7
liu77195752009-08-22 17:28
不是吧  我估计是就没连接上数据库  不会是这的问题吧
#8
gongqf2009-08-22 18:52
this.TextBox2.Text.ToString()
text请查看是什么类型
ToString() 有什么作用
当你拿字符串类型再转字符串时候自己看输出的什么
#9
dhbest2009-08-23 17:04
设断点检查一下哪里出问题了...
#10
ghostql2009-08-24 21:30
有可能是你数据库里没那数据····
#11
jalonlovesja2009-08-25 12:51
+ "'and name='"
 and前面好像没空格吧!
#12
tkgg8882009-08-25 16:42
把Response去掉让它黄屏
#13
tkgg8882009-08-25 16:43
单引号是英文半角吗??
#14
lyroge2009-08-26 08:23
string sql = "deletefrom book where k_id='" +Convert.ToInt32( this.TextBox1.Text)+ "'。。。
连接整数的时候不用单引号吧,去掉单引号试试
string sql = "deletefrom book where k_id=" +Convert.ToInt32( this.TextBox1.Text)+ "。。。
#15
panyanyan2009-08-26 13:14
string sql = "deletefrom book where k_id='" +Convert.ToInt32( this.TextBox1.Text)
            + "'and name='"
            + this.TextBox2.Text.ToString() + "'";
 

deletefrom  里面没空格  
and  前面少个空格  
应该  
string sql = "delete from book where k_id='" +Convert.ToInt32( this.TextBox1.Text)
            + "'   and name='"
            + this.TextBox2.Text.ToString() + "'";
#16
liu06205412009-08-28 09:45
你的连接字符串怎么没有用户名和密码呢?
#17
fangyongp2009-08-30 00:43
string sql ="delete from book where k_id=" +Convert.ToInt32(this.TextBox1.Text)
            + "and name= "+ this.TextBox2.Text.ToString();
试试。我没用VS测试。你试下。
            
#18
liabsd20082009-08-31 14:38
delete from
#19
visolleon2009-09-05 22:29
既然用Linq,怎么还用SQL语句?
#20
xudongcsharp2009-09-07 08:11
晕~
你数据库根本都没打开!
CON.OPEN();
#21
aganarRMJ2009-09-10 16:56
SqlConnection con = new SqlConnection(str);
        SqlCommand cmd = new SqlCommand(sqlstr, con);
        try
        {
            a = cmd.ExecuteNonQuery();
            
        }

我想除了上面高手点出的问题外,还有这里的问题,你创建了一个con数据库连接后并没有用语句con.Open();打开数据库。你改改试试看,应该能够ok的了!
1