public static class staticCommon
{
public enum PrepareType
{
Insert,
Update,
Delete,
List,
Param,
getModel,
PK,
PKColumn
}
public static PrepareType columnPrepareType = PrepareType.Insert;
public static List<cSharpTypeMappingDBType> list = new List<cSharpTypeMappingDBType>();
public static void initcSharTypeMappingDBType()
{
list.Add(new cSharpTypeMappingDBType("int", "int","0"));
list.Add(new cSharpTypeMappingDBType("string", "varchar","string.Empty"));
list.Add(new cSharpTypeMappingDBType("long", "bigint","0"));
list.Add(new cSharpTypeMappingDBType("bool", "bit","false"));
list.Add(new cSharpTypeMappingDBType("string", "varchar", "string.Empty"));
list.Add(new cSharpTypeMappingDBType("Guid", "uniqueidentifier", "Guid.NewGuid()"));
list.Add(new cSharpTypeMappingDBType("string", "text", "string.Empty"));
list.Add(new cSharpTypeMappingDBType("DateTime", "datetime", "DateTime.Now"));
list.Add(new cSharpTypeMappingDBType("byte[]", "binary", "null"));
list.Add(new cSharpTypeMappingDBType("string", "char", "string.Empty"));
list.Add(new cSharpTypeMappingDBType("decimal", "decimal", "0"));
list.Add(new cSharpTypeMappingDBType("byte[]", "Image", "null"));
}
//從庫中獲取所有表
public static DataTable getAllTableFromDB(string dbName)
{
return SqlHelper.ExecuteDataset(SqlHelper.connString, CommandType.Text, "SELECT Name FROM " + dbName + "..SysObjects Where XType='U' ORDER BY Name").Tables[0];
}
//static string getIncreaseColumnName(string tableName)
//{
//}
static cSharpTypeMappingDBType getcSharpTypeFromColumnName(string columnName,string tableName)
{
cSharpTypeMappingDBType cSharpType = new cSharpTypeMappingDBType();
StringBuilder sqlString = new StringBuilder();
sqlString.Append("select a.name,b.name as type from syscolumns ");
sqlString.Append("a join systypes b on a.xtype=b.xtype ");
sqlString.Append("where a.id=object_id( '" + tableName + " ') order by a.colorder ");
DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.connString, CommandType.Text, sqlString.ToString()).Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
string cName = dt.Rows[i]["name"].ToString();
string type = dt.Rows[i]["type"].ToString();
if (columnName == cName)
{
cSharpType = getcSharpTypeFromMappingTypeList(type);
}
}
return cSharpType;
}
public static string getAllColumnNameFromTable(string tableName, PrepareType type)
{
StringBuilder sb = new StringBuilder();
if (type == PrepareType.Insert)
{
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
sb.AppendLine(" strSql.Append(/"insert into "+tableName+"(/");");
foreach (string s in getAllColumnsData(tableName).Split(','))
{
if (!getIncrease(s, tableName))
{
sb1.Append(s + ",");
sb2.Append("@" + s + ",");
}
}
sb.AppendLine(" strSql.Append(/""+sb1.ToString().TrimEnd(',')+")/");");
sb.AppendLine(" strSql.Append(/" values (/");");
sb.AppendLine(" strSql.Append(/""+sb2.ToString().TrimEnd(',')+")/");");
}
if (type == PrepareType.Update)
{
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
sb.AppendLine(" strSql.Append(/"update " + tableName + " set /");");
string[] strs=getAllColumnsData(tableName).Split(',');
bool isHasIncrease = false;
for (int i = 0; i < strs.Length;i++ )
{
if (!getIncrease(strs[i], tableName))
{
if (i == strs.Length - 1)
{
sb1.AppendLine(" strSql.Append(/"" + strs[i] + "=@" + strs[i] + "/");");
}
else
{
sb1.AppendLine(" strSql.Append(/"" + strs[i] + "=@" + strs[i] + ",/");");
}
}
else
{
isHasIncrease = true;
sb2.AppendLine(" strSql.Append(/" where " + strs[i] + "=@" + strs[i] + " /");");
}
}
if (!isHasIncrease)
{
string pk = getPK(tableName);
sb2.AppendLine(" strSql.Append(/" where " + pk + "=@" + pk + " /");");
}
sb.Append(sb1.ToString().TrimEnd(','));
sb.Append(sb2.ToString());
}
if (type == PrepareType.getModel)
{
sb.AppendLine(" strSql.Append(/"select * from "+tableName+" /");");
bool isHasIncrease = false;
string[] strs = getAllColumnsData(tableName).Split(',');
for (int i = 0; i < strs.Length; i++)
{
if (getIncrease(strs[i], tableName))
{
isHasIncrease = true;
sb.AppendLine(" strSql.Append(/" where " + strs[i] + "=@" + strs[i] + "/");");
}
}
if (!isHasIncrease)
{
string pk = getPK(tableName);
sb.AppendLine(" strSql.Append(/" where " + pk + "=@" + pk + " /");");
}
}
if (type == PrepareType.Delete)
{
bool isHasIncrease = false;
sb.AppendLine(" StringBuilder strSql = new StringBuilder();");
sb.AppendLine(" strSql.Append(/"delete "+tableName+" /");");
string[] strs=getAllColumnsData(tableName).Split(',');
for (int i = 0; i < strs.Length; i++)
{
if (getIncrease(strs[i], tableName))
{
isHasIncrease = true;
sb.AppendLine(" strSql.Append(/" where " + strs[i] + "=@" + strs[i] + " /");");
}
}
if (!isHasIncrease)
{
string pk = getPK(tableName);
sb.AppendLine(" strSql.Append(/" where " + pk + "=@" + pk + " /");");
}
}
if (type == PrepareType.PK)
{
foreach (string s in getAllColumnsData(tableName).Split(','))
{
if (getIncrease(s, tableName))
{
sb.AppendLine(" m_Command.Parameters.Add(new SqlParameter(/"@" + s + "/", model." + s + "));");
}
}
if (sb.ToString().Length < 10)
{
string s = getPK(tableName);
sb.AppendLine(" m_Command.Parameters.Add(new SqlParameter(/"@" + s + "/", model." + s + "));");
}
}
if (type == PrepareType.PKColumn)
{
string[] strs = getAllColumnsData(tableName).Split(',');
for (int i = 0; i < strs.Length; i++)
{
if (getIncrease(strs[i], tableName))
{
sb.AppendLine(strs[i] );
}
}
}
if (type == PrepareType.Param)
{
foreach (string s in getAllColumnsData(tableName).Split(','))
{
sb.AppendLine(" m_Command.Parameters.Add(new SqlParameter(/"@" + s + "/", model." + s + "));");
}
}
return sb.ToString();
}
static bool getIncrease(string columnName, string tableName)
{
object obj = SqlHelper.ExecuteScalar(SqlHelper.connString, CommandType.Text, "select COLUMNPROPERTY(OBJECT_ID('" + tableName + "'), '" + columnName + "', 'IsIdentity')");
if (obj == null)
{
return false;
}
else
{
if ((int)obj == 0)
{
return false;
}
else
{
return true;
}
}
}
static string getPK(string tableName)
{
DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.connString, CommandType.Text, "SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME='" + tableName + "'").Tables[0];
if (dt.Rows.Count == 0)
{
return string.Empty;
}
else
{
return dt.Rows[0]["column_name"].ToString();
}
}
static string getAllColumnsData(string tableName )
{
DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.connString, CommandType.Text, "SELECT * FROM SysColumns WHERE id=Object_Id('" + tableName + "')").Tables[0];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dt.Rows.Count; i++)
{
string colName = dt.Rows[i]["name"].ToString();
sb.AppendFormat( colName+ ",");
}
return sb.ToString().TrimEnd(',');
}
public static string getModelCSFile(string tableName)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("using System;");
sb.AppendLine("using System.Collections.Generic;");
sb.AppendLine("using System.Text;");
sb.AppendLine("namespace Model");
sb.AppendLine("{");
sb.AppendLine(" public class "+tableName);
sb.AppendLine(" {");
sb.AppendLine(getModelContent(tableName));
sb.AppendLine(" }");
sb.AppendLine("}");
sb.AppendLine("");
sb.AppendLine("");
return sb.ToString();
}