记录从sql server中删除 [英] record deleting from sql server

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

问题描述

SqlConnection con=new SqlConnection ("connectionstring");
        con.Open ();
        SqlCommand cmd = new SqlCommand("select count(*) from tableName where cid=1233", con);
        int i = 0;
        i=(int)cmd.ExecuteScalar();
        if (i > 0)
        {
            //record is available
            SqlCommand deleteCmd = new SqlCommand("delete tablename where cid=1233", con);
            int res = 0;
            res=deleteCmd.ExecuteNonQuery();
            if (res > 0)
            {
                //deleted successfully
            }
            else
            {
                //failed to delete
            }
        }
        else
        {
            //there is no record
        }




这是问题所在
i =(int)cmd.ExecuteScalar();

每次我得到我> 0 false




here is the problem
i=(int)cmd.ExecuteScalar();

every time me getting i > 0 false

推荐答案

您需要在以下行中进行修复,

SqlConnection con=new SqlConnection ("connectionstring");

SqlConnection 需要一个连接字符串,当您传递"connectionstring"时,该字符串不是SqlConnection的有效连接字符串.

供您参考,

sqlconnection [ ^ ]

连接字符串/ [ ^ ]

sqlconnection-connection-string.aspx [ ^ ]

希望对您有所帮助:)
You would need to fix in the below line,

SqlConnection con=new SqlConnection ("connectionstring");

SqlConnection needs a connection string where as you are passing "connectionstring" which is not a valid connection string for SqlConnection.

For your ref,

sqlconnection[^]

connectionstrings/[^]

sqlconnection-connection-string.aspx[^]

Hope it helps :)


这意味着查询

This means the query

select count(*) from tableName where cid=1233



总是返回0.检查数据库中是否有与此条件相关的记录.



always returns 0. Check if there are any records in database with this condition.



在条件必须且应满足的查询中,oly您将得到i = 1,只需给出有效的连接字符串和有效的查询
Hi,
in query where condition must and should satisfy then oly u will get i=1 ,just give valid connectionstring and valid query


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

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