使用entitiy
Posted wonderfulviews
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用entitiy相关的知识,希望对你有一定的参考价值。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data; 6 using System.Data.SqlClient; 7 8 namespace SQL 9 { 10 public class DBParse 11 { 12 /// <summary> 13 /// 獲取DB中所有的表格 14 /// </summary> 15 /// <returns></returns> 16 public List<TableEntity> GetAllTable() 17 { 18 string sql = "SELECT Name FROM SysObjects Where XType=\'U\' ORDER BY Name"; 19 DataTable dt = DbHelperSQL.Query(sql).Tables[0]; 20 List<TableEntity> list = new List<TableEntity>(); 21 if (dt != null && dt.Rows.Count > 0) 22 { 23 foreach (DataRow dr in dt.Rows) 24 { 25 TableEntity entity = new TableEntity() { TableName = dr["Name"].ToString() }; 26 list.Add(entity); 27 } 28 } 29 30 return list; 31 } 32 33 public List<TableInfoEntity> GetTableInfo(string tablename) 34 { 35 string strProc = "sp_columns"; 36 SqlParameter[] parameters = { 37 new SqlParameter("@table_name", SqlDbType.VarChar,50) 38 }; 39 parameters[0].Value = tablename; 40 DataTable dt = DbHelperSQL.RunProcedure(strProc,parameters,tablename).Tables[0]; 41 List<TableInfoEntity> list = new List<TableInfoEntity>(); 42 List<TablePKEntity> list_PK = GetTablePK(tablename); 43 string strPK = ""; 44 string strpk_name = ""; 45 foreach (TablePKEntity pk in list_PK) 46 { 47 strPK += pk.PK_ColumnName + ","; 48 } 49 if (list_PK != null && list_PK.Count > 0) 50 { 51 if (!string.IsNullOrWhiteSpace(list_PK[0].PK_Name)) 52 { 53 strpk_name = list_PK[0].PK_Name; 54 } 55 } 56 strPK = strPK.TrimEnd(\',\'); 57 if (dt != null && dt.Rows.Count > 0) 58 { 59 foreach (DataRow dr in dt.Rows) 60 { 61 TableInfoEntity entity = new TableInfoEntity() ; 62 entity.TableName = dr["TABLE_NAME"].ToString().Trim().Replace("\\t", ""); 63 entity.ColumnName = dr["COLUMN_NAME"].ToString().Trim().Replace("\\t", ""); 64 entity.TypeName = dr["TYPE_NAME"].ToString(); 65 entity.Precision = Convert.ToInt32(dr["PRECISION"].ToString()); 66 entity.SCALE = dr["SCALE"] == System.DBNull.Value ? 0 : Convert.ToInt32(dr["SCALE"].ToString()); 67 entity.Nullable = dr["NULLABLE"].ToString().Trim() == "0" ? false : true; 68 entity.IsIdentity = dr["TYPE_NAME"].ToString().Contains("identity") ? true : false; 69 entity.IsPK = list_PK.Where(q => q.PK_ColumnName == dr["COLUMN_NAME"].ToString()).Count() > 0 ? true : false; 70 entity.PKColumn = strPK; 71 entity.PK_Name = strpk_name.Replace(" ", "").Replace(" ", "").Replace(" ", "").Replace(" ", "").Replace("\\t","").Replace(".",""); 72 list.Add(entity); 73 } 74 } 75 76 return list; 77 } 78 79 /// <summary> 80 /// 獲取表格的主鍵 81 /// </summary> 82 /// <param name="tablename">表格名稱</param> 83 /// <returns></returns> 84 public List<TablePKEntity> GetTablePK(string tablename) 85 { 86 string strProc = "sp_pkeys"; 87 SqlParameter[] parameters = { 88 new SqlParameter("@table_name", SqlDbType.VarChar,50) 89 }; 90 parameters[0].Value = tablename; 91 DataTable dt = DbHelperSQL.RunProcedure(strProc, parameters, tablename).Tables[0]; 92 List<TablePKEntity> list = new List<TablePKEntity>(); 93 if (dt != null && dt.Rows.Count > 0) 94 { 95 foreach (DataRow dr in dt.Rows) 96 { 97 TablePKEntity entity = new TablePKEntity(); 98 entity.TableName = dr["TABLE_NAME"].ToString().Trim().Replace("\\t",""); 99 entity.PK_ColumnName = dr["COLUMN_NAME"].ToString().Trim().Replace("\\t", ""); 100 entity.PK_Name = dr["PK_NAME"].ToString(); 101 list.Add(entity); 102 } 103 } 104 return list; 105 } 106 107 public DataTable GetTableData(string querySql) 108 { 109 DataTable dt = DbHelperSQL.Query(querySql).Tables[0]; 110 return dt; 111 } 112 113 public int ExcuteSql(List<string> sql) 114 { 115 return DbHelperSQL.ExecuteSqlTran(sql); 116 } 117 } 118 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data.SQLite; 6 using System.IO; 7 using System.Data; 8 9 namespace SQL2Sqlite 10 { 11 public class BaseHelper 12 { 13 //日志文件生命周期的时间标记 14 //private static DateTime TimeSign; 15 public Dictionary<string, string> dicSqlInsert = new Dictionary<string, string>(); 16 public Dictionary<string, string> dicSqlQuery = new Dictionary<string, string>(); 17 private string GetDBName() 18 { 19 DateTime now = DateTime.Now; 20 21 string name = "FB_Proposal" + now.ToString("yyyyMMdd") + ".db"; 22 if (!File.Exists(name)) 23 { 24 //創建DB 25 createNewDatabase(name); 26 //創建Table 27 //CreateTable(name); 28 } 29 return name; 30 } 31 //创建一个空的数据库 32 private void createNewDatabase(string name) 33 { 34 SQLiteConnection.CreateFile(name); 35 } 36 private void CreateTable(string name) 37 { 38 string sql = "create table YouKeyBorad (addtime INTEGER, keyvalue varchar(10));create table YouScreen (addtime INTEGER, screenimg TEXT)"; 39 SQLiteConnection conn = new SQLiteConnection(string.Format("Data Source={0};Version=3;UseUTF8Encoding=True;", name)); 40 SQLiteCommand command = new SQLiteCommand(sql, conn); 41 conn.Open(); 42 command.ExecuteNonQuery(); 43 conn.Close(); 44 } 45 public SQLiteConnection Connect() 46 { 47 SQLiteConnection m_dbConnection = new SQLiteConnection(string.Format("Data Source={0};Version=3;UseUTF8Encoding=True;", GetDBName())); 48 return m_dbConnection; 49 } 50 51 public bool CreateTable2(string tablename) 52 { 53 54 bool retbool = false; 55 StringBuilder strSql = new StringBuilder(); 56 //生成 插入數據的SQl語句 57 StringBuilder strSql_insert = new StringBuilder(); 58 StringBuilder strSql_Query = new StringBuilder(); 59 try 60 { 61 strSql.Append(" create table "); 62 strSql.Append("[" + tablename.TrimEnd() + "]"); 63 strSql.Append(" ( "); 64 DBParse dbSQL = new DBParse(); 65 List<TableInfoEntity> list = dbSQL.GetTableInfo(tablename); 66 //foreach (TableInfoEntity entity in list) 67 string strPK = ""; 68 string strpk_name = ""; 69 strSql_insert.AppendFormat(" insert into {0} values(", tablename); 70 strSql_Query.Append("select "); 71 //拼接創建表的SQL 72 for (int i = 0; i < list.Count; i++) 73 { 74 TableInfoEntity entity = list[i]; 75 #region 拼接創建表 SQL 76 strSql.Append("[" + entity.ColumnName + "]"); 77 strSql.Append(" "); 78 strSql.Append(SQLType2SQLiteType.SqlTypeString2SqliteType(entity.TypeName, entity.Precision, entity.SCALE)); 79 80 if (!entity.Nullable) 81 { 82 strSql.Append(" NOT NULL "); 83 } 84 85 //if (entity.IsIdentity) 86 //{ 87 // strSql.Append(" AUTOINCREMENT "); 88 //} 89 #endregion 90 #region 拼接插入數據的SQL 91 if (entity.TypeName.ToLower().Equals("varchar") || entity.TypeName.ToLower().Equals("ntext") 92 || entity.TypeName.ToLower().Equals("nvarchar") || entity.TypeName.ToLower().Equals("text") 93 || entity.TypeName.ToLower().Equals("char") || entity.TypeName.ToLower().Equals("nchar") 94 || entity.TypeName.ToLower().Equals("bit") || entity.TypeName.ToLower().Equals("datetime") 95 || entity.TypeName.ToLower().Equals("date") || entity.TypeName.ToLower().Equals("time")) 96 { 97 strSql_insert.Append(" \'{"); 98 } 99 else 100 { 101 strSql_insert.Append(" {"); 102 } 103 if (entity.TypeName.Equals("System.DateTime") || entity.TypeName.ToLower().Contains("datetime")) 104 { 105 strSql_insert.Append(i + ":yyyy-MM-dd HH:mm:ss.fff"); 106 } 107 else 108 { 109 strSql_insert.Append(i); 110 } 111 //strSql_insert.Append(i); 112 if (entity.TypeName.ToLower().Equals("varchar") || entity.TypeName.ToLower().Equals("ntext") 113 || entity.TypeName.ToLower().Equals("nvarchar") || entity.TypeName.ToLower().Equals("text") 114 || entity.TypeName.ToLower().Equals("char") || entity.TypeName.ToLower().Equals("nchar") 115 || entity.TypeName.ToLower().Equals("bit") || entity.TypeName.ToLower().Equals("datetime") 116 || entity.TypeName.ToLower().Equals("date") || entity.TypeName.ToLower().Equals("time")) 117 { 118 strSql_insert.Append("}\'"); 119 } 120 else 121 { 122 strSql_insert.Append("}"); 123 } 124 #endregion 125 #region 拼接查詢數據的SQL 126 strSql_Query.Append("[" + entity.ColumnName + "]"); 127 #endregion 128 if (i < list.Count - 1) 129 { 130 strSql.Append(","); 131 strSql_insert.Append(","); 132 strSql_Query.Append(","); 133 } 134 strPK = entity.PKColumn; 135 strpk_name = entity.PK_Name; 136 } 137 //給表添加主鍵 138 if (strPK != "") 139 { 140 if (strpk_name.Trim() == "") 141 { 142 strpk_name = "pk_" + tablename.TrimEnd(); 143 } 144 strSql.AppendFormat(" ,constraint {0} primary key ({1}) ", strpk_name, strPK); 145 } 146 strSql.Append(" ) "); 147 strSql_insert.Append(" );"); 148 strSql_Query.Append(" from " + tablename.TrimEnd()); 149 SQLiteHelper.ExecuteNonQuery(Connect(), strSql.ToString()); 150 retbool = true; 151 dicSqlInsert.Add(tablename.TrimEnd(), strSql_insert.ToString()); 152 dicSqlQuery.Add(tablename.TrimEnd(), strSql_Query.ToString()); 153 } 154 catch (Exception ee) 155 { 156 MyConsole.WriteLine(ee.Message + "----" + strSql.ToString()); 157 retbool = false; 158 } 159 return retbool; 160 } 161 162 public int ImportData(string querySql,string intoSql) 163 { 164 int retInt = 0; 165 DBParse dbSQL = new DBParse(); 166 DataTable dtSqlData = dbSQL.GetTableData(querySql); 167 //最後的執行insert的SQL語句 168 List<string> listSql = new List<string>(); 169 if (dtSqlData != null && dtSqlData.Rows.Count > 0) 170 { 171 for (int i = 0; i < dtSqlData.Rows.Count; i++) 172 { 173 try 174 { 175 string sqlTemp = ""; 176 object[] args = new object[dtSqlData.Columns.Count]; 177 object objtemp = null; 178 bool haveNull = false; 179 for (int j = 0; j < dtSqlData.Columns.Count; j++) 180 { 181 //MyConsole.TransferEncoding(Encoding.Default, Encoding.UTF8, dtSqlData.Rows[i][j].ToString()); 182 if (intoSql.Contains("PIA_LOG_MAIN")) 183 { 184 break; 微信小程序代码片段