[英]How to remove special character while fetching data
下面是我的查询以获取数据,我的问题是,我将地址字段存储为
Address1%Address2%Address3
现在,在获取时,我想将其作为Adress1 (newline) Adress2 (newline)Adress3
来获取?
例如Dange%abc%Pune
饰演Dange
abc
浦那
OleDbDataAdapter da = new OleDbDataAdapter(@"SELECT FirstName,LastName,Address FROM Doctor_Master WHERE Type_of_Dr='" + typeofDr + "'", con))
很遗憾的是, Microsoft.Jet.OLEDB.4.0
提供商不支持Replace()
的功能等Microsoft.ACE.OLEDB.12.0
提供程序,否则这将是你要容易得多。
我认为您必须正常获取“ Address
字段,并使用一个辅助函数将该string
转换为所需的格式,如下所示:
public string GetBrokenLines(string address){
return address.Replace("%","\r\n");
}
//instead of using the address directly, you just need to pass it into the GetBrokenLines
//method and get the expected result.
要使用适配器填充DataTable
,请尝试以下操作:
public static DataTable GetRefDrList(string typeofDr, bool display){
DataTable refDrListTable = new DataTable();
refDrListTable.RowChanged += Format;
//....
da.Fill(refDrListTable);
//....
}
bool suppressFormat;
private void Format(object sender, DataRowChangedEventArgs e){
if(suppressFormat) return;
suppressFormat = true;
e.Row["Address"] = e.Row["Address"].ToString().Replace("%","\r\n");
suppressFormat = false;
}
我会更改您存储数据的方式。 您可以引入一个新表Doctor_Master_Address。 如果每个医生只有一个地址,也可以在Doctor_Master中添加更多列。 这样,您知道肯定会有街道名称,省名,邮政编码,国家等。
如果您仍然选择使用值字段,则可以将地址以xml的形式存储在该字段中,以便于序列化数据。 如果省略了Address2然后解析了Address1%Address3,该怎么办? 如果仅以'%'分隔,您如何知道Address3代表什么?
如果仍然选择使用以%分隔的字段,请检查msdn以获取拆分字符串信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.