c# - 当传入参数的值来自 Function 时,没有记录返回

标签 c# winforms string data-binding parameters

我是一个 VB 人,正在慢慢迁移到 C#。在继续讨论主要问题之前,我想先向您展示我在处理字符串时使用的函数。

class NewString
{
    public static string RemoveExtraSpaces(string xString)
    {
        string iTemp = string.Empty;
        xString = xString.Trim();
        string[] words = xString.Split(' ');
        foreach (string xWor in words)
        {
            string xxWor = xWor.Trim();
            if (xxWor.Length > 0)
            {
                iTemp += " " + xxWor;
            }

        }
        return iTemp;
    }
}

该函数只是删除字符串中的所有尾随空格和多余空格。例如:

NewString.RemoveExtraSpaces("  Stack    OverFlow  ")
==> will return "Stack OverFlow"

所以我的问题是,当我使用该函数删除传入参数的字符串中的空格时,datagridview 中将没有绑定(bind)的记录。

    private void LoadCandidateList(bool SearchAll, string iKey)
    {
        using (MySqlConnection xConn = new MySqlConnection(ConnectionClass.ConnectionString))
        {
            using (MySqlCommand xCOmm = new MySqlCommand())
            {
                xCOmm.Connection = xConn;
                xCOmm.CommandType = CommandType.StoredProcedure;
                xCOmm.CommandText = "LoadCandidateList";
                xCOmm.Parameters.AddWithValue("LoadAll", Convert.ToInt16(SearchAll));

                string fnlKey = iKey.Trim();
                // when i use the code above, the procedure performs normally
                // but if i use the code below, no records will be return
                // why is that? i prompt it in the MessageBox to check 
                // and displays the correct value.

                // string fnlKey = NewString.RemoveExtraSpaces(iKey.Trim());
                // MessageBox.Show(fnlKey); // => return correct value

                xCOmm.Parameters.AddWithValue("iKey", fnlKey);
                xCOmm.Parameters.AddWithValue("iCurrentID", _CurrentEventID);

                using (DataSet ds = new DataSet())
                {
                    using (MySqlDataAdapter xAdapter = new MySqlDataAdapter(xCOmm))
                    {
                        try 
                        {
                            xConn.Open();
                            xAdapter.Fill(ds,"CandidateList");
                            grdResult.DataSource = ds.Tables["CandidateList"];
                        }
                        catch (MySqlException ex)
                        {
                            MessageBox.Show(ex.Message.ToString(), "Function Error <LoadCandidateList>", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        finally
                        {
                            xConn.Close();
                        }    
                    }
                }
            }
        }
    }

最佳答案

用单个空格替换空格序列的函数可以在一行中重写:

string.Join(" ", xString.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries);

如果你想让它成为一个扩展方法,你可以这样做:

static class StringExtensions {
    public static string RemoveExtraSpaces(this string xString) {
        return string.Join(" ", xString.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries));
    }
}

关于c# - 当传入参数的值来自 Function 时,没有记录返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9113086/

相关文章:

c# - 搭建脚手架时如何指定在 View 中使用哪个外键列

c - WinAPI 组合框问题 - 按案例跳过

Java Regex for Word 后面没有另一个单词

c++ - 如何在构造函数中初始化 char 数组样式的字符串

python - 使用 python 格式化 Json 字符串

c# - 了解 C# 中运行时代码生成的各种选项(Roslyn、CodeDom、Linq 表达式,...?)

c# - Windows Phone 8 应用程序动态/以编程方式在 g 网格/面板中创建按钮

c# - C# 中的 "Dictionary nest"?

.net - 每个控件都有一个单独的ErrorProvider是否有问题?

c# - 将 GroupBox 中的所有控件设置为只读?