/// <summary> /// 把整个单词数据 转化成 可插入数据库的 /// </summary> /// <param name="word"></param> /// <returns></returns> public static Word FilterDB(this Word word) { PropertyInfo[] PropertyList = word.GetType().GetProperties(); foreach (PropertyInfo item in PropertyList) { string name = item.Name; string value = (string)item.GetValue(word, null); item.SetValue(word, value.FilterDB(), null); } return(word); }
/// <summary> /// 把单词转成datarow /// </summary> /// <param name="word"></param> /// <returns></returns> public static DataRow ToDataRow(this Word word) { DataTable dt = new DataTable(); DataRow dr = dt.NewRow(); PropertyInfo[] PropertyList = word.GetType().GetProperties(); foreach (PropertyInfo item in PropertyList) { string name = item.Name; string value = (string)item.GetValue(word, null); dt.Columns.Add(name); dr[name] = value; } return(dr); }
/// <summary> /// 写入数据库 /// </summary> private static void WriteWotd2DB(object o) { Word word = (Word)o; if (word.Value == null || word.Value.Equals("")) { return; } string sql = ""; try { if (word.GetType() == typeof(WordEn)) { WordEn w = word as WordEn; w = (WordEn)w.FilterDB(); sql = @"UPDATE WORD_EN SET PronouncesUs = N'{1}', Sample = N'{2}', Phrase = N'{3}', Detail = N'{4}', PronouncesEn = N'{5}', DetailEnEn = N'{6}', Synant = N'{7}', Inflections = N'{8}', AudioUrl = N'{9}', UpdateCount = UpdateCount + 1, UpdateTime = GETDATE() WHERE VALUE = N'{0}'; IF @@ROWCOUNT = 0 INSERT INTO WORD_EN( Value, PronouncesUs, Sample, Phrase, Detail, PronouncesEn, DetailEnEn, Synant, Inflections, AudioUrl, Audio, Mark, UpdateCount, UpdateTime ) VALUES ( N'{0}',N'{1}',N'{2}',N'{3}',N'{4}',N'{5}',N'{6}',N'{7}',N'{8}',N'{9}',NULL, 0, 0, GETDATE() )"; sql = string.Format(sql, w.Value, w.PronouncesUs, w.Sample, w.Phrase, w.Detail, w.PronouncesEn, w.DetailEnEn, w.Synant, w.Inflections, w.Audio); } else if (word.GetType() == typeof(WordJp)) { WordJp w = word as WordJp; w = (WordJp)w.FilterDB(); sql = @"UPDATE WORD_JP SET Sample = N'{2}', Detail = N'{3}', Synant = N'{4}', AudioUrl = N'{5}', UpdateCount = UpdateCount + 1, UpdateTime = GETDATE() WHERE VALUE = N'{0}' AND Pronounces = N'{1}'; IF @@ROWCOUNT = 0 INSERT INTO WORD_JP( Value, Pronounces, Sample, Detail, Synant, AudioUrl, Audio, UpdateCount, UpdateTime ) VALUES (N'{0}',N'{1}',N'{2}',N'{3}',N'{4}',N'{5}', NULL, 0, GETDATE())"; sql = string.Format(sql, w.Value, w.Pronounces, w.Sample, w.Detail, w.Synant, w.Audio); } using (DbManager db = new DbManager()) { db.ExeceteQuery(sql); } } catch (Exception ex) { if (ex.GetType() == typeof(SqlException) && ((SqlException)ex).Number == 2627) { //sql = "UPDATE WORD_JP"; } else { Log.Write("WriteWotd2DB Filed [{0}]\t{1}", word.Value, ex.Message); Log.Write("SQL【{0}】", sql); } } }