开发时数据调用往往会用到占位符,然而刚开始使用很容易碰见sql语句在SQLServer中查询时显示“—”,如下代码:
#region 先判断是否有记录,再确定使用哪条命令 //查询数据库 20140713 commandText = "use zzbjcdzz select *" + "from [T_VillageBasicInfo] " + "where ltrim(rtrim([code]))='{0}' "; //注意参数 commandText = String.Format(commandText, root.SelectSingleNode("region").InnerText); //执行查询 dataTable = dataEngine.Select(commandText, out execRight, out retMsg); //如果存在,则使用update //20140714 有错误================================ if (dataTable.Rows.Count > 0) { //查询数据库 commandText = "use zzbjcdzz update [T_VillageBasicInfo] " + " set [population]={0},[area]={1},[Describes]='{2}',[Date]='{3}',[LeadingIndustry]='{4}',[AnnualIncome]={5},[Award]='{6}',[TeamSubsidy]={7},[PartyBuildTotalNum]={8}" + "where ltrim(rtrim([Code]))='{9}'"; //注意 commandText = String.Format(commandText, root.SelectSingleNode("population").InnerText, root.SelectSingleNode("area").InnerText,root.SelectSingleNode("Describes").InnerText,root.SelectSingleNode("Date").InnerText, root.SelectSingleNode("LeadingIndustry").InnerText,root.SelectSingleNode("AnnualIncome").InnerText,root.SelectSingleNode("Award").InnerText, root.SelectSingleNode("TeamSubsidy").InnerText,root.SelectSingleNode("PartyBuildTotalNum").InnerText,root.SelectSingleNode("region").InnerText); } else { //查询数据库 commandText = "insert into [T_VillageBasicInfo]([population],[area],[code],[Describes],[Date],[LeadingIndustry],[AnnualIncome],[Award],[TeamSubsidy],[PartyBuildTotalNum]) values({0},{1},'{2}',{3},'{4}','{5}','{6}',{7},'{8}',{9},{10} "; //注意 commandText = String.Format(commandText, root.SelectSingleNode("population").InnerText, root.SelectSingleNode("area").InnerText, root.SelectSingleNode("region").InnerText,root.SelectSingleNode("Describes").InnerText, root.SelectSingleNode("Date").InnerText, root.SelectSingleNode("LeadingIndustry").InnerText, root.SelectSingleNode("AnnualIncome").InnerText, root.SelectSingleNode("Award").InnerText, root.SelectSingleNode("TeamSubsidy").InnerText, root.SelectSingleNode("PartyBuildTotalNum").InnerText); } //执行查询 dataEngine.Execute(commandText, out execRight, out retMsg); if (execRight == true) { result = "<root><success>true</success><message>更新成功</message></root>"; } else { result = "<root><success>false</success><message>更新失败</message></root>"; }
如在更新数据库时,“
<pre name="code" class="csharp">set [population]={0},[area]={1},[Describes]='{2}',[Date]='{3}',[LeadingIndustry]='{4}',[AnnualIncome]={5},[Award]='{6}',[TeamSubsidy]={7},[PartyBuildTotalNum]={8}
”如果‘ ,’写成中文的就会在字段前显示“—”。
(给出代码供大家参考这种替换方式)