Click here to Skip to main content
15,558,869 members
Home / Discussions / C#
   

C#

 
GeneralRe: Get file size - while inuse by another process Pin
mav.northwind1-Jul-07 3:00
mav.northwind1-Jul-07 3:00 
AnswerRe: Get file size - while inuse by another process Pin
Nissim Salomon1-Jul-07 1:04
Nissim Salomon1-Jul-07 1:04 
GeneralRe: Get file size - while inuse by another process Pin
liqnit1-Jul-07 4:20
liqnit1-Jul-07 4:20 
QuestionStreaming desktop over internet Pin
jbmoens30-Jun-07 23:26
jbmoens30-Jun-07 23:26 
AnswerRe: Streaming desktop over internet Pin
.jpg1-Jul-07 7:56
.jpg1-Jul-07 7:56 
QuestionFormula validation Pin
Bojja Lakshmi30-Jun-07 22:40
Bojja Lakshmi30-Jun-07 22:40 
AnswerRe: Formula validation Pin
Christian Graus30-Jun-07 23:10
mveChristian Graus30-Jun-07 23:10 
Questionwhy DataView.RowFilter run more solwer then DataTable.Select Pin
alonesword30-Jun-07 22:02
alonesword30-Jun-07 22:02 
why DataView.RowFilter is more solwer than DataTable.Select function?

using the following code to test.

class Program
{
const int ROWS_COUNT = 50; // 设置处理数据大小。 单位:w 万
const int ROWS_COUNT_UNIT = 10000; // 数据行单位:万
const double ROWS_TOTAL_COUNT = ROWS_COUNT * ROWS_COUNT_UNIT; // 总循环次数
const double ROWS_SHOW_INFO_LINE = ROWS_TOTAL_COUNT / 10; // 显示进度
private static string SAVE_FILE_NAME = string.Format("{0}w_{1}", ROWS_COUNT.ToString(), DateTime.Now.ToString("yyyyMMddhhmmss"));
private static StringBuilder builder = new StringBuilder(string.Format("ROWS COUNT :{0}w\n", ROWS_COUNT));
private static DataTable mDataTable = null;
private static DataView dv = null;
private static string str = string.Empty;

static void Main(string[] args)
{
// 添加数据行
DateTime start = DateTime.Now;
AddData();
str = string.Format("cost [ {0} ms ] to add [ {1}w ] rows.\n{2}\n",
(DateTime.Now.Subtract(start).TotalMilliseconds.ToString()),
ROWS_COUNT.ToString(),
"----- AddData() END -----\n");
Console.WriteLine(str);
builder.AppendFormat(str);

// 添加 DataTable 的主键
start = DateTime.Now;
AddPrimaryKey(true);
str = string.Format("cost [ {0} ms ] to AddPrimaryKey().\n{1}\n",
(DateTime.Now.Subtract(start).TotalMilliseconds.ToString()),
"----- AddPrimaryKey() END -----\n");
Console.WriteLine(str);
builder.AppendFormat(str);

// 循环进行 10 次数据过滤测试
start = DateTime.Now;
int times = 0;
string enter = string.Empty;
while ((++times) < 11)
{
str = string.Format("running {0} times ...\n", times.ToString());
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine(str);
Console.ResetColor();
builder.AppendFormat(str);
Test();
builder.Append("\n\n");
}
str = string.Format("\ncost [ {0} ms ] to WHILE loop.\n",
(DateTime.Now.Subtract(start).TotalMilliseconds.ToString()));
Console.WriteLine(str);
builder.AppendFormat(str);

// 测试结果输入到 文件中
WriteToFile(builder.ToString());

// 提示用户操作
Console.WriteLine("Press any key to continue ...");
Console.ReadLine();
}

///
/// 创建dataTable
///

private static void AddData()
{
Console.WriteLine("Adding data ... ");

mDataTable = null;
mDataTable = new DataTable("test");

DataColumn dc = new DataColumn("index", typeof(double));
dc.DefaultValue = 3000;
mDataTable.Columns.Add(dc);
mDataTable.Columns.Add(new DataColumn("content", typeof(string)));

DateTime start = DateTime.Now;
TimeSpan end;

double rowsCount = ROWS_TOTAL_COUNT + 1;
DataRow row = null;
for (int i = 1; i < rowsCount; i++)
{
if (0 == (i % ROWS_SHOW_INFO_LINE))
{
end = DateTime.Now.Subtract(start);
str = string.Format("Readed {0} rows [ {1} ms]...\n\n", i.ToString(), end.TotalMilliseconds.ToString());
Console.WriteLine(str);
builder.AppendFormat(str);
start = DateTime.Now;
}
// 1000w 数据时,出现了 SystemOutOfMemory 的异常
row = mDataTable.NewRow();

//row["index"] = i;
row["content"] = i.ToString();

mDataTable.Rows.Add(row);
}

mDataTable.AcceptChanges();

Console.Clear();
}

///
/// 添加主键值
///

/// <param name="add" />
private static void AddPrimaryKey(bool add)
{
Console.WriteLine("Adding Primarykey ...");
DateTime start;

if (add)
{
// add PrimaryKey columns
int key = mDataTable.PrimaryKey.Length;
DataColumn[] dcs = new DataColumn[key + 2];
dcs[key + 0] = mDataTable.Columns["index"];
dcs[key + 1] = mDataTable.Columns["content"];

start = DateTime.Now;
mDataTable.PrimaryKey = dcs;
str = string.Format("cost [ {0} ms ] mDataTable.PrimaryKey...\n", (DateTime.Now.Subtract(start).TotalMilliseconds.ToString()));
Console.WriteLine(str);
builder.AppendFormat(str);
}

start = DateTime.Now;
dv = null;
dv = mDataTable.DefaultView;
str = string.Format("cost [ {0} ms ] mDataTable.DefaultView...\n", (DateTime.Now.Subtract(start).TotalMilliseconds.ToString()));
Console.WriteLine(str);
builder.AppendFormat(str);

builder.Append("PrimaryKey Columns Details;\n");
foreach (DataColumn dc in mDataTable.PrimaryKey)
{
builder.AppendFormat("Column Name: \t{0}\nColumn DataType: \t{1}\n", dc.ColumnName, dc.DataType.Name);
}
}

///
/// 数据筛选测试
///

private static void Test()
{
const string CONDITIONS_1 = "index < 80000";
const string CONDITIONS_2 = "index < 80000 or (content LIKE '%200%')";

SelectFromDatatable(CONDITIONS_1);
SelectFromDatatable(CONDITIONS_2);

FilterDataView(CONDITIONS_1);
FilterDataView(CONDITIONS_2);
}

///
/// 将结果记录到文件
///

/// <param name="content" /><文件内容/param>
private static void WriteToFile(string content)
{
try
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(string.Format("{0}.txt", SAVE_FILE_NAME), false, Encoding.UTF8);
sw.Write(content);
sw.Close();
sw = null;
}
catch (Exception exp)
{
System.Diagnostics.Debug.WriteLine(exp.Message);
}
}

///
/// 通过DataTabl.Select 过滤数据
///

/// <param name="condtions" />
private static void SelectFromDatatable(string condtions)
{
DateTime start = DateTime.Now;
start = DateTime.Now;
mDataTable.Select(condtions);
builder.AppendFormat("mDataTable.Select(\"{0}\"):\n\t{1} ms\n", condtions, (DateTime.Now.Subtract(start).TotalMilliseconds.ToString()));
}

///
/// 通过 DataTable.DefaultView.RowFilter 过滤数据
///

/// <param name="conditions" />
private static void FilterDataView(string conditions)
{
DateTime start = DateTime.Now;
start = DateTime.Now;
dv.RowFilter = conditions;
builder.AppendFormat("dv.RowFilter = \"{0}\": \n\t{1} ms\n", conditions, (DateTime.Now.Subtract(start).TotalMilliseconds.ToString()));
}
}

宝剑锋从磨砺出,梅花香自苦寒来



我欲仗剑走天涯

Questionhow to move in records? Pin
s_nazari@yahoo.com30-Jun-07 21:52
s_nazari@yahoo.com30-Jun-07 21:52 
AnswerRe: how to move in records? Pin
Christian Graus30-Jun-07 22:22
mveChristian Graus30-Jun-07 22:22 
GeneralRe: how to move in records? Pin
Robert Rohde1-Jul-07 3:49
Robert Rohde1-Jul-07 3:49 
QuestionRead width & Height of jpeg file Pin
Alaric_30-Jun-07 21:52
professionalAlaric_30-Jun-07 21:52 
AnswerRe: Read width & Height of jpeg file Pin
Mazdak30-Jun-07 21:59
Mazdak30-Jun-07 21:59 
GeneralRe: Read width &amp Height of jpeg file Pin
Alaric_30-Jun-07 22:11
professionalAlaric_30-Jun-07 22:11 
AnswerRe: Read width & Height of jpeg file Pin
Luc Pattyn1-Jul-07 5:23
sitebuilderLuc Pattyn1-Jul-07 5:23 
GeneralRe: Read width & Height of jpeg file Pin
Alaric_1-Jul-07 7:29
professionalAlaric_1-Jul-07 7:29 
GeneralRe: Read width & Height of jpeg file Pin
Luc Pattyn1-Jul-07 7:40
sitebuilderLuc Pattyn1-Jul-07 7:40 
GeneralRe: Read width & Height of jpeg file Pin
Alaric_1-Jul-07 7:45
professionalAlaric_1-Jul-07 7:45 
GeneralRe: Read width & Height of jpeg file Pin
Luc Pattyn1-Jul-07 7:55
sitebuilderLuc Pattyn1-Jul-07 7:55 
GeneralRe: Read width & Height of jpeg file Pin
Alaric_1-Jul-07 8:06
professionalAlaric_1-Jul-07 8:06 
GeneralRe: Read width & Height of jpeg file Pin
Luc Pattyn1-Jul-07 8:15
sitebuilderLuc Pattyn1-Jul-07 8:15 
GeneralRe: Read width & Height of jpeg file Pin
Alaric_1-Jul-07 9:01
professionalAlaric_1-Jul-07 9:01 
GeneralRe: Read width & Height of jpeg file Pin
Alaric_1-Jul-07 9:15
professionalAlaric_1-Jul-07 9:15 
GeneralRe: Read width & Height of jpeg file Pin
Alaric_1-Jul-07 10:14
professionalAlaric_1-Jul-07 10:14 
GeneralRe: Read width &amp; Height of jpeg file Pin
Luc Pattyn1-Jul-07 10:20
sitebuilderLuc Pattyn1-Jul-07 10:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.