温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:jimmyNews(Asp.net新闻系统)1.0源码
当前文件:
jimmyNews/Admin/Import/Default.aspx.cs[7K,2009-6-12 11:45:38],打开代码结构图
jimmyNews/Admin/Import/Default.aspx.cs[7K,2009-6-12 11:45:38],打开代码结构图1using System; 2
using System.Data; 3
using System.Data.OleDb; 4
using System.Configuration; 5
using System.Collections; 6
using System.Web; 7
using System.Web.Security; 8
using System.Web.UI; 9
using System.Web.UI.WebControls; 10
using System.Web.UI.WebControls.WebParts; 11
using System.Web.UI.HtmlControls; 12
using Wuqi.Webdiyer; 13
using CFCHINA.TOOLS.ACCESS; 14
using CFCHINA.TOOLS.COMM; 15
using CFCHINA.TOOLS.UBB; 16
17
public partial class Admin_Import_Default : System.Web.UI.Page 18
{ 19
private DataBase db = new DataBase(System.Configuration.ConfigurationManager.AppSettings["spiderdb"].ToString()); 20
private OleDbConnection conn = null; 21
private string strSql = "", sqlWhere = ""; 22
private Tools tl = new Tools(); 23
24
protected void Page_Load(object sender, EventArgs e) 25
{ 26
try 27
{ 28
InitPage(); 29
30
conn = db.GetConn(); 31
OleDbCommand cmd = null; 32
33
//这样处理的前提是必须AspNetPage不能用urlpage方式 34
strSql = "select count(*) from [content] "; 35
sqlWhere = getSqlWhere(); 36
if (sqlWhere != "") 37
{ 38
strSql += " where " + sqlWhere; 39
} 40
cmd = new OleDbCommand(strSql, conn); 41
Pager1.RecordCount = (int)cmd.ExecuteScalar(); 42
db.Dispose(conn); 43
cfBindData(); 44
45
if (!IsPostBack) 46
{ 47
BindData(); 48
} 49
} 50
catch { } 51
} 52
53
54
protected void InitPage() 55
{ 56
this.Page.Title = ConfigurationManager.AppSettings["webtitle"].ToString() + "_数据列表"; 57
((Label)(this.Master.FindControl("lblTitle"))).Text = "数据列表(注意:导入的源表名必须为[content],且必须包含[id],[分类],[标题],[内容],[发表时间]五个字段,不含括号)"; 58
} 59
60
61
public string getPageInfo() 62
{ 63
string result = ""; 64
result = "记录总数:" + this.Pager1.RecordCount + " 页数:<span style='color:red'>" + this.Pager1.CurrentPageIndex + "</span>/" + this.Pager1.PageCount; 65
return result; 66
} 67
68
69
/// <summary> 70
/// 绑定数据 71
/// </summary> 72
/// <param name="sqlWhere">查询条件</param> 73
protected void cfBindData() 74
{ 75
conn = db.GetConn(); 76
strSql = "select [id],[分类],[标题],[发表时间] from [content] order by [id]"; 77
sqlWhere = getSqlWhere(); 78
if (sqlWhere != "") 79
{ 80
strSql = strSql + " where " + sqlWhere; 81
} 82
OleDbCommand cmd = new OleDbCommand(strSql, conn); 83
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd); 84
DataSet ds = new DataSet(); 85
adapter.Fill(ds, Pager1.PageSize * (Pager1.CurrentPageIndex - 1), Pager1.PageSize, "mytable"); 86
this.GV1.DataSource = ds.Tables[0]; 87
this.GV1.DataBind(); 88
db.Dispose(conn); 89
} 90
91
/// <summary> 92
/// 翻页处理过程 93
/// </summary> 94
protected void ChangePage(object src, PageChangedEventArgs e) 95
{ 96
Pager1.CurrentPageIndex = e.NewPageIndex; 97
cfBindData(); 98
} 99
100
101
/// <summary> 102
/// 获取查询条件 103
/// </summary> 104
/// <returns></returns> 105
protected string getSqlWhere() 106
{ 107
return ""; 108
} 109
110
protected void btnImport_Click(object sender, EventArgs e) 111
{ 112
string[] srcF = txtSrcField.Text.Split(','); 113
string[] aimF = txtAimField.Text.Split(','); 114
if (srcF.Length == aimF.Length) 115
{ 116
strSql = "select top 20 id," + txtSrcField.Text.Trim() + " from [Content]"; 117
strSql = strSql.Replace(",", ","); 118
conn = db.GetConn(); 119
OleDbDataReader dr = db.RunSql(strSql, conn); 120
DataBase db2 = new DataBase(ConfigurationManager.AppSettings["db"].ToString()); 121
if (dr.HasRows) 122
{ 123
btnImport.Text = "继续导入下20条"; 124
} 125
else { 126
btnImport.Text = "导入完成"; 127
//导入完成后,删除重复记录 128
strSql = "DELETE * FROM Cf_News WHERE cf_Id in (Select cf_id from cf_news as A where cf_AutoId not In(Select max(cf_AutoId) from cf_News as B Where a.cf_Title = b.cf_Title))"; 129
db2.ExecuteNonQuery(strSql); 130
} 131
while (dr.Read()) 132
{ 133
strSql = "select top 1 cf_id from cf_news where cf_clsId='" + ddlClass.SelectedValue + "' and cf_Title='" + dr["标题"].ToString() + "'"; 134
if (!db2.ExistDate(strSql)) 135
{ 136
//先生成目标字段集 137
string[] arrF = new string[aimF.Length + 2]; 138
arrF[0] = "CF_ID"; 139
arrF[1] = "CF_CLSID"; 140
for (int i = 2; i <= aimF.Length + 1; i++) 141
{ 142
arrF[i] = aimF[i - 2]; 143
} 144
145
//再生成目标字段值集 146
string[] arrV = new string[aimF.Length + 2]; 147
arrV[0] = tl.NewComb().ToString(); 148
arrV[1] = ddlClass.SelectedValue; 149
150
for (int i = 2; i <= srcF.Length + 1; i++) 151
{ 152
arrV[i] = dr[srcF[i - 2]].ToString(); 153
} 154
155
for (int i = 0; i < arrV.Length; i++) 156
{ 157
arrV[i] = doCode(arrV[i]); 158
} 159
160
db2.InsertData("cf_News", arrF, arrV); 161
} 162
strSql = "Delete from [content] where id=" + dr[0].ToString(); 163
db.ExecuteNonQuery(strSql); 164
} 165
dr.Close(); 166
db.Dispose(conn); 167
} 168
else 169
{ 170
tl.Alert("源表字段数与目标表字段数不符,请检查!(字段之间用中文的逗号分开)", "javascript:window.history.back()"); 171
} 172
} 173
174
protected string doCode(string s) { 175
s = s.Replace("%26;amp;", "&").Replace("%26;#125;", "").Replace("%26;#123;", "").Replace("%26;#36;", "").Replace("%26amp;","&"); 176
UBB ubb = new UBB(); 177
if (s.Contains("[/")) 178
{ 179
s = ubb.UbbToHtml(s); 180
} 181
s = s.Replace("\n", "<br/>"); 182
s = tl.ReplaceEx(s,"<br>", "<br/>"); 183
s = tl.SuperFilter(s, "ScriptEx"); 184
s = tl.ReplaceEx(s, "<img", "<img onerror=\"this.src='/images/spacer.gif'\""); 185
s = tl.ReplaceEx(s, "http://www.dsbao.com", ConfigurationManager.AppSettings["weburl"].ToString()); 186
return s; 187
} 188
189
190
protected void BindData() { 191
DataBase db2 = new DataBase(ConfigurationManager.AppSettings["db"].ToString()); 192
conn = db2.GetConn(); 193
string t = ""; 194
DataSet ds = new DataSet(); 195
strSql = "select * from cf_Class order by cf_Sort"; 196
OleDbDataReader odr = db2.RunSql(strSql,conn); 197
ddlClass.DataSource = odr; 198
ddlClass.DataValueField = "cf_id"; 199
ddlClass.DataTextField = "cf_name"; 200
ddlClass.DataBind(); 201
202
strSql = "select top 1 * from cf_News"; 203
db2.RunSql(strSql, ds, conn); 204
for (int i = 0; i < ds.Tables[0].Columns.Count; i++) 205
{ 206
t += ("," + ds.Tables[0].Columns[i].ColumnName); 207
} 208
t = tl.TrimEx(t, ","); 209
txtAimField.Text = t; 210
211
db2.Dispose(conn); 212
213
conn = db.GetConn(); 214
strSql = "select top 1 * from [Content]"; 215
216
ds.Tables.Clear(); 217
218
db.RunSql(strSql, ds, conn); 219
220
t = ""; 221
222
for (int i = 0; i < ds.Tables[0].Columns.Count; i++) 223
{ 224
t += ("," + ds.Tables[0].Columns[i].ColumnName); 225
} 226
227
t = tl.TrimEx(t, ","); 228
txtSrcField.Text = t; 229
db.Dispose(conn); 230
} 231
} 232






}
}