|
|
2007年12月20日
对于定时任务的处理方法一般有两种 自定义windows service和设置计划任务
两种方案比较:
自定义Windows Service:
优点:灵活度比较高,定时任务可以保存在数据库中也可以在config文件中。可以另外写一个可视的配置小程序来帮助用户(管理员)来管理。运行结果也方便跟踪。发布的时候配置一次即可。
缺点:需要额外的编码来读取配置的定时任务并运行相应的程序,同时需要记录运行结果
利用windows计划任务
优点:节省开发时间
缺点:对系统依赖程度较高;配置稍微麻烦一些,特别是需要配置多种计划任务的情况,对用户(系统管理员)来说比较麻烦;计划任务是否按时启动需要考启动程序的log来查看,windows的计划任务本身的log只有4k,而且是循环使用,用来判断计划任务是否启动不太可靠。
SchTasks使用说明:
示例:
删除:SCHTASKS /DELETE /TN ScheduleTaskTest /F
创建:SCHTASKS /Create /RU SYSTEM /TN ScheduleTaskTest /SC MONTHLY /D 1 /ST 00:00:00 /TR "C:\Solution\TestApp\Bin\Debug\TestApp.exe /Method Test"
参数说明:
/sc Schedule Type
指定计划类型。有效值为 MINUTE、HOURLY、DAILY、WEEKLY、MONTHLY、ONCE、ONSTART、ONLOGON、ONIDLE。
MINUTE、HOURLY、DAILY、WEEKLY、MONTHLY 指定计划的时间单位。
ONCE 任务在指定的日期和时间运行一次。
ONSTART 任务在每次系统启动的时候运行。可以指定启动的日期,或下一次系统启动的时候运行任务。
ONLOGON 每当用户(任意用户)登录的时候,任务就运行。可以指定日期,或在下次用户登录的时候运行任务。
ONIDLE 只要系统空闲了指定的时间,任务就运行。可以指定日期,或在下次系统空闲的时候运行任务。
/tn Task Name
指定任务的名称。系统上的每项任务都必须具有一个唯一的名称。名称必须符合文件名称规则
/tr Task to Run
指定任务运行的程序或命令。键入可执行文件、脚本文件或批处理文件的完全合格的路径和文件名。路径名称不得超过 262 个字符。如果忽略该路径,SchTasks 将假定文件在 Systemroot\System32 目录下。
/ru {[Domain\]User | System}
使用指定用户帐户的权限运行任务。默认情况下,使用本地计算机当前用户的权限,或者使用 /u 参数指定的用户的权限(如果包含的话)运行任务。在本地或远程计算机上计划任务时,/ru 参数才有效。
可设置值说明:
[Domain\]User 指定候选用户帐户。
System 指定 Local System 帐户,这是一种操作系统和系统服务使用的具有高度特权的帐户。
/sd StartDate
可选, 指定任务计划开始的日期。默认值为本地计算机上的当前日期。
其实windows还提供了另外一个指令创建计划任务AT,不过AT指令会自动给计划任务起名字,不支持指定名称,用来做脚本就不太显示,这里就不多说这条指令了。
2007年12月6日
2007年12月5日
Step 6 Import the data in excel to database
a. Create excel app object and open the uploaded file
Microsoft.Office.Interop.Excel.Application xlsApp = new ApplicationClass();

Workbook wb = xlsApp.Workbooks.Open(_filePath,Type.Missing,Type.Missing, Type.Missing,Type.Missing,Type.Missing,Type.Missing, Type.Missing,Type.Missing,Type.Missing,Type.Missing, Type.Missing,Type.Missing,Type.Missing,Type.Missing);

wb.Unprotect(TemplatePassword);

b. Save the upload file as a temporary file. Then close uploaded file and open the temporary file
string tempFileName = _filePath.ToLower().Replace(".xls","_Temp.xls");

wb.Unprotect(TemplatePassword);

wb.SaveCopyAs(tempFileName);

c. Verify the data in template again in web application
ReadDataSource((Worksheet)wb.Worksheets[DataSourceSheet], out branchID, out planDate, out startDate, out endDate, out iChecked, out templatetype);

if(!(iChecked == 1))

 {

returnVal = "Please verify the data before upload to the server!";

throw new Exception(returnVal);

}

if(templatetype.ToUpper() != _templatetype.ToUpper())

 {

returnVal = "The version is not corrected, please verify the document and uploaded again";

throw new Exception(returnVal);

}

……

private void ReadDataSource(Worksheet ws, out string branchID, out string planDate, out DateTime startDate,out DateTime endDate, out int iChecked, out string templatetype)

 {

string check = ws.get_Range("A1", System.Type.Missing).Text.ToString();

branchID = ws.get_Range("A2", System.Type.Missing).Text.ToString();

string sDate = ws.get_Range("A4", System.Type.Missing).Text.ToString();

string eDate = ws.get_Range("A5", System.Type.Missing).Text.ToString();

planDate = ws.get_Range("A6", System.Type.Missing).Text.ToString();

templatetype = ws.get_Range("A7", System.Type.Missing).Text.ToString();

startDate = DateTime.Parse(sDate);

endDate = DateTime.Parse(eDate);

try

 {

iChecked = Convert.ToInt16(check);

}

catch

 {

iChecked = 0;

}

}

d. Read the data in the worksheet
string territoryList = ws.get_Range("B"+ i.ToString(), System.Type.Missing).Text.ToString();

string territoryIDList = ws.get_Range("AB" + i.ToString(), System.Type.Missing).Text.ToString();

string category = ws.get_Range("E" + i.ToString(), System.Type.Missing).Text.ToString();

string categoryID = ws.get_Range("AE" + i.ToString(), System.Type.Missing).Text.ToString();

e. Change the database based on data in excel sheet
SqlHelper.ExecuteNonQuery(connnectionString, CommandType.Text, "insert into … ");
Summary: Objects used in this sample
a. Excel.Application
i. Application.Workbooks.Open
ii. Quit
b. Excel.Workbook
i. SaveCopyAs
ii. Unprotect
iii. Worksheets
iv. Protect
v. .Names.Add
vi. Close
c. Excel.WorkSheet
i. Unprotect
ii. Protect
iii. .Hyperlinks.Add
d. Range
i. Value2
ii. Text
e. Cell, Cells
2007年12月4日
2. Step 2 Generate an Excel template to fill with source data in C#
a. Create excel application object
Microsoft.Office.Interop.Excel.Application xlsApp = null;

Workbook wb=null;
b. Open the template and SaveCopyAs a new temporary template file name
fileName= templatePath + @"\template.xls";
excelFileName = tempFileName + "_template.xls";
tempFileName= tempFileName + "_template_Temp.xls";
xlsApp = new ApplicationClass();
wb = xlsApp.Workbooks.Open(fileName, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
wb.Unprotect(TemplatePassword);

wb.SaveCopyAs(tempFileName);
c. Close the template and open the new temporary file
wb.Close(false, Type.Missing, Type.Missing);
xlsApp.Quit();
wb = xlsApp.Workbooks.Open(tempFileName, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);

d. Get master data from database and fill in each cell and set, validation lock property properly based on requirement.
ws = (Worksheet)wb.Worksheets[DataSourceSheet];
LoadDataSource(ws, wb, beginDate, endDate);
 .


private void LoadDataSource(Worksheet ws, Workbook wb, DateTime beginDate, DateTime endDate)
 {
DataSet ds = SqlHelper.ExecuteDataset(connnectionString, CommandType.Text, "");
//Generate the Base Info
ws.get_Range("A1",System.Type.Missing).Value2 = 0; //Check flag
ws.get_Range("A4",System.Type.Missing).Value2 = beginDate.ToString("yyyy-MM-dd");
ws.get_Range("A5",System.Type.Missing).Value2 = endDate.ToString("yyyy-MM-dd");
ws.get_Range("A6",System.Type.Missing).Value2 = beginDate.ToString(PlanDateFormat);
ws.get_Range("A7",System.Type.Missing).Value2 = _templatetype;

LoadActionStatus(wb, ws, ds.Tables[0]);
LoadPromotionType(wb, ws, ds.Tables[0]);

LoadUserDataSource(wb, ws, beginDate.ToString(PlanDateFormat));

LoadCategoryDataSource(ws);

//ws.Visible = XlSheetVisibility.xlSheetVisible;
}


private void LoadMaterialDataSource(Worksheet ws)
 {
string sql = "select MaterialCode, EnglighShortName as MaterialName from Material";
string conn = ConfigurationSettings.AppSettings["ConnString"];
Range rng = ws.get_Range("Q1",System.Type.Missing);
QueryTable qt = ws.QueryTables.Add("OLEDB;Provider=SQLOLEDB.1;" + conn, rng, sql);
qt.Refresh(System.Type.Missing);
qt.Name = "Material";
}

private void LoadCategoryDataSource(Worksheet ws)
 {
string sql = "SELECT CategoryID, CategoryNameEn FROM Category WHERE Status = 3";
string conn = ConfigurationSettings.AppSettings["ConnString"];
Range rng = ws.get_Range("W1",System.Type.Missing);
QueryTable qt = ws.QueryTables.Add("OLEDB;Provider=SQLOLEDB.1;" + conn, rng, sql);
qt.Refresh(System.Type.Missing);
qt.Name = "Category";
}
e. Protected worksheets and workbook based on requirement
ws = (Worksheet) wb.Worksheets[SummarySheet];

ws.Protect(TemplatePassword,System.Type.Missing,System.Type.Missing,System.Type.Missing,System.Type.Missing,System.Type.Missing,System.Type.Missing,System.Type.Missing,System.Type.Missing,System.Type.Missing,System.Type.Missing,System.Type.Missing,System.Type.Missing,System.Type.Missing,System.Type.Missing, System.Type.Missing);

wb.Protect(TemplatePassword,System.Type.Missing, System.Type.Missing);


f. Save the temporary file as the final template name
wb.SaveCopyAs(excelFileName);
g. Close the template and release the excel resource
if (wb!=null)

 {

wb.Close(false, Type.Missing, Type.Missing);

}

if(xlsApp != null)

 {

xlsApp.Quit();

}

//Remove the temporary file

System.IO.File.Delete(tempFileName);

. Step 3 Download the template from web site
4. Step 4 Fill in the template and verify the data
a. User fills in the data according to the description: list validation, free typing in, popup form etc.
b. Click the validation button in the sheet and validate the dat
5. Step 5 Upload the template to the web site
2007年12月3日
Excel programming (C# + VBA)
Here is the secario of a full process of solution to use excel to submit information.
-
Clarify the requirement and create the Excel template (VBA)
-
Generate an Excel template to fill with source data (C#)
-
Download the template from web site (C# + ASP.NET)
-
Fill in the template and verified the data via VBA (VBA)
-
Upload the template (Web) (ASP.NET)
-
Import data in Excel Template to database (C#)
1. Setp 1 Create the Excel Template based on requirement
a. Clarify the requirement
b. Create hidden template sheet
i. Clarify the data type in each cell, fill in type (user input/list validation or a pop form) and the lock property.
c. Create hidden data source sheet
i. Clarify the source data in each column
ii. Create named range for list validation (If the data is already known)
d. Create Summary, Filling Sheet
i. Set the layout, add validation button and Add new button
e. VBA programming to add list validation or auto-filling
rng.Validation.Add xlValidateList, xlValidAlertStop, xlBetween, "=SpecialOfferType"
rng.Validation.IgnoreBlank = True
rng.Validation.InCellDropdown = True
f. VBA programming to select data in pop form
With frmChoose
.CCodeColumn = "AB"
.CNameColumn = "B"
.CRow = Target.Row
.CCodeSourceColumn = "B"
.CNameSourceColumn = "C"
.CKeyWords = Me.Cells(iRow, iColumn).Text
.Caption = Me.Cells(iRow, iColumn - 1)
Set .CWorksheet = Application.ActiveSheet
Set .CSourceWorksheet = sourceSheet
.Show
End With

In frmChoose, when the Ok button is clicked:
CWorksheet.Unprotect Password
n = 2
If (CSourceWorksheet Is Nothing) Then
name = wsDataSource.Range(CNameSourceColumn & n).Text
Else
name = CSourceWorksheet.Range(CNameSourceColumn & n).Text
End If
While (name <> "")
If name = lstSelected.List(lstSelected.ListIndex) Then
If (CSourceWorksheet Is Nothing) Then
code = wsDataSource.Range(CCodeSourceColumn & n).Text
Else
code = CSourceWorksheet.Range(CCodeSourceColumn & n).Text
End If
End If
n = n + 1
If (CSourceWorksheet Is Nothing) Then
name = wsDataSource.Range(CNameSourceColumn & n).Text
Else
name = CSourceWorksheet.Range(CNameSourceColumn & n).Text
End If
Wend
If (CNameColumn <> "") Then
CWorksheet.Range(CNameColumn & CRow).Value2 = lstSelected.List(lstSelected.ListIndex)
End If
If (CCodeColumn <> "") Then
CWorksheet.Range(CCodeColumn & CRow).Value2 = code
End If
CWorksheet.Protect Password
g. VBA programming to validate the data filled in every sheets
If Not CheckDateType(ws.Cells(i, 2).Value) Then
bCheck = False
MsgBox ws.Cells(i, 1).Value & "ÄÚÊäÈëµÄ²»ÊÇÓÐЧʱ¼ä"
ws.Activate
ws.Cells(i, 2).Select
Exit Sub
End If
.....

Function CheckNumberic(sourceString As String) As Boolean
Dim bOk As Boolean
bOk = True
If sourceString <> "" And Not IsNumeric(sourceString) Then
bOk = False
End If
CheckNumberic = bOk
End Function


Function CheckDateType(sourceString As String) As Boolean
Dim bOk As Boolean
bOk = False
If sourceString <> "" And IsDate(sourceString) Then
bOk = True
End If
CheckDateType = bOk
End Function
2.
2005年7月19日
2005年5月30日
2005年1月5日
很久之前碰到的问题,使用XMLHTTP boject打开另外一个网站的asp文件,结果不能返回需要的数据,只是返回了个错误号503。当时查了资料,说是安全验证没有通过,到MS的newsgroup上询问这个问题,也没有能够得到解决方案 前几天MS的人给出如下的方案: 1.代码里面创建验证的凭证,但是没有办法得到用户的密码,所以此路不通 2.使用站点间的public token,间接完成验证功能。
第一个方法肯定不行,当时测试过。第二个方法还没有开始测试,正在查站点之间如何交互public token,有什么进展会在这里更新。
大家新年好心情,新年新气象 ^_^
2004年10月2日
1.「你們人類自己的罪過,沒事遷拖到我身上來做啥?」
2.「我就是邪惡的化身,如何?有錢賺嗎?」
3.「當初那個獨裁專制不容異己的大混蛋,把我趕出家園不說, 還教壞無知的 人類憎恨我。明明自己在幹壞事,還要叫我當代罪羔羊?」
4.「是、是、是......如果我手下有那麼多惡魔,我早就統一宇宙了, 還需要 跟你爭一小個地球嗎?」
5.「地球上有一堆笨蛋自稱是我的信徒。卻不知道我根本用不著這些成事不足、 目光短淺、只知道破壞的白痴。」
6.「時代在進步,那本叫做聖經的書早跟時代脫節兩千年啦。」
7.「毀滅世界對我有好處嗎?少蠢了,正大光明準備製造世界末日的傢伙就是 那個叫做耶和華的人。」
8.「印度人、中國人、愛斯基摩人就不相信我,那你說我存不存在?」
9.「你的錢都進了誰的口袋?不是耶和華的,也不是我的,是那些神棍的。」
10.「當你需要神的時候,他在哪裡? 一樣的道理,當你需要我的時候,我可 沒空理你這個微不足道的白痴。」
20 樓跳下和2樓跳下的差別
從20樓跳下來: 阿阿阿阿阿阿阿阿阿阿阿阿阿!!!!!!!!!砰
從2樓跳下來: 砰!!!!!!!!!!阿阿阿阿阿阿阿阿阿阿阿阿阿!!!!....
|