替换数据库中所有字段的值--利用CodeSmith生成sql

<%--
Name:
Author:
Description:
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="ture" Description="Template description here." %>
<%@ Property Name="DataBase" Type="SchemaExplorer.DatabaseSchema" %>
<%@ Property Name="pre_Context" Type="System.String" %>
<%@ Property Name="edit_Context" Type="System.String" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="System.Data" %>

<%= find()%>

<script runat="template">
string find()
{
 string result ="";
 foreach(TableSchema table in this.DataBase.Tables)
 {
  
  if(table.HasPrimaryKey)
  {
  string[] Keys = new string[table.PrimaryKey.MemberColumns.Count];
  for(int keycount = 0;keycount < table.PrimaryKey.MemberColumns.Count;keycount ++)
  {
   Keys[keycount] = table.PrimaryKey.MemberColumns[keycount].Name;
  }
  DataTable datatable = table.GetTableData();
  
  foreach(ColumnSchema column in table.Columns)
  {
   foreach(System.Data.DataRow row in datatable.Rows)
   {
    string str = row[column.Name].ToString();
    
    if(str.IndexOf(pre_Context) >= 0)
    {
     result += "update " + table.Name + "\r\n" + " set " +
     column.Name + "=" + "'" +
     str.ToString().Replace(pre_Context,edit_Context) + "'\r\n" + " where " +
     "1=1 ";
     foreach(string key in Keys)
     {
      result += "and " + key + "= '" + row[key].ToString() + "'";
     }
     result += "\r\n" + "GO" + "\r\n";
    }
   }
  }
  }
 }
 return result;
}
</script>

你可能感兴趣的:(code)