为什么我的count_mem没有增加 [英] why my count_mem is not incrementing

查看:49
本文介绍了为什么我的count_mem没有增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个函数,我想递增count_mem,但是它无法仅递增1次增量

请检查代码,并告诉我我哪里出错了.


i have created a function in that i want to increment count_mem but it fails to increment only 1st time increment

please check the code and tell me where i go wrong..


private void testsidecount(string code)
   {

       con.Open();

       com = new SqlCommand("SELECT COUNT(*) FROM Registration_Master WHERE sponcorid='" + code + "'", con);
       int count = (int)com.ExecuteScalar();
       SqlDataAdapter da = new SqlDataAdapter();
       try
       {
           if (count == 2)
           {
               int presentCount = 0;
               presentCount++;

               SqlCommand comm = new SqlCommand();
               comm.Connection = con;
               comm.CommandText = "UPDATE Registration_Master SET count_mem = '" + presentCount + "' ,modifieddate='" + DateTime.Now + "' WHERE regCode = '" + code + "'";
               comm.CommandType = CommandType.Text;
               comm.ExecuteNonQuery();

               if ((sponcorL2.Text != "Blank") && (sponcorR3.Text != "Blank"))
               {
                   int temp = 0;
                   temp = presentCount + 1;

                   comm = new SqlCommand();
                   comm.Connection = con;
                   comm.CommandText = "UPDATE Registration_Master SET count_mem='" + temp + "' WHERE sponcorid='" + sponcor + "'";
                   comm.CommandType = CommandType.Text;
                   comm.ExecuteNonQuery();

                   if ((SponcorL4.Text != "Blank") && (sponcorR7.Text != "Blank"))
                   {
                       int temp1 = 3;

                       com.Connection = con;
                       com.CommandText = "UPDATE Registration_Master SET count_mem='" + temp1 + "' WHERE sponcorid='" + sponcor + "'";
                       com.CommandType = CommandType.Text;
                       com.ExecuteNonQuery();
                       if ((sponcorL8.Text != "Blank") && (sponcorR15.Text != "Blank"))
                       {
                           string count4 = "";
                           count4 = "4";
                           com.Connection = con;
                           com.CommandText = "UPDATE Registration_Master SET count_mem='" + count4 + "' WHERE sponcorid='" + sponcor + "'";
                           com.CommandType = CommandType.Text;
                           com.ExecuteNonQuery();
                           if ((sponcorL16.Text != "Blank") && (sponcorR31.Text != "Blank"))
                           {
                               string count5 = "";
                               count5 = "5";
                               com.Connection = con;
                               com.CommandText = "UPDATE Registration_Master SET count_mem='" + count5 + "' WHERE sponcorid='" + sponcor + "'";
                               com.CommandType = CommandType.Text;
                               com.ExecuteNonQuery();
                               if ((sponcorL32.Text != "Blank") && (sponcorR63.Text != "Blank"))
                               {
                                   string count6 = "";
                                   count6 = "6";
                                   com.Connection = con;
                                   com.CommandText = "UPDATE Registration_Master SET count_mem='" + count6 + "' WHERE sponcorid='" + sponcor + "'";
                                   com.CommandType = CommandType.Text;
                                   com.ExecuteNonQuery();
                                   if ((sponcorL64.Text != "Blank") && (sponcorR127.Text != "Blank"))
                                   {
                                       string count7 = "";
                                       count7 = "7";
                                       com.Connection = con;
                                       com.CommandText = "UPDATE Registration_Master SET count_mem='" + count7 + "' WHERE sponcorid='" + sponcor + "'";
                                       com.CommandType = CommandType.Text;
                                       com.ExecuteNonQuery();
                                       if ((sponcorL128.Text != "Blank") && (sponcorR255.Text != "Blank"))
                                       {
                                           string count8 = "";
                                           count8 = "8";
                                           com.Connection = con;
                                           com.CommandText = "UPDATE Registration_Master SET count_mem='" + count8 + "' WHERE sponcorid='" + sponcor + "'";
                                           com.CommandType = CommandType.Text;
                                           com.ExecuteNonQuery();
                                           if ((sponcorL256.Text != "Blank") && (sponcorR511.Text != "Blank"))
                                           {
                                               string count9 = "";
                                               count9 = "9";
                                               com.Connection = con;
                                               com.CommandText = "UPDATE Registration_Master SET count_mem='" + count9 + "' WHERE sponcorid='" + sponcor + "'";
                                               com.CommandType = CommandType.Text;
                                               com.ExecuteNonQuery();
                                               if ((sponcorL512.Text != "Blank") && (sponcorR1023.Text != "Blank"))
                                               {
                                                   string count10 = "";
                                                   count10 = "10";
                                                   com.Connection = con;
                                                   com.CommandText = "UPDATE Registration_Master SET count_mem='" + count10 + "' WHERE sponcorid='" + sponcor + "'";
                                                   com.CommandType = CommandType.Text;
                                                   com.ExecuteNonQuery();
                                               }
                                           }
                                       }
                                   }
                               }

                           }
                       }

                   }
               }
           }



       }
       catch (Exception ex)
       {
           throw ex;
       }
       finally
       {
           con.Close();
       }


   }



[edit]添加了代码块-OriginalGriff [/edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

这可能是我很长一段时间以来最熟练的方法.

您可能在向同一列,同一行,同一表,同一数据库发出10条UPDATE命令,并且只有最后执行的一条命令才会计数-其他所有值都将被丢弃.此外,您正在以最原始的方式更新它,方法是创建一个字符串,将其添加到另一个字符串中,然后将其发送到SQL进行解析,然后转换为整数,然后才能实际使用它.

您真的没有发生过在开始时设置整数值,在if条件下对其进行更新,并且仅将其写入数据库一次的情况吗?

我不知道您打算如何使用该代码,但我强烈建议您再考虑一下,因为我不敢相信最终得到的就是您计划的内容!
That is probably the most cack-handed way of doing things I have seen in a very long time.

You are potentially issuing Ten UPDATE commands to the same column, of the same row, of the same table, of the same database, and only the final one that is executed is ever going to count - all the other values are discarded. In addition, you are updating it in the crudest way you can, by creating a string, adding it into another string, and sending it to SQL to parse and convert back into an integer before it can actually use it.

Did it really not occur to you to set an integer value at the start, update it in the if conditions, and only write it once to the database?

I don''t know what you are trying to do with that code, but I strongly suggest you have another think about it, because I can''t believe that what you have ended up with is what you planned!


这篇关于为什么我的count_mem没有增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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