本文介绍了如何转换文本的第一个字母并将其保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我为客户创建了一个表单,我希望将客户基本信息保存在表格中,但在保存该信息时,我需要转换每个字符串的第一个字母并将其保存到数据库表格 这是我的表格的代码 < div class = form-group col-md-5 > < label for = exampleInputEmail1 > 名字:< / label > < asp: TextBox ID = txt_first_name runat = server CssClass =' form-control' 占位符 = 名字 > < / asp:TextBox > < / div > < div class = form-group col-md-5 > < 标签 = exampleInputEmail1 > 中间名:< / label > < asp:TextBox ID = txt_middle_name runat = server CssClass =' form-control' 占位符 = 中间名 > < / asp:TextBox > < / div > < div class = form-group col-md-5 > < label for = exampleInputEmail1 > 姓氏:< / label > < asp:TextBox ID = txt_last_name runat = server CssClass =' 表格控制' 占位符 = 姓氏 > < / asp:TextBox > < / div > 这是保存按钮的代码 // string fn = txt_first_name.Text.Substring(0,1).ToUpper() + txt_first_name.Text.Substring(1,txt_first_name.Text.Length-1).ToLower(); cmd = new SqlCommand( new_cust,con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue( @ invoice_associate_id,custid.ToString()); cmd.Parameters.AddWithValue( @ invoice_associate_name,txt_first_name.Text.Substring( 0 , 1 )。ToUpper()+ txt_first_name.Text.Substring( 1 ,txt_first_name.Text.Length - 1 )。ToLower()); 注释代码完美无缺,但在传递参数时我需要转换它是否有任何方法可以实现这一点 请建议方法来做到这一点,并提前感谢为您提供帮助解决方案 您好, 请尝试以下方法: cmd.Parameters.AddWithValue( @ invoice_associate_name,CultureInfo.CurrentCulture .TextInfo.ToTitleCase(txt_first_name.Text.ToLower())); 它使用ToTitleCase 方法top达到了预期的格式。 ...将.ToLower()添加到文本中可确保实现所需的格式,即使用户在文本框中输入所有大写字母也是如此。 希望它有所帮助。 在将它发送到存储过程之前使用此函数转换 public static string FirstCharToUpper( string input) { if ( String .IsNullOrEmpty(input)) throw new ArgumentException( 错误); return input.First()。ToString()。ToUpper()+ String .Join ( ,input.Skip( 1 )); } 这会将字符串的每个首字母转换为Upper public static string FirstCharToUpper( string str) { return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower()); } I have created a Form for customer i want to save the customer basic information in a table but while saving that information i need to convert First Letter of a every string and save it to database tableHere is code for my form<div class="form-group col-md-5"> <label for="exampleInputEmail1">First Name : </label> <asp:TextBox ID="txt_first_name" runat="server" CssClass='form-control' placeholder="First Name"></asp:TextBox> </div> <div class="form-group col-md-5"> <label for="exampleInputEmail1">Middle Name : </label> <asp:TextBox ID="txt_middle_name" runat="server" CssClass='form-control' placeholder="Middle Name"></asp:TextBox> </div> <div class="form-group col-md-5"> <label for="exampleInputEmail1">Last Name : </label> <asp:TextBox ID="txt_last_name" runat="server" CssClass='form-control' placeholder="Last Name"></asp:TextBox> </div>Here Is My Code For Save Button //string fn = txt_first_name.Text.Substring(0, 1).ToUpper() + txt_first_name.Text.Substring(1, txt_first_name.Text.Length-1 ).ToLower(); cmd = new SqlCommand("new_cust", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@invoice_associate_id", custid.ToString()); cmd.Parameters.AddWithValue("@invoice_associate_name", txt_first_name.Text.Substring(0, 1).ToUpper() + txt_first_name.Text.Substring(1, txt_first_name.Text.Length - 1).ToLower());The commented code works perfectly but while passing parameters i need to convert it is there is any way to achieve this Please suggest the methods to do this and thanks in advance for your help 解决方案 Hi,Try the following:cmd.Parameters.AddWithValue("@invoice_associate_name", CultureInfo.CurrentCulture.TextInfo.ToTitleCase(txt_first_name.Text.ToLower()));It uses the ToTitleCase method top achieve the desired format.... adding the .ToLower() to your text ensures the desired format is achieved, even if the user enter all capital letters into your textbox.Hope it help.Use this function to convert before sending it to stored procedurepublic static string FirstCharToUpper(string input){ if (String.IsNullOrEmpty(input)) throw new ArgumentException("Error"); return input.First().ToString().ToUpper() + String.Join("", input.Skip(1));}this will convert every first letter of string to Upperpublic static string FirstCharToUpper(string str){ return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());} 这篇关于如何转换文本的第一个字母并将其保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-03 15:25