计数不检查数据库中的记录.并一直插入. [英] count is not checking the records in databse. and inserted all the time.

查看:41
本文介绍了计数不检查数据库中的记录.并一直插入.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用count sql查询来检查记录是否存在.如果它们退出,那么我希望该记录已存在的错误消息.
我为此写了一个代码.我收到了错误的错误

i am using count sql query to check if the record exist. if they exits then i want error mesage that record already exist.
i wrote a code for this. i am getting the fallowing error

notSystem.Data.SqlClient.SqlException: The Count function requires 1 argument(s). at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlDataReader.ConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteScalar() at Default2.Button1_Click(Object sender, EventArgs e)







try
        {
            //int count = -1;
            MAconn = new SqlConnection();
            MAconn.ConnectionString = sConnectionString;
            MAconn.Open();
            //string a = "islamabad pakistan";
            MAcmd = MAconn.CreateCommand();
            
            MAcmd.CommandText = "SELECT Count(UserId,GroupID) FROM roles Select UserId,GroupID from Users,Groups WHERE username = '" + TextBox1 + "' AND name = '" + TextBox2.Text + "'";
            int count =(int)MAcmd.ExecuteScalar();
            if(count==1)
            {
                Label1.Text="record exits";
            }
            else
            {
            MAcmd.CommandText = "INSERT INTO ROLES (UserId,GroupId) SELECT UserId,GroupId FROM Users, Groups WHERE username= '" + TextBox1.Text + "' AND name='" + TextBox2.Text + "'";

            MAcmd.ExecuteNonQuery();
            //MessageBox.Show("Inserted Sucessfully");
            Label1.Text = "inserted";
            }
            MAconn.Close();
        }
        catch (Exception ex)
        {
            Label1.Text = "not" + ex;
        }

推荐答案

不要问相同的问题.您已经在此处提问.我已经解决了您提到的错误的地方,现在您在此处发布经过修订的代码,这是另一个问题.甚至这也可以在此处解决. .
请检查这些.

如果您的问题得到解决,那么为什么不接受答案.
Don''t ask same question. You already ask a questions here. Where I have already solved your errors mentioned by you and now you are posting revised code here as another questions. Even this is also solved here.
Please check these.

And if you problem is solved than why don''t you accept the answer.


在if条件下使用
if(count > 0)

.

OP更新问题后更新答案:
当然,它不会运行.您只能在Sql中将一个参数传递给

in the if condition.

Answer updated after OP updated question:
Ofcourse it won''t run. You can only pass one argument to

Count()

.


这篇关于计数不检查数据库中的记录.并一直插入.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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