当传入的参数值来自Function时,没有记录返回 [英] No records return when the value being pass in parameter is from Function

查看:77
本文介绍了当传入的参数值来自Function时,没有记录返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm a VB guy and slowly migrating to C#. Before i go on the main problem, I would like to show you first the function which i used in manipulating the string.

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;
    }
}

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

The function simply removes all the trailing and extra spaces in the string. For example:

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

所以我的问题是,当我使用该函数删除要传递给参数的字符串中的空格时,在datagridview中将不会绑定任何记录.

So my problem is, when i use that function to remove spaces within the string being pass in the parameter, there will be no records binded in the datagridview.

    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();
                        }    
                    }
                }
            }
        }
    }

推荐答案

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

Your function that replaces sequences of spaces with a single space can be re-written in a single line:

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

如果要使其成为扩展方法,则可以这样进行:

If you would like to make it an extension method, you can do it like this:

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

这篇关于当传入的参数值来自Function时,没有记录返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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