当值大于0时如何避免每次初始化

How to avoid every time initialization when value have greater than 0

我有一个方法,在检查它是否已经存在后插入一条新记录。

这是我的方法:

protected void btn_save_Click(object sender, EventArgs e)
{
    string MobileNo = "";
    string replaceValue = txt_mobile.Text.Replace(Environment.NewLine, "$");

    string[] values = replaceValue.Split('$');
    int uCnt = 0;
    int sCnt = 0;

    foreach (string item in values)
    {
        SaveRecord(item.Trim(),out MobileNo,out uCnt,out sCnt);
    }

    txt_mobile.Text = string.Empty;

    if(uCnt > 0)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "BulkSMS System", "alert('Mobile No(s) : "+MobileNo.TrimEnd(',')+" Already Exist');", true);
    }

    if(sCnt > 0)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "BulkSMS System", "alert('" + sCnt + " Record(s) Inserted Successfully');", true);
    }

    Get_Data();
}

public void SaveRecord(string value, out string MobileNo, out int uCnt, out int sCnt)
{
    uCnt = 0; //every time initialized to 0
    sCnt = 0; //every time initialized to 0 
    MobileNo = "";

    try
    {
        DataTable dt = new DataTable();
        var dot = Regex.Match(value, @"\+?[0-9]{10}");

        if (dot.Success)
        {
            string str = "SELECT TOP 1 [ID],[MobileNo] FROM[dbo].[whitelistdata]";
            str += " WHERE [UserID] = '" + Convert.ToInt32(ddl_users.SelectedValue.ToString()) + "' AND [SenderId] = '" + Convert.ToInt32(ddl_senders.SelectedValue.ToString()) + "' AND [MobileNo] = '" + value + "'";
            dt = obj.Get_Data_Table_From_Str(str);

            if (dt.Rows.Count > 0)
            {
                uCnt++;
                MobileNo += value + ",";
            }
            else
            {
                string str1 = "INSERT INTO [dbo].[whitelistdata]([UserID],[SenderId],[KeywordID],[MobileNo])";
                str1 += "VALUES (" + Convert.ToInt32(ddl_users.SelectedValue.ToString()) + "," + Convert.ToInt32(ddl_senders.SelectedValue.ToString()) + ",1," + value + ")";
                obj.Execute_Query(str1);
                sCnt++;
            }
        }
    }
    catch (Exception ex)
    {
        CommonLogic.SendMailOnError(ex);
        ClientScript.RegisterStartupScript(this.GetType(), "BulkSMS System", "alert('" + ex.Message.ToString() + "');", true);
    }
}

问题是每次调用方法时将其设置为 0 我想在先前的值大于 0 时阻止它们。

请大家帮帮我..

请先确定是哪个组合签到数据库。

如果 UserID 和 SenderId 组合匹配 Then

 string str = "SELECT TOP 1 [ID],[MobileNo] FROM[dbo].[whitelistdata]";
 str += " WHERE [UserID] = '" + Convert.ToInt32(ddl_users.SelectedValue.ToString()) + "' AND [SenderId] = '" + Convert.ToInt32(ddl_senders.SelectedValue.ToString()) + "'";

if 检查唯一的 UserID Match Then

 string str = "SELECT TOP 1 [ID],[MobileNo] FROM[dbo].[whitelistdata]";
 str += " WHERE [UserID] = '" + 
     Convert.ToInt32(ddl_users.SelectedValue.ToString()) +"'";

if UserID OR SenderId 组合 Match Then

 string str = "SELECT TOP 1 [ID],[MobileNo] FROM[dbo].[whitelistdata]";
 str += " WHERE [UserID] = '" + Convert.ToInt32(ddl_users.SelectedValue.ToString()) + "' OR [SenderId] = '" + Convert.ToInt32(ddl_senders.SelectedValue.ToString()) + "'";

if UserID AND SenderId AND MobileNo 组合匹配 Then

string str = "SELECT TOP 1 [ID],[MobileNo] FROM[dbo].[whitelistdata]";
str += " WHERE [UserID] = '" + Convert.ToInt32(ddl_users.SelectedValue.ToString()) + "' AND [SenderId] = '" + Convert.ToInt32(ddl_senders.SelectedValue.ToString()) + "' AND [MobileNo] = '" + value + "'";

如果您想保留此设计,您需要使用 ref 而不是 out1。这意味着该方法可以假定变量已经初始化,并且您不必在方法中重新初始化它们:

public void SaveRecord(string value,out string MobileNo,ref int uCnt,ref int sCnt)
{
    //uCnt = 0; //initialized by caller
    //sCnt = 0; //initialized by caller
    MobileNo = ""; //?
   ....

并在调用站点:

SaveRecord(item.Trim(),out MobileNo,ref uCnt,ref sCnt);

如果您希望 积累 值而不是每次通过循环覆盖。也许将其设为 StringBuilder 而不是您正常传递(没有 refout)并让 SaveRecord 方法附加到。 out肯定是错的。


1许多人会对一种方法皱眉,这种方法显然 想要 到 return 值被声明 void并通过 ref/out.

制作所有 return

类似于:

public bool SaveRecord(string value)
{
   ...

为新记录返回 true,为现有记录返回 false。我可能会从那里取出异常处理,让异常在处理之前传播得更高。那么呼叫站点将是:

if(SaveRecord(item.Trim()))
{
   sCnt++;
}
else
{
   uCnt++;
   MobileNo += item.Trim + ","
}