﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>博客园-郝文标的博客</title><link>http://www.cnblogs.com/haowenbiao/</link><description /><language>zh-cn</language><lastBuildDate>Thu, 21 Aug 2008 16:45:29 GMT</lastBuildDate><pubDate>Thu, 21 Aug 2008 16:45:29 GMT</pubDate><ttl>60</ttl><item><title>SQL Server Backup file in standard Zip format</title><link>http://www.cnblogs.com/haowenbiao/archive/2008/08/19/1270970.html</link><dc:creator>恭喜发财</dc:creator><author>恭喜发财</author><pubDate>Tue, 19 Aug 2008 03:01:00 GMT</pubDate><guid>http://www.cnblogs.com/haowenbiao/archive/2008/08/19/1270970.html</guid><wfw:comment>http://www.cnblogs.com/haowenbiao/comments/1270970.html</wfw:comment><comments>http://www.cnblogs.com/haowenbiao/archive/2008/08/19/1270970.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/haowenbiao/comments/commentRss/1270970.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/haowenbiao/services/trackbacks/1270970.html</trackback:ping><description><![CDATA[摘要: Back_InZipFormat.zipIntroductionThis is SQL Server Backup and restore tool, the system will store the backup files in standard Zip format, the user-friendly screen let you backup and restore SQL Serve&nbsp;&nbsp;<a href='http://www.cnblogs.com/haowenbiao/archive/2008/08/19/1270970.html'>阅读全文</a><img src ="http://www.cnblogs.com/haowenbiao/aggbug/1270970.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/41822/" target="_blank">[新闻]第一财经周刊:当前互联网世界正处无秩序时代</a>]]></description></item><item><title>Working with binary large objects (BLOBs)</title><link>http://www.cnblogs.com/haowenbiao/archive/2008/08/19/1270931.html</link><dc:creator>恭喜发财</dc:creator><author>恭喜发财</author><pubDate>Tue, 19 Aug 2008 02:31:00 GMT</pubDate><guid>http://www.cnblogs.com/haowenbiao/archive/2008/08/19/1270931.html</guid><wfw:comment>http://www.cnblogs.com/haowenbiao/comments/1270931.html</wfw:comment><comments>http://www.cnblogs.com/haowenbiao/archive/2008/08/19/1270931.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/haowenbiao/comments/commentRss/1270931.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/haowenbiao/services/trackbacks/1270931.html</trackback:ping><description><![CDATA[<p><strong>sample:</strong><strong><a title="WorkingWithBLOBs.zip" href="http://www.cnblogs.com/Files/haowenbiao/WorkingWithBLOBs.zip">WorkingWithBLOBs.zip</a></strong></p>
<p><strong>Introduction:</strong><br />
<br />
You can define a BLOB as a large photo, document, audio etc. saved in binary formats that you want to save in a database.<br />
Saving and retrieving BLOBs in a database is more complex than querying string or numeric data.<br />
<br />
The BLOB may be very large and if you try to move it in one piece will consume a lot of system memory and that for sure will affect your application performance. <br />
<br />
To reduce the amount of system memory you have to break up the BLOB into smaller pieces.<br />
<br />
There are a lot of classes that are designed for moving large amount of binary data like BinaryRader, BinaryWriter which exists in System.IO namespace. In the next paragraphs you will see how to use all of this.<br />
<br />
<strong>Saving a BLOB value to the database:<br />
</strong>&nbsp;<br />
To save a BLOB value to database we use FileStream and BinaryReader classes. <br />
<br />
The next example will show you the process of saving a BLOB to a database.</p>
<span style="font-size: 10pt; color: blue; font-family: Verdana">
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: blue; font-family: Verdana">string</span></span><span style="font-size: 10pt; font-family: Verdana"> filePath = @<span style="color: maroon">"D:""My Movie.wmv";</span></p>
</span>
<p style="margin: 0in 0in 0pt">&nbsp;</p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: green; font-family: Verdana">//A stream of bytes that represnts the binary file</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: teal; font-family: Verdana">FileStream</span><span style="font-size: 10pt; font-family: Verdana"> fs = <span style="color: blue">new <span style="color: teal">FileStream</span>(filePath, <span style="color: teal">FileMode</span>.Open, <span style="color: teal">FileAccess</span>.Read);</span></p>
</span>
<p style="margin: 0in 0in 0pt">&nbsp;</p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: green; font-family: Verdana">//The reader reads the binary data from the file stream</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: teal; font-family: Verdana">BinaryReader</span><span style="font-size: 10pt; font-family: Verdana"> reader = <span style="color: blue">new <span style="color: teal">BinaryReader</span>(fs);</span></p>
</span>
<p style="margin: 0in 0in 0pt">&nbsp;</p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: green; font-family: Verdana">//Bytes from the binary reader stored in BlobValue array</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: blue; font-family: Verdana">byte</span><span style="font-size: 10pt; font-family: Verdana">[] BlobValue = reader.ReadBytes((<span style="color: blue">int)fs.Length);</span></p>
</span>
<p style="margin: 0in 0in 0pt">&nbsp;</p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">fs.Close();</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">reader.Close();</p>
</span>
<p style="margin: 0in 0in 0pt">&nbsp;</p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: teal; font-family: Verdana">SqlConnection</span><span style="font-size: 10pt; font-family: Verdana"> BlobsDatabaseConn = <span style="color: blue">new <span style="color: teal">SqlConnection</span>(</span></span><span style="font-size: 10pt; font-family: Verdana"><span style="color: maroon">"Data Source = .; Initial Catalog = BlobsDatabase; Integrated Security = SSPI");<br />
</span></span><span style="font-size: 10pt; color: teal; font-family: Verdana">SqlCommand</span><span style="font-size: 10pt; font-family: Verdana"> SaveBlobeCommand = <span style="color: blue">new <span style="color: teal">SqlCommand</span>();</span></p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">SaveBlobeCommand.Connection = BlobsDatabaseConn;</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">SaveBlobeCommand.CommandType = <span style="color: teal">CommandType.Text;</span></p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">SaveBlobeCommand.CommandText = <span style="color: maroon">"INSERT INTO BlobsTable(BlobFileName, BlobFile)" + <span style="color: maroon">"VALUES (@BlobFileName, @BlobFile)"</span>;</span></span>
<p style="margin: 0in 0in 0pt"></p>
<p><span style="font-size: 10pt; color: teal; font-family: Verdana">SqlParameter</span><span style="font-size: 10pt; font-family: Verdana"> BlobFileNameParam = <span style="color: blue">new <span style="color: teal">SqlParameter</span>(<span style="color: maroon">"@BlobFileName"</span>, <span style="color: teal">SqlDbType</span>.NChar);</span></p>
<p>&nbsp;</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: teal; font-family: Verdana">SqlParameter</span><span style="font-size: 10pt; font-family: Verdana"> BlobFileParam = <span style="color: blue">new <span style="color: teal">SqlParameter</span>(<span style="color: maroon">"@BlobFile"</span>, <span style="color: teal">SqlDbType</span>.Binary);</span></p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">SaveBlobeCommand.Parameters.Add(BlobFileNameParam);</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">SaveBlobeCommand.Parameters.Add(BlobFileParam);</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">BlobFileNameParam.Value = filePath.Substring(filePath.LastIndexOf(<span style="color: maroon">"""") + 1);</span></p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">BlobFileParam.Value = BlobValue;</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: blue; font-family: Verdana">try</span> </p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">{</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp; SaveBlobeCommand.Connection.Open();</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp; SaveBlobeCommand.ExecuteNonQuery();</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp; <span style="color: teal">MessageBox</span>.Show(BlobFileNameParam.Value.ToString() + <span style="color: maroon">" saved to database."</span>,<span style="color: maroon">"BLOB Saved"</span>, <span style="color: teal">MessageBoxButtons</span>.OK, <span style="color: teal">MessageBoxIcon</span>.Information);</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">}<br />
</span>
<p style="margin: 0in 0in 0pt"></p>
<p><span style="font-size: 10pt; color: blue; font-family: Verdana">catch</span><span style="font-size: 10pt; font-family: Verdana">(<span style="color: teal">Exception ex)</span></p>
<p>&nbsp;</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">{</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: teal; font-family: Verdana">&nbsp;&nbsp;&nbsp; MessageBox</span><span style="font-size: 10pt; font-family: Verdana">.Show(ex.Message, <span style="color: maroon">"Save Failed", <span style="color: teal">MessageBoxButtons</span>.OK, <span style="color: teal">MessageBoxIcon</span>.Error);</span></p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">}</span>
<p style="margin: 0in 0in 0pt"></p>
<p><span style="font-size: 10pt; color: blue; font-family: Verdana">finally</p>
<p>&nbsp;</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">{</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp; SaveBlobeCommand.Connection.Close();</p>
<p style="margin: 0in 0in 0pt"></span><span style="font-size: 10pt; font-family: Verdana">}<br />
<br />
</span></span><strong>Retrieving a BLOB from the database:<br />
</strong><br />
To retrieve a BLOB value from database we use FileStream and BinaryWriter classes. <br />
<br />
The next example will show you the process of retrieving a BLOB to a database.<br />
<br />
<strong><u><em>NOTE:</em></u></strong> you will see that we set the CommandBehavior to SquentialAccess when we call ExecuteReader() method, this allow us to use the GetBytes() method of the SqlDataRader, so we can read the BLOB from database in smaller, user-definable amounts.<br />
<span style="font-size: 10pt; color: blue; font-family: Verdana"><br />
string</span></span><span style="font-size: 10pt; font-family: Verdana"> SavePath = @<span style="color: maroon">"D:""My BLOBs";</span></p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: teal; font-family: Verdana">SqlConnection</span><span style="font-size: 10pt; font-family: Verdana"> SaveConn = <span style="color: blue">new <span style="color: teal">SqlConnection</span>(<span style="color: maroon">"Data Source = .; Initial Catalog = BlobsDatabase; Integrated Security = SSPI"</span>);</span></p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: teal; font-family: Verdana">SqlCommand</span><span style="font-size: 10pt; font-family: Verdana"> SaveCommand = <span style="color: blue">new <span style="color: teal">SqlCommand</span>();</span></p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">SaveCommand.CommandText = <span style="color: maroon">"Select BlobFileName, BlobFile from BlobsTable where BlobFileName = @BlobFileName";</span></p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">SaveCommand.Connection = SaveConn;</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">SaveCommand.Parameters.Add(<span style="color: maroon">"@BlobFileName", <span style="color: teal">SqlDbType</span>.NVarChar).Value = <span style="color: maroon">"My Movie.wmv"</span>;</span></p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: green; font-family: Verdana"><br />
//the index number to write bytes to</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: blue; font-family: Verdana">long</span><span style="font-size: 10pt; font-family: Verdana"> CurrentIndex = 0;</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: green; font-family: Verdana"><br />
//the number of bytes to store in the array</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: blue; font-family: Verdana">int</span><span style="font-size: 10pt; font-family: Verdana"> BufferSize = 100;</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: green; font-family: Verdana"><br />
//The Number of bytes returned from GetBytes() method</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: blue; font-family: Verdana">long</span><span style="font-size: 10pt; font-family: Verdana"> BytesReturned;</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: green; font-family: Verdana"><br />
//A byte array to hold the buffer</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: blue; font-family: Verdana">byte</span><span style="font-size: 10pt; font-family: Verdana">[] Blob = <span style="color: blue">new <span style="color: blue">byte</span>[BufferSize];</span></p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><br />
SaveCommand.Connection.Open();</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: green; font-family: Verdana"><br />
//We set the CommandBehavior to SequentialAccess</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: green; font-family: Verdana">//so we can use the SqlDataReader.GerBytes() method.</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: teal; font-family: Verdana"><br />
SqlDataReader</span><span style="font-size: 10pt; font-family: Verdana"> reader = SaveCommand.ExecuteReader(<span style="color: teal">CommandBehavior.SequentialAccess);</span></p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: blue; font-family: Verdana"><br />
while</span><span style="font-size: 10pt; font-family: Verdana"> (reader.Read())</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">{</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: teal; font-family: Verdana">&nbsp;&nbsp;&nbsp; FileStream</span><span style="font-size: 10pt; font-family: Verdana"> fs = <span style="color: blue">new <span style="color: teal">FileStream</span>(SavePath + <span style="color: maroon">""""</span> + reader[<span style="color: maroon">"BlobFileName"</span>].ToString(), <span style="color: teal">FileMode</span>.OpenOrCreate, <span style="color: teal">FileAccess</span>.Write);</span></span>
<p style="margin: 0in 0in 0pt"></p>
<p><span style="font-size: 10pt; color: teal; font-family: Verdana">&nbsp;&nbsp;&nbsp; BinaryWriter</span><span style="font-size: 10pt; font-family: Verdana"> writer = <span style="color: blue">new <span style="color: teal">BinaryWriter</span>(fs);</span></span> </p>
<p style="margin: 0in 0in 0pt"></p>
<p><span style="font-size: 10pt; color: green; font-family: Verdana">&nbsp;&nbsp;&nbsp; //reset the index to the beginning of the file</p>
<p>&nbsp;</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp; CurrentIndex = 0;</span>
<p style="margin: 0in 0in 0pt"></p>
<p><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp; BytesReturned = reader.GetBytes(</span><span style="font-size: 10pt; font-family: Verdana">1, <span style="color: green">//the BlobsTable column index</span></span><span style="font-size: 10pt; font-family: Verdana">CurrentIndex, <span style="color: green">// the current index of the field from which to begin the read operation</span></span><span style="font-size: 10pt; font-family: Verdana">Blob, <span style="color: green">// Array name to write tha buffer to</span></span><span style="font-size: 10pt; font-family: Verdana">0, <span style="color: green">// the start index of the array to start the write operation</span></span><span style="font-size: 10pt; font-family: Verdana">BufferSize <span style="color: green">// the maximum length to copy into the buffer</span></span><span style="font-size: 10pt; font-family: Verdana">);</span>&nbsp;</p>
<p>&nbsp;</p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp; <span style="color: blue">while</span> (BytesReturned == BufferSize)</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp; {</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; writer.Write(Blob);</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; writer.Flush();<br />
</span><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CurrentIndex += BufferSize;</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BytesReturned = reader.GetBytes(1, CurrentIndex, Blob, 0, BufferSize);</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp; }</span>&nbsp;</p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp; writer.Write(Blob, 0, (<span style="color: blue">int</span>)BytesReturned);</p>
</span>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp; writer.Flush(); writer.Close();</span>&nbsp;</p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp; fs.Close();</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">}<br />
</span>&nbsp;</p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">reader.Close();</p>
<p style="margin: 0in 0in 0pt"></span><span style="font-size: 10pt; font-family: Verdana">SaveCommand.Connection.Close();<br />
</span><br />
To fully understand the concept you need to try to write this code yourself.<br />
<br />
<strong><u><em>Note:</em></u></strong>&nbsp;The database and the full source code in the source code area with this article.</p>
<img src ="http://www.cnblogs.com/haowenbiao/aggbug/1270931.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/41822/" target="_blank">[新闻]第一财经周刊:当前互联网世界正处无秩序时代</a>]]></description></item><item><title>c# 得到局域网中可用SqlServer服务器列表(转)</title><link>http://www.cnblogs.com/haowenbiao/archive/2008/08/15/1268944.html</link><dc:creator>恭喜发财</dc:creator><author>恭喜发财</author><pubDate>Fri, 15 Aug 2008 11:57:00 GMT</pubDate><guid>http://www.cnblogs.com/haowenbiao/archive/2008/08/15/1268944.html</guid><wfw:comment>http://www.cnblogs.com/haowenbiao/comments/1268944.html</wfw:comment><comments>http://www.cnblogs.com/haowenbiao/archive/2008/08/15/1268944.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/haowenbiao/comments/commentRss/1268944.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/haowenbiao/services/trackbacks/1268944.html</trackback:ping><description><![CDATA[static void Main(string[] args)<br />
{<br />
// Retrieve the enumerator instance and then the data.<br />
SqlDataSourceEnumerator instance =SqlDataSourceEnumerator.Instance;<br />
System.Data.DataTable table = instance.GetDataSources();<br />
// Display the contents of the table.<br />
DisplayData(table);<br />
<br />
Console.WriteLine("Press any key to continue.");<br />
Console.ReadKey();<br />
}<br />
private static void DisplayData(System.Data.DataTable table)<br />
{<br />
foreach (System.Data.DataRow row in table.Rows)<br />
{<br />
foreach (System.Data.DataColumn col in table.Columns)<br />
{<br />
Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);<br />
}<br />
Console.WriteLine("===");<br />
}<br />
} 
<img src ="http://www.cnblogs.com/haowenbiao/aggbug/1268944.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/41820/" target="_blank">[新闻]Visual Studio 2008 SDK 1.1 发布</a>]]></description></item><item><title>C#实现UDP协议(转)</title><link>http://www.cnblogs.com/haowenbiao/archive/2008/08/15/1268931.html</link><dc:creator>恭喜发财</dc:creator><author>恭喜发财</author><pubDate>Fri, 15 Aug 2008 11:08:00 GMT</pubDate><guid>http://www.cnblogs.com/haowenbiao/archive/2008/08/15/1268931.html</guid><wfw:comment>http://www.cnblogs.com/haowenbiao/comments/1268931.html</wfw:comment><comments>http://www.cnblogs.com/haowenbiao/archive/2008/08/15/1268931.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/haowenbiao/comments/commentRss/1268931.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/haowenbiao/services/trackbacks/1268931.html</trackback:ping><description><![CDATA[<p align="left"><font class="f14" id="zoom">一.走进UDP协议：<br />
<br />
　　UDP（UserDatagramProtocol）协议就是&#8220;用户数据报协议&#8221;，它是一种无连接的协议，无连接主要是和TCP协议相比较的。我们知道当利用TCP协议传送数据的时候，首先必须先建立连接（也就是所谓的握手）才可以传输数据。而当计算机利用UDP协议进行数据传输的时候，发送方只需要知道对方的IP地址和端口号就可以发送数据，而并不需要进行连接。当然如果你非要进行连接，通过VisualC＃也是可以实现的，但前提是要确定连接的远程主机的端口号处于监听状态，否则程序会出现不必要的错误，但这是种画蛇添足的做法，不仅丢失了UDP协议的无连接传送数据的特点和优点，而且还给程序运行带来了不安定的因素。所以这种方法并不值得提倡。<br />
<br />
　　由于UDP协议并不需要进行确定的连接，所以编写基于UDP协议的应用程序比起编写基于TCP协议的应用程序要简单些（程序中可以不需要考虑连接和一些异常的捕获工作）。但同时也给基于UDP协议编写的程序带来了一个致命的缺点，UDP由于不提供可靠数据的传输，当计算机之间利用UDP协议传送数据的时候，发送方只管发送数据，而并不确认数据是否被对方接收。这样就会导致某些UDP协议数据包在传送的过程中丢失，尤其网络质量不令人满意的情况下，丢失数据包的现象会更严重。这就是为什么在网络上传输重要数据不采用UDP协议的原因。<br />
<br />
　　但是我们也不能因为这一个缺点就全面否定UDP协议，这是因为虽然利用UDP协议来传送安全性要求高的数据是不适合的，但对于那些不重要的数据，或者即使丢失若干数据包也不影响整体性的数据，如音频数据，视频数据等，采用UDP协议就是一个非常不错的选择。如目前网络流行的很多即时聊天程序，如OICQ和ICQ等，采用的就是UDP协议。同时虽然UDP协议无法保证数据可靠性，但具有对网络资源开销较小，数据处理速度快的优点，所以在有些对数据安全性要求不是很高的情况下，采用UDP协议也是一个非常不错的选择。<br />
<br />
　　总结一下上面的内容，可见UDP是一种不面向连接的网络协议，既有其优点，也有其不足，具体如下：<br />
<br />
1.基于UDP协议的网络应用程序，实现起来比较简单，并且基于UDP协议的网络应用程序在运行时，由于受到环境影响较小，所以不容易出错。<br />
<br />
2.UDP协议占用网络资源较少，数据处理较快，所以在网络中传送对安全性要求不是十分高数据时，其优点比较明显。所谓对安全性要求不高的数据，是指那些不重要的数据，或者是即使丢失若干数据，也不影响其整体的数据，如音频数据等。目前很多流行的网络应用程序都是基于UDP协议的，如OICQ、ICQ等。<br />
<br />
3.由于其不是面向连接的网络协议，其缺点也是非常明显的，有些时候甚至是致命的。因为使用UDP协议来传送数据，在数据发送后，在发送方并不确认对方是否接收到。这样就可能导致传送的数据在网络中丢失，尤其在网络条件并不很好的情况下，丢失数据包的现象就更多。所以传送重要数据一般不采用UDP协议。<br />
<br />
二．简介VisualC＃发送、接收UDP数据包使用的主要类及其用法:<br />
<br />
　　用VisualC＃实现UDP协议，最为常用，也是最为关键的类就是UdpClient，UdpClient位于命名空间System.Net.Sockets中，VisualC＃发送、接收UDP数据包都是通过UdpClient类的。表01和表02是UdpClient类中常用方法和属性及其简要说明。<br />
<table border="1">
    <tr>
            <td>方法</td>
            <td>说明</td>
        </tr>
        <tr>
            <td>Close</td>
            <td>关闭 UDP 连接</td>
        </tr>
        <tr>
            <td>Connect</td>
            <td>建立与远程主机的连接</td>
        </tr>
        <tr>
            <td>DropMulticastGroup</td>
            <td>退出多路广播组</td>
        </tr>
        <tr>
            <td>JoinMulticastGroup</td>
            <td>将 UdpClient 添加到多路广播组</td>
        </tr>
        <tr>
            <td>Receive</td>
            <td>返回已由远程主机发送的 UDP 数据文报</td>
        </tr>
        <tr>
            <td>Send</td>
            <td>将 UDP 数据文报发送到远程主机</td>
        </tr>
    </table>
</ccid_nobr><br />
<br />
表01：UdpClient类中常用方法及其说明。 <br />
<br />
<ccid_nobr>
<table border="1">
    <tr>
            <td>属性</td>
            <td>说明</td>
        </tr>
        <tr>
            <td>Active</td>
            <td>获取或设置一个值，该值指示是否已建立了与远程主机的连接</td>
        </tr>
        <tr>
            <td>Client</td>
            <td>获取或设置基础网络套接字</td>
        </tr>
    </table>
<p></ccid_nobr>表02：UdpClient类中常用方法及其说明。<br />
<br />
1.Visual C＃使用UdpClient类发送UDP数据包： <br />
<br />
在具体使用中，一般分成二种情况： <br />
<br />
（1）. 知道远程计算机IP地址： <br />
<br />
"Send"方法的调用语法如下：</p>
<p><br />
<ccid_nobr>
<table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1">
    <tr>
            <td class="code" style="font-size: 9pt" bgcolor="#e6e6e6">
            <pre><ccid_code>public int Send (
            byte[] dgram ,
            int bytes ,
            IPEndPoint endPoint
            ) ;</ccid_code></pre>
            </td>
        </tr>
    </table>
</ccid_nobr><br />
<br />
参数说明： <br />
<br />
dgram 要发送的 UDP 数据文报（以字节数组表示）。 <br />
<br />
bytes 数据文报中的字节数。 <br />
<br />
endPoint 一个 IPEndPoint，它表示要将数据文报发送到的主机和端口。 <br />
<br />
返回值 已发送的字节数。 <br />
<br />
下面使用UdpClient发送UDP数据包的具体的调用例子： <br />
<br />
<ccid_nobr>
<table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1">
    <tr>
            <td class="code" style="font-size: 9pt" bgcolor="#e6e6e6">
            <pre><ccid_code>IPAddress  HostIP = new IPAddress.Parse  ( "远程计算机IP地址" ) ;
            IPEndPoint host = new IPEndPoint ( HostIP , 8080 ) ;
            UdpClient.Send ( "发送的字节" , "发送的字节长度" , host ) ;</ccid_code></pre>
            </td>
        </tr>
    </table>
</ccid_nobr><br />
<br />
（2）. 知道远程计算机名称:： <br />
<br />
知道远程计算机名称后，利用"Send"方法直接把UDP数据包发送到远程主机的指定端口号上了，这种调用方式也是最容易的，语法如下： <br />
<br />
<ccid_nobr>
<table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1">
    <tr>
            <td class="code" style="font-size: 9pt" bgcolor="#e6e6e6">
            <pre><ccid_code>public int Send (
            byte[ ] dgram ,
            int bytes ,
            string hostname ,
            int port
            ) ;</ccid_code></pre>
            </td>
        </tr>
    </table>
</ccid_nobr><br />
<br />
参数说明： <br />
<br />
dgram 要发送的 UDP 数据文报（以字节数组表示）。 <br />
<br />
bytes 数据文报中的字节数。 <br />
<br />
hostname 要连接到的远程主机的名称。 <br />
<br />
port 要与其通讯的远程端口号。 <br />
<br />
返回值 已发送的字节数。 <br />
<br />
2.Visual C＃使用UdpClient类接收UDP数据包： <br />
<br />
接收UDP数据包使用的是UdpClient中的&#8220;Receive&#8221;方法。此方法的调用语法如下： <br />
<br />
<ccid_nobr>
<table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1">
    <tr>
            <td class="code" style="font-size: 9pt" bgcolor="#e6e6e6">
            <pre><ccid_code>public byte [] Receive (
            ref IPEndPoint remoteEP
            ) ;</ccid_code></pre>
            </td>
        </tr>
    </table>
</ccid_nobr><br />
<br />
参数 <br />
<br />
remoteEP 是一个 IPEndPoint类的实例，它表示网络中发送此数据包的节点。 <br />
<br />
如果指定了远程计算机要发送到本地机的端口号，也可以通过侦听本地端口号来实现对数据的获取，下面就是通过侦听本地端口号&#8220;8080&#8221;来获取信息代码： <br />
<br />
<ccid_nobr>
<table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1">
    <tr>
            <td class="code" style="font-size: 9pt" bgcolor="#e6e6e6">
            <pre><ccid_code>server = new UdpClient ( ) ;
            receivePoint = new IPEndPoint
            (new IPAddress ( "127.0.0.1" ) , 8080 ) ;
            byte[] recData = server.Receive ( ref receivePoint ) ;</ccid_code></pre>
            </td>
        </tr>
    </table>
</ccid_nobr><br />
<br />
<strong>三．Visual C＃实现UDP协议之网络对时系统的体系结构及功能简介：</strong> <br />
<br />
在局域网中有很多应用软件为了协同工作，需要保证客户机上时间统一，而为了实现这一点，通常的做法是客户机从一个时间相对正确的服务器读取时间，以此来校正本地时间。如经常看到的GPS对时系统等。本节编写的局域网上对时系统的主要的功能是保证局域网上计算机时间、日期的统一。网络对时程序是体系结构分成服务器端程序和客户端程序二个部分，具体的作法是：在同一个网段上，固定一台计算机作为对时的服务器，在这个网段的所有计算机都可以读取这台服务器上的时间和日期，并依此服务器上的时间和日期为基准，来确定本地的时间和日期。在服务器端程序需要达到以下功能： <br />
<br />
能够接收局域网中任一台客户机的请求 <br />
<br />
记录请求客户机的计算机名称，和请求时间 <br />
<br />
准确发送服务器端的时间和日期 <br />
<br />
端程序要达到以下功能： <br />
<br />
能够设定服务器的主机或者IP地址 <br />
<br />
能够接收服务器端发送的时间、日期信息 <br />
<br />
能够以接收的服务器端时间、日期为基准，校正本地时间 <br />
<br />
因此在具体用Visual C＃实现网络对时系统时就包括二个部分：服务器端程序和客户端程序。下面首先介绍Visual C＃实现网络对时系统中服务器端程序的具体步骤。 <br />
<br />
<strong>四．Visual C＃实现网络对时系统之服务器端程序的具体步骤：</strong> <br />
<br />
服务器端程序比客户端程序相对要简单，主要因是服务器端程序的工作比较简单，就 <br />
<br />
是接收客户端的对时请求、发送服务器端的时间数据。而于客户端不仅要传送和接收数据，还要把服务器端的时间提取出来，并以此来修改本地计算机的时间、日期。下面是用Visual C＃实现网络对时系统之服务器端程序的具体步骤步骤。 <br />
<br />
1.启动Visual Studio .Net。 <br />
<br />
2.选择菜单【文件】|【新建】|【项目】后，弹出【新建项目】对话框。 <br />
<br />
3.将【项目类型】设置为【Visual C＃项目】。 <br />
<br />
4.将【模板】设置为【Windows应用程序】。 <br />
<br />
5.在【名称】文本框中输入【UDP对时服务器端】。 <br />
<br />
6.在【位置】的文本框中输入【E:\VS.NET项目】，然后单击【确定】按钮 <br />
<br />
7.在【解决方案资源管理器】窗口中，双击Form1.cs文件，进入Form1.cs文件的编辑界面。 <br />
<br />
8.在Form1.cs文件的开头，用下列导入命名空间代码替代系统缺省的导入命名空间代码。 <br />
<br />
<ccid_nobr>
<table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1">
    <tr>
            <td class="code" style="font-size: 9pt" bgcolor="#e6e6e6">
            <pre><ccid_code>using System ;
            using System.Drawing ;
            using System.Collections ;
            using System.ComponentModel ;
            using System.Windows.Forms ;
            using System.Data ;
            using System.Net ;
            using System.Net.Sockets ;
            using System.Threading ;
            //程序中使用到线程
            using System.Text ;
            //程序中使用到编码</ccid_code></pre>
            </td>
        </tr>
    </table>
</ccid_nobr><br />
<br />
9.切换到【Form1.cs（设计）】窗口，并从【工具箱】中的【Windows窗体组件】中往窗体中拖入下列组件，并执行相应操作： <br />
<br />
一个Label组件，显示对时服务器正在运行信息 <br />
<br />
一个ListBox组件，名称为listBox1，用以显示客户端和服务器端交流的日志 <br />
<br />
一个Button组件，名称为button1，并在其拖入窗体后，双击，则系统会在Form1.cs文件中产生其Click事件对应的处理代码。 <br />
<br />
10.在【解决方案资源管理器】窗口中，双击Form1.cs文件，进入Form1.cs文件的编辑界面。在Form1.cs中的class代码区添加下列代码，下列代码是定义程序中使用的全局变量和创建全局使用的实例： <br />
<br />
<ccid_nobr>
<table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1">
    <tr>
            <td class="code" style="font-size: 9pt" bgcolor="#e6e6e6">
            <pre><ccid_code>private UdpClient server ;
            private IPEndPoint receivePoint ;
            private int port = 8080 ;
            //定义端口号
            private int ip = 127001 ;
            //设定本地IP地址
            private Thread startServer ;</ccid_code></pre>
            </td>
        </tr>
    </table>
</ccid_nobr><br />
<br />
11.以下面代码替代系统产生的InitializeComponent过程。 <br />
<br />
<ccid_nobr>
<table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1">
    <tr>
            <td class="code" style="font-size: 9pt" bgcolor="#e6e6e6">
            <pre><ccid_code>private void InitializeComponent ( )
            {
            this.listBox1 = new System.Windows.Forms.ListBox ( ) ;
            this.label1 = new System.Windows.Forms.Label ( ) ;
            this.button1 = new System.Windows.Forms.Button ( ) ;
            this.SuspendLayout ( ) ;
            this.listBox1.ItemHeight = 12 ;
            this.listBox1.Location = new System.Drawing.Point ( 14 , 40 ) ;
            this.listBox1.Name = "listBox1" ;
            this.listBox1.Size = new System.Drawing.Size ( 268 , 220 ) ;
            this.listBox1.TabIndex = 0 ;
            this.label1.ForeColor = System.Drawing.Color.Red ;
            this.label1.Location = new System.Drawing.Point ( 44 , 10 ) ;
            this.label1.Name = "label1" ;
            this.label1.Size = new System.Drawing.Size ( 210 , 24 ) ;
            this.label1.TabIndex = 1 ;
            this.label1.Text = "UDP对时服务器端正在运行......" ;
            this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat ;
            this.button1.Location = new System.Drawing.Point ( 106 , 278 ) ;
            his.button1.Name = "button1" ;
            this.button1.Size = new System.Drawing.Size ( 75 , 34 ) ;
            this.button1.TabIndex = 2 ;
            this.button1.Text = "清除信息" ;
            this.button1.Click += new System.EventHandler ( this.button1_Click ) ;
            this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
            this.ClientSize = new System.Drawing.Size ( 300 , 329 ) ;
            this.Controls.AddRange ( new System.Windows.Forms.Control[] {
            this.button1 ,
            this.listBox1 ,
            this.label1} ) ;
            this.MaximizeBox = false ;
            this.Name = "Form1" ;
            this.Text = "UDP对时服务器端" ;
            this.Load += new System.EventHandler ( this.Form1_Load ) ;
            this.ResumeLayout ( false ) ;
            }</ccid_code></pre>
            </td>
        </tr>
    </table>
</ccid_nobr><br />
<br />
至此，【UDP对时服务器端】项目的界面设计和功能实现的前期工作就完成了，设计界面如图01所示： <br />
</p>
<p align="center"><img src="http://www.myfaq.com.cn/A-A-B/Image/2005/08/02/8154333666.jpg" border="0"  alt="" /></p>
<center><strong>图01：【UDP对时服务器端】项目的设计界面</strong></center>
<p>12.在Form1.cs文件中的InitializeComponent过程的后面添加下面代码，下列代码是定义过程&#8220;start_server&#8221;。此过程的功能是获取客户端对时请求数据，并向客户端发送服务器当前时间和日期。 <br />
<br />
</p>
<ccid_nobr>
<p>
<table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1">
    <tr>
            <td class="code" style="font-size: 9pt" bgcolor="#e6e6e6">
            <pre><ccid_code>public void start_server ( )
            {
            while ( true )
            {
            //接收从远程主机发送到本地8080端口的数据
            byte[] recData = server.Receive ( ref receivePoint ) ;
            ASCIIEncoding encode = new ASCIIEncoding ( ) ;
            //获得客户端请求数据
            string Read_str = encode.GetString ( recData ) ;
            //提取客户端的信息，存放到定义为temp的字符串数组中
            string[] temp = Read_str.Split ( "/".ToCharArray ( ) ) ;
            //显示端口号的请求信息
            listBox1.Items.Add ( "时间："+ DateTime.Now.
            ToLongTimeString ( ) + "   接收信息如下：" ) ;
            listBox1.Items.Add ( "客户机：" + temp[0] ) ;
            listBox1.Items.Add ( "端口号：" + temp[1] ) ;
            //发送服务器端时间和日期
            byte[] sendData =encode.GetBytes
            ( System.DateTime.Now.ToString ( ) ) ;
            listBox1.Items.Add ( "发送服务器时间！" ) ;
            //对远程主机的指定端口号发送服务器时间
            server.Send ( sendData , sendData.Length ,
            temp[0] , Int32.Parse ( temp[1] ) ) ;
            }
            }</ccid_code></pre>
            </td>
        </tr>
    </table>
</p>
</ccid_nobr>
<p><br />
<br />
请注意：上述代码中约定客户机程序发送对时请求信息到服务器的8080端口号。服务器端程序接收发送到本地8080端口号的数据就完成了数据接收。为了能够让服务器端程序知道是那台客户机提出请求和要把对时信息发送到客户机的那个端口号上，客户端程序对发送的对时请求信息进行了设计。客户端的对时请求信息结构为： <br />
<br />
计算机名称 + / + 客户机接收信息端口号 <br />
<br />
这样如果客户端计算机名称为：majinhu，接收服务器端时间数据的端口号是8080，则客户端程序发送的对时请求数据就为：majinhu/8080。 <br />
<br />
服务器端程序在接收到客户端对时请求数据，并进行分析后，就能够通过UdpClient类的Send方法准确的把服务器端当前的时间和日期发送到客户端指定的端口号上。这样客户端程序通过读取指定的端口号，就能够获得服务器端当前的时间和日期，从而以此来修正客户端的时间和日期了。 <br />
<br />
13.在&#8220;start_server&#8221;过程之后面添加下面代码，下列代码是定义&#8220;run&#8221;过程。&#8220;run&#8221;过程的作用是创建一个线程实例，并以&#8220;start_server&#8221;过程来初始化线程实例。之所以采用线程是因为服务器端程序需要不间断读取发送到8080端口号，并且Receive方法是一个阻塞式方法。采用线程就是为了保证服务器端程序正常运行： <br />
<br />
</p>
<ccid_nobr>
<p>
<table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1">
    <tr>
            <td class="code" style="font-size: 9pt" bgcolor="#e6e6e6">
            <pre><ccid_code>public void run ( )
            {
            //利用本地8080端口号来初始化一个UDP网络服务
            server = new UdpClient ( port ) ;
            receivePoint = new IPEndPoint ( new IPAddress ( ip ) , port ) ;
            //开一个线程
            startServer = new Thread ( new ThreadStart ( start_server ) ) ;
            //启动线程
            startServer.Start ( ) ;
            }</ccid_code></pre>
            </td>
        </tr>
    </table>
</p>
</ccid_nobr>
<p><br />
<br />
14.在Form1.cs中的Main函数之后添加下列代码，下列代码是定义&#8220;Form1_Load&#8221;事件，在此事件中将调用&#8220;run&#8221;过程，这样当服务器端程序运行后，就启动网络对时服务： <br />
<br />
</p>
<ccid_nobr>
<p>
<table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1">
    <tr>
            <td class="code" style="font-size: 9pt" bgcolor="#e6e6e6">
            <pre><ccid_code>private void Form1_Load ( object sender , System.EventArgs e )
            {
            //启动对时服务
            run ( ) ;
            }</ccid_code></pre>
            </td>
        </tr>
    </table>
</p>
</ccid_nobr>
<p><br />
<br />
15.在Form1.cs文件中的&#8220;Form1_Load&#8221;事件之后，添加下列代码，下列代码是定义button1的&#8220;Click&#8221;事件，此事件的作用是清除服务器端程序显示的日志信息： <br />
<br />
</p>
<ccid_nobr>
<p>
<table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1">
    <tr>
            <td class="code" style="font-size: 9pt" bgcolor="#e6e6e6">
            <pre><ccid_code>private void button1_Click ( object sender , System.EventArgs e )
            {
            //清除服务器端程序日志
            listBox1.Items.Clear ( ) ;
            }</ccid_code></pre>
            </td>
        </tr>
    </table>
</p>
</ccid_nobr>
<p><br />
<br />
16.用下列代码替换Form1.cs中的Dispose方法。下列代码的功能是手动收集程序中使用的资源： <br />
<br />
</p>
<ccid_nobr>
<p>
<table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="400" align="center" bordercolorlight="black" border="1">
    <tr>
            <td class="code" style="font-size: 9pt" bgcolor="#e6e6e6">
            <pre><ccid_code>protected override void Dispose (  bool disposing  )
            {
            try
            {
            //关闭线程
            startServer.Abort ( ) ;
            //清除资源
            server.Close ( ) ;
            }
            catch
            {
            } ;
            if (  disposing  )
            {
            if  ( components != null )
            {
            components.Dispose ( ) ;
            }
            }
            base.Dispose (  disposing  ) ;
            }</ccid_code></pre>
            </td>
        </tr>
    </table>
</p>
</ccid_nobr>
<p><br />
<br />
至此，在上述步骤都正确完成，【UDP对时服务器端】项目的全部工作就完成了。图02【UDP对时服务器端】运行后的界面，在日志信息中记录了对时请求客户机的名称，发送对时数据的端口号以及客户端请求的时间： </p>
<p><br />
&nbsp;</p>
<p align="center"><img src="http://www.myfaq.com.cn/A-A-B/Image/2005/08/02/8154859462.jpg" border="0"  alt="" /></p>
<center><strong>图02：【UDP对时<a href="http://www.myfaq.com.cn/System/Server/index.html" target="_blank">服务器</a>端】项目的运行界面</strong></center></font>
<img src ="http://www.cnblogs.com/haowenbiao/aggbug/1268931.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/41820/" target="_blank">[新闻]Visual Studio 2008 SDK 1.1 发布</a>]]></description></item><item><title>健康大讲堂—凡膳皆药 寓医于食</title><link>http://www.cnblogs.com/haowenbiao/archive/2008/08/06/1261774.html</link><dc:creator>恭喜发财</dc:creator><author>恭喜发财</author><pubDate>Wed, 06 Aug 2008 03:07:00 GMT</pubDate><guid>http://www.cnblogs.com/haowenbiao/archive/2008/08/06/1261774.html</guid><wfw:comment>http://www.cnblogs.com/haowenbiao/comments/1261774.html</wfw:comment><comments>http://www.cnblogs.com/haowenbiao/archive/2008/08/06/1261774.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/haowenbiao/comments/commentRss/1261774.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/haowenbiao/services/trackbacks/1261774.html</trackback:ping><description><![CDATA[<p><embed src="http://space.tv.cctv.com/playcfg/player_new.swf" width="500" height="400" type="application/x-shockwave-flash" flashvars="id=VIDE1217215175946741&amp;site=http://space.tv.cctv.com&amp;method=http" allowfullscreen="true" wmode="window" allowscriptaccess="always"></p>
<p>&nbsp;</p>
<p>背景资料<br />
赵 霖&nbsp; 教授&nbsp; 解放军总医院营养科<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 古人云食药一体，膳药同功，食物是最好的药物。看似简单的买菜做饭，其实蕴含着养生的奥妙。历代名医先贤对天然食物都有哪些精辟论述。食疗的功效到底是什么，健康教育首席专家赵霖教授激情论述凡膳皆药，寓医于食。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在古代吕氏春秋本味篇里边，有这么两句话，叫阳朴之姜，招摇之桂，姜指的什么，生姜，桂是指的什么啊，桂皮，我们在几千年以前，生姜和桂皮就已经是咱们的调料，而生姜和桂皮都是中药，咱们中药有一个治感冒的一个桂枝汤，太阳中风的桂枝汤，芍药甘草大枣姜，有芍药，有甘草，有大枣，有姜，当然它还有桂枝，所以就是说这些东西事药兼用的这么一个理念，贯穿了我们几千年的中国的饮食文化。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 古代说男子不可百日无姜，郑和下西洋，欧洲有一个旅游家，就记载说中国人在他们的船上种生姜，为什么呀，咱们的船员都是男的，男的要想保健，就不可百日无姜啊，你得经常吃姜。那么关于这个生姜你看我们繁体字生姜的姜是什么样的，是一个边疆的疆一半，加一个草字头，什么意思呢，疆御百邪，它能够防御各种各样的病，它就有这样的作用，所以咱们有好多好多的谚语，家备小姜，小病不慌，夏季常吃姜，益寿保安康，冬吃萝卜夏吃姜，不劳医生开药方，四季吃生姜，百病一扫光，早吃三片姜胜过人参汤，有很多古代的名谚，所以这些东西大家是不是都应该记住啊，这些咱们祖宗告诉咱们的，这东西也不上税，也不骗你，也没有专利保护，现在你想学外国人什么玩意儿，人家有专利，人家不告诉你，交钱才告诉你呢，这个用不着，所以这些东西都是非常真实的东西，大家都应该尊重它。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 生姜是一种常用的调味品，它能为菜肴体味，吃饭不香或饭量减少时，吃几片姜，或在菜里放上一点嫩姜就能改善食欲增加饭量，所以俗话说饭不香吃生姜，相传在楚汉相争时期，汉高祖刘邦征战河南鲁山，身染瘟疫，久治不愈，当地百姓献计，生姜萝卜汤，刘邦喝后病情大减，再一喝则药到病除，自古以来生姜被誉为药食两用之佳品，如果说男子保健应该多吃姜，那女子保健又该吃些什么呢。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 女的要保健你们知道要吃什么吗，女子不可百日无糖，指的是红糖，这个白糖大家记住没有任何营养，只有热量，我们叫纯热能食物，但是中国的红糖就特别有营养，我有一次出差，我在火车上，我对面有两个同志聊天，我说你们做什么工作的，他俩说我们都是甜蜜的事业，什么事业，是糖业公司的，后来我赶紧请教，我说咱们现在红糖都是哪出的，他说浙江义乌原来是个重点产地，现在基本上都在云贵一带，红糖就是中国的土糖，那么我们的红糖就有非常好的疗效，所以你看日本的市场都写着中国纯正红糖，而咱们现在呢，好多人都不知道要吃红糖了，现在咱们有个毛病，叫有宝不识宝，洋人说好他就说好。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 医生专家提醒人们，红糖具有独特的滋补保健功效，尤其是女人，更不可百日无红糖，性温的红糖通过温而补之，温而通之，温而散之，来发挥补血的作用。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 什么时候吃红糖啊，妇女要排恶露，你生完孩子之后都得吃红糖，来例假的时候要吃红糖吧，为什么，它有这样的功能啊，所以这些知识还得让我们年轻一代都掌握，要学会女子不可百日无糖，指的就是红糖。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 前年我在电视台做节目，有一个观众问我一个问题，说孩子要考试吃什么好，我说今儿您孩子高考去，你早上给他吃俩炸油饼，再吃一个炸糕，再吃俩煎鸡蛋，我说你到考完试他还没消化呢，那么孩子要考试了，早上应该怎么吃啊，我说很简单，第一个喝碗粥，或者喝杯豆浆，里面加蜂蜜，然后给吃一个蒸豆包，然后给他吃一煮鸡蛋，同时最后给他吃一小碗蒸枣，为什么呢，因为这个枣可以非常稳定的维持高血糖，咱们大脑要消耗人体30%的血糖，因为大脑唯一能用的原料就是葡萄糖，你把这碗枣吃了之后，小孩就能维持一个很好的血糖水平，他脑袋就灵，转的快，就容易考好，当然你这孩子得念书，不念书吃什么也不行。咱们关于红枣有很多这方面的记载，红枣最早是在哪儿种的，在河南的新郑县，你们到那儿去看，那些枣树全是大肚子，咱们有一个砑，每年要拿这个东西敲，所以这个枣树你看完了非常受感动，因为新郑是黄帝故里，是咱们黄帝出生的地方，所以当地枣树都上千年，我在欧洲访问，那一段想吃枣，我就跟我那欧洲学生说你给我买点枣去，他跑半天给我买回来几个什么啊，伊拉克蜜枣，就是椰枣，欧洲只知道吃椰枣，他没有中国这个枣。我们古代记载一日吃三枣，百岁不显老，门前一棵枣，红颜直到老，五谷加红枣，胜似灵芝草，要想皮肤好，粥利佳红枣。有好多好多这样的办法，你说枣那么多干嘛不吃啊，所以我现在有时候看小孩早上拿着这个派那个派，我心里面琢磨你给他把枣也比吃这玩意儿强啊，你吃点神造的行不行啊。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 据史料记载，枣在我国已有八千多年的历史，小小一棵红枣，保健功效很强，健脾益胃，补气养血，促进睡眠，缓和药性，因此在中医的方子里，常常能见到枣的踪影。红枣被誉为百果之王，同时物美价廉，百姓无需购买昂贵的补品，善用红枣即可达到养生保健功效。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在我们国家第一步农耕专著齐民要术里边，我们有五果，这第一个就是枣，所以大家一定要记住，就是要多吃枣。我还见过这么一个病例，前些年有一天我在门诊碰见一个山东来的老汉来看病，来了一检查发现是早期胃癌，我们就动员他住院，这老头说住不了院，因为我们家特别穷，他说我要住了院我就家破人亡，我回家人亡家不破，他说明儿我不行了，今儿我还能下地，这老头就走了，过了一年半，我一研究生在门诊给我打电话说赵主任你来一趟，我说什么事儿，他说人亡家不破那老头又来了，我就赶紧跑门诊就见这老大爷，一看满面红光，身体特好，后来他们就检查发现没事儿了，我就问他我说老大爷您治了吗，他说我哪有钱治啊，我说您吃药了吗，他说没吃药，我说那您吃什么了，他想半天他说我能吃什么呀，他说我们家别的不多，就有枣树，我们家十好几棵枣树，我天天吃枣，这老头吃了一年枣，把这癌给吃没了，后来我一查就发现这个枣里有一种环磷酸腺苷，这东西具有抗癌作用，能抑制癌细胞生长，当然这些例子都是个例，我不能说得了癌都去吃枣，但是我就知道，你至少这种饮食结构它就能够预防，能够把这个问题解决，所以给我一个很大的启发，别老迷信药，还得相信咱们这个食物，我就觉得咱们在中国，就得依托中国的条件，来解决中国人民自己的问题，咱们原来那个许嘉璐副委员长讲过一段话，他说中国人做不起美国人的梦，中国人也不想做日本人的梦，中国人只能做自己的梦，也就是说我们要立足自己的条件，用我们自己的方法来解决我们健康的问题。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 我国历史重视食养食疗，早在殷商时期就广泛用汤液药酒治疗疾病。商代初年有位叫伊尹的厨师写了部《汤液经》，专论饮食调和问题受到商王的赏识，还破格提拔他当了丞相，伊尹以厨师之身跃居当朝宰相，足可见他鞠躬志美，那么伊尹到底做了什么，博得商王如此的器重和赏识呢。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 伊尹这个人对我们中国有极大的贡献，为什么呢，大家知道我们做菜做饭的时候，我们这个菜肴都是好多种东西在一块做的，他就根据这样一个经验，他就发明了熬中药，就是把各种不同的草药搁在一块熬，所以古代说伊尹亚圣之才，撰用神农本草以为汤液。就是说他发明了熬这个中药的技术。远在三千年前的周朝，西周的时代，咱们国家就建立了世界上最早的医疗体系，那时候医生分成四类，第一类叫食医，第二类叫疾医，就是咱们的内科医生，第三类叫疡医，溃疡的疡，就是外科医生，最后一个是兽医，当时这四类医生怎么设的呢，食医什么工作呢，食医掌和王之六食，六饮、六膳、百馐、百酱、八珍之齐，就是他要负责周王的所有的食物，它的搭配，它的配制，它的烹饪，实际上是干嘛用的，保健，所以这是人类历史上最早的营养医学的实践；内科医生当时在周代干什么呢，疾医用五味、五谷、五药养其病，你看不光用药，还要用什么啊，用食物，那五味是什么啊，就是菜肴啊，调味品啊，所以就是说在当时的内科医生就要掌握使用这个食物来治病的这么一个技巧。那么外科医生疡医怎么做呢，外科医生是以酸养骨，以辛养津，就是吃酸的东西能够养骨，而辛辣的，像葱姜这些东西能够养津，以咸养脉，以苦养气，以甘养肉，以滑养窍。你要吃滑这种性质的食物你才能通便，叫养窍。所以当时实际上外科医生也在使用食物，所以周朝这个医疗体系我觉得是人类最早的一个医疗体系，是非常值得借鉴的。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 大家知道绿豆里边有一个特别好的蛋白，是一种解毒蛋白，所以古代管绿豆叫什么，叫济世之谷，它能够救人的这么一个东西，前两天我们那有一个老先生在那住院，我一个同事的父亲，浑身搔痒，老年搔痒，受不了，后来找我，我说有一个很简单的方，就是你把这绿豆拿水煮了，但是别煮开花，拿这个水来擦，也让病人同时喝，最后很快就收效。我在欧洲做访问学者的时候，有一个德国女博士，牙疼，疼的受不了，后来上大夫那去给她拔了俩牙还不行，后来她来找我，他说赵教授你们中国有什么办法吗，我说有一个简单的办法，因为你牙疼就是上焦火盛啊，我说弄点大蒜，在脚上涌泉穴那糊一下，那个大蒜很好找，德国都有，找着了一糊，这女的特娇气，刚糊了一个钟头就拔出一个泡来，我就赶紧拿下来不敢糊了，后来想糊什么呀，我就想绿豆，最后跟这些洋人一说谁也不知道绿豆，最后跑到慕尼黑，跑到中国店才买到这绿豆，买完之后把这绿豆拿水一泡，这绿豆泡发了以后，拿捣蒜的东西给它捣烂，然后就把生绿豆连汤一块搁在纱布上敷到涌泉穴上面，敷了一夜，第二天这人牙就不疼了，所以你说这绿豆就有这种很好的疗效。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在本草纲目里，有这样的记载，用绿豆煮食可消肿下气，清热解毒消暑解渴，调和五脏，安精神，补元气，滋润皮肤。绿豆粉可解诸毒，治疮肿，疗烫伤，绿豆皮能解热毒，退目翳，绿豆牙可解酒、解毒，因此李时珍称绿豆为真济世之良谷也，大自然提供给人类丰富的天然食物，其中有强身健体功效的还远不止这些。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 另外一个现在老年人好多人腰腿疼，人老哪儿先老啊，腿先老啊，为什么大家练太极拳啊，你得增加腿部力量，那么腰腿疼怎么治啊，一个很简单的方法，就是每天早晚各吃两个生栗子，怎么吃啊，生吃细嚼的，咱们当时古代的燕国，就是燕赵之地有木本粮食，木本粮食是什么啊，一个是枣，一个是栗子，所以板栗当时在古代对燕赵之地，咱们北方民众健康发挥了很重要的作用，早晚各吃两个栗子怎么吃，这个很有讲究，就是把栗子放在嘴里以后，仔细地嚼，把它嚼成浆，然后再咽下去，你就能够有效的解决腰腿疼，因为什么呢，因为能治肾虚，腰腿无力，它能够通肾益气厚胃肠，古代有很详细的记载。那么古代有一首诗，我给大家背一下，老去自添腰脚病，山翁服栗旧传方。说老了腰腿都有毛病了，上山碰见一个老头在山上给一个方，干嘛啊，吃栗子。客来为说晨兴晚，三咽徐收白玉浆，就是说你必须要把这个栗子嚼成浆，而且慢慢的咽下去，还得咽三回，这样一个诗，非常形象的描述了靠吃生栗子来解决腰腿疼的食疗的方法，所以这个不妨方法大家试试。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 唐代大医学家孙思邈称栗子为肾之果也，肾病宜食之。明代李时珍则别有一番见解，说栗子有驱寒、止泻之功效，栗子可谓全身是宝，就连其内果皮，外果皮，树叶、树皮及花皆可入药，但栗子生食难消化，熟食又易滞气，所以一次不宜多食。还有一种与板栗一并在炒锅店出现，人们休闲娱乐经常吃的食物，那就是花生。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 花生叫什么啊，叫长生果，这个花生我们这两年做了好多研究，我带着研究生就做花生蛋白的研究，就发现咱们国家有些地方解决了低温榨油的技术，因为一般花生得炒熟了榨油，咱们这个花生不炒，就生着炸，炸完之后剩的这个渣里头，这个花生蛋白就能治病，后来我们就查，就发现本草纲目里没花生，本草纲目什么时候写的，明朝，至少说明李时珍那时候咱们花生还没有广泛的种植，后来到了什么时候有呢，到了清炒的本草纲目拾遗这本书里，查着有花生了，叫什么名，叫落花生，我就发现一个花生季下来，跟着我们一块收花生这些孩子都又白又胖，为什么，他一边采一边吃，那么我们古代怎么记载呢，说花生性和平而味美香，生用清火润肺，炒用能健脾胃，大家炒的时候一定要注意，就是得少加点油，因为花生本身就有油，所以尽量吃蒸的或者轻轻的炒的这种，而且花生有什么优点，花生里边有一半都是油酸，这个油酸就是橄榄油里最主要的一个成分，那么油酸有什么特点呢，油酸它不去转化成脂肪，而是去转换成热量。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 花生所含的蛋白质和脂肪的含量比肉、蛋还高，所以有人把花生叫做素中之荤，或称植物肉。古人称花生为长生果，民谚道常吃花生能养生，由此可见花生的食疗功效非统一般，花生悦脾和胃，润肺化痰，滋养补气，清咽止痒，还有食用花生养胃醒脾，滑肠润燥之说，由优质花生压榨的食用油是百姓煎炸烹炒的首选，花生油有东方第一油之称。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 我自己有个经历，我在二十世纪六十年代，我当时得了肾炎，我那时候老尿蛋白，就解决不了，后来我住院的时候，我隔壁有一个小女孩，施今墨大夫来给她会诊，施大夫走了以后她就告诉我，她说施大夫给了我一个偏方，我说什么方啊，她说每天一两花生仁，就是带着红皮那个花生仁，搁在沙锅里搁上水，把它煮烂，然后每天早上起来空腹吃一两花生仁，吃多少天，吃40天，当时花生也很难买，我一个朋友在秦皇岛当兵，我就请他给我买，就认认真真地吃了40天，我这尿蛋白就没了，所以大家看，这比吃药不是灵多了吗，所以这些食疗在我们身上，我老觉得我要不是活在中国，可能我早就没事儿了，就是因为有祖宗传的这些个好方，咱才活到今天，还能给大伙讲点健康，要老吃那洋药没准早吃死了。现在好多年轻人，长时间在计算机前面工作，眼睛都不好，好多小孩戴眼镜，我也不例外，那么为什么眼睛会不好呢，这和我们眼睛保健当然有关系，但是跟食物也有关系，如果你食物不好不健康，你玉米黄素和叶黄素缺乏，这个眼底就得不到足够的补充，就非常容易出毛病，咱们国家平常吃的什么东西里边玉米黄素、叶黄素多啊，第一个就是玉米，为什么咱们老人愿意喝玉米粥，对眼睛好啊，还有一个是什么呢，就是枸杞子，枸杞子宁夏的最好，枸杞子里边含有非常高的玉米黄素，香港理工大学做实验，就让一组人每天吃十五克枸杞子，连吃28天，吃完一测就发现血里面的玉米黄素比没吃的时候高了2.5倍，你血里面玉米黄素一高，当然就补充到你的视网膜，这样就不容易闹老年黄斑症。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 中医很早就有枸杞养生的说法，认为常吃枸杞子，能坚筋骨耐寒暑，古人认为常食枸杞可以留住青春美色，与天地齐寿，因此枸杞花被称为长生花，枝条被称为仙人障，西王母障，自古至今在人们心目中枸杞子在强身健体益寿延年方面是十全十美的极品神品。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 现在咱们老年人前列腺的病特别多，怎么预防啊，吃西红柿啊，我一个同学跟我岁数差不多，60多了，晚上老得上厕所，他说怎么办，我说你每天吃西红柿，他就一天三顿都吃一小盘西红柿，吃了一年，现在夜里不上厕所了，为什么呢，因为西红柿里边含有非常高的番茄红素，尤其是咱们中国新疆出的西红柿，是世界上番茄红素含量最高的，因为新疆的日照时间特别长，那么哈佛大学他用了四年时间，跟踪了将近五万名成年男性，就发现大量的食用含有番茄红素的东西，就可以降低前列腺癌的发生率。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 按照中医五行学说，红色为火，为阳，故红色食物进入人体后可入心入血，大多具有益气补血和促进血液、淋巴液生成的作用，因此有红色食物养心一说。多吃红色食物可以令人精神抖索，增强自信及意志力，使人充满力量。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 菠菜为什么好呢，美国哈佛大学研究发现，常吃菠菜对视网膜的健康特别好，因为菠菜里含有非常高的叶酸，古代记载凡老人久病大便涩滞即不通者，吃什么，菠菜，菠菜通便功能特别强，菠菜是滑的，所以我们古代说菠菜性凉味甘，入肺肠胃经，入三个经，有养血、止血、润燥、通利肠胃、健脾和中，解酒毒、热毒之功效，所以为什么咱们喝酒的时候要来一个凉菜，菠菜拌花生仁，就是这个菠菜有解酒的功能。还有一个就是蜂蜜，我给大家介绍一下，这里还有一个蜂胶的知识，现在说蜂胶、蜂蜜、花粉，蜂蜜这个东西我们古代记载是清热、补中、解毒、润燥、止痛，我建议老年人要多吃蜂蜜，这个蜂蜜不光有保健功能，我觉得吃蜂蜜还是一个爱国的行动，为什么呢，大家说这蜜蜂是干什么的，传粉的，你多吃蜂蜜是咱们国家蜜蜂就多了，蜜蜂一多传粉就多了，这农业不就带动了吗，所以号召大家要多吃蜂蜜。后来我就想到一个道理，你说神农尝百草，是先科学了才吃，还是吃了才科学呢，你是先明白了才吃，还是吃完了才明白呢，他肯定得先吃，他琢磨琢磨然后才去想科学，什么叫科学，科学叫分科之学，所以我就觉得现在老说咱们中医不科学，我觉得中医比谁都科学，为什么，因为我们五千年活下来了，活得很健康，就是很科学的，能治病就是科学。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 我们的祖先何其聪明与智慧，他们得出食药一体，食药同功的精辟论述，并留下了很多食疗方，其中大部分沿袭至今仍被广泛运用。这些食疗理念与成方对指导我们现代人养生保健意义深远。经典食疗方：生姜当归羊肉汤。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 我国古代医学文献里第一个食疗方，大家知道是什么吗，就是生姜当归羊肉汤，你冬天买块羊肉，然后到药店买两片当归足够，然后搁一些姜在一块炖，喝这个汤在冬天保健就特别好，我那年到苏州去出差，11月份，苏州满街都是卖羊肉汤的，我就觉得苏州地区的居民非常懂得保健，所以我觉得在中华民族的先贤，在对现代科学毫无了解的古代，就能够认识到羊肉效同人参，就能做出中国的第一个食疗方生姜当归羊肉汤，这种深刻的洞察力，真是令我们钦佩不已，所以我觉得作为中国人非常自豪。大家知道羊肉大热，我那年就上当，我那年夏天出差到东北一个城市，最后一个朋友非要请我吃涮羊肉，我当时想这么热，能吃涮羊肉吗，他说没事儿，开着空调呢，我就跟他去饭店吃涮羊肉，吃完之后我就遭殃了，我这腋窝底下原来有一个脂肪瘤，就吃完这个涮羊肉，这脂肪瘤就发起来了，最后开刀、流脓折腾半天才好，后来我就明白一道理，夏天就得喝绿豆汤，冬天才能吃涮羊肉呢。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 食疗保健大家一定要记住，食疗保健有一个特别特别重要的原则，两句话，润物细无声，王道无近功。就是说食疗给你的帮助是跟下这个小雨一样，慢慢的帮助你的，你别琢磨着吃一天就好，他是王道他不是霸道，所以他不会有近功，就是说你一定要坚持，所以食疗本身就要把它渗透到我们日常生活的每一个角落，你要渗透食疗的道理，那么食疗的功效是什么呢，我们古代记载叫无功可言，无德可鉴，他没多少功，但是结论是什么呢，而人登寿域，你看不到它的功效但是你活的长寿，这就是我们古代，我们历代的先贤，对我们食疗的一个非常好的总结和概括。所以最后我想用一个诺贝尔奖获奖人他们开会的宣言的一句话来结尾，这个宣言里面有这么一句话，这句话怎么说的，他说如果人类要在二十一世纪继续生存下去，那么就必须回首两千五百年前去吸取中国孔子的智慧。我觉得这句话充分认识到了，我们当前中国提出的要建设和谐社会的这种主张，我就希望我们的观众朋友们，我们一定要在我们的生活中认真的贯彻食疗养生保健的理念，如果你贯彻了你能做到什么，就是个人少受罪，儿女少受累，节省医药费，造福全社会。<br />
</p>
<img src ="http://www.cnblogs.com/haowenbiao/aggbug/1261774.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/41821/" target="_blank">[新闻]死敌VMware变身微软认证计划新成员</a>]]></description></item><item><title>ReportView（RDSL）参考资料</title><link>http://www.cnblogs.com/haowenbiao/archive/2008/07/23/1249438.html</link><dc:creator>恭喜发财</dc:creator><author>恭喜发财</author><pubDate>Wed, 23 Jul 2008 04:34:00 GMT</pubDate><guid>http://www.cnblogs.com/haowenbiao/archive/2008/07/23/1249438.html</guid><wfw:comment>http://www.cnblogs.com/haowenbiao/comments/1249438.html</wfw:comment><comments>http://www.cnblogs.com/haowenbiao/archive/2008/07/23/1249438.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/haowenbiao/comments/commentRss/1249438.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/haowenbiao/services/trackbacks/1249438.html</trackback:ping><description><![CDATA[<p>微软官方reportview参考：<a href="http://msdn.microsoft.com/zh-cn/library/ms251671.aspx">http://msdn.microsoft.com/zh-cn/library/ms251671.aspx</a></p>
<p>&nbsp;</p>
<p><a href="http://www.gotreportviewer.com/">http://www.gotreportviewer.com/</a>&nbsp;需用代理</p>
<img src ="http://www.cnblogs.com/haowenbiao/aggbug/1249438.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/41819/" target="_blank">[新闻]英特尔雅虎开发网络计算机频道</a>]]></description></item><item><title>How To Print Using Custom Page Sizes on Windows NT and Windows 2000(VB6)</title><link>http://www.cnblogs.com/haowenbiao/archive/2008/07/16/1244560.html</link><dc:creator>恭喜发财</dc:creator><author>恭喜发财</author><pubDate>Wed, 16 Jul 2008 09:33:00 GMT</pubDate><guid>http://www.cnblogs.com/haowenbiao/archive/2008/07/16/1244560.html</guid><wfw:comment>http://www.cnblogs.com/haowenbiao/comments/1244560.html</wfw:comment><comments>http://www.cnblogs.com/haowenbiao/archive/2008/07/16/1244560.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/haowenbiao/comments/commentRss/1244560.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/haowenbiao/services/trackbacks/1244560.html</trackback:ping><description><![CDATA[<p>这是微软的官方关于自定义打印的处理，VB6的实现方式。<br />
<font face="Verdana">http://support.microsoft.com/default.aspx?scid=kb;en-us;282474</font></p>
<img src ="http://www.cnblogs.com/haowenbiao/aggbug/1244560.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/41818/" target="_blank">[新闻]Windows Live视频邮件9月9日开始测试</a>]]></description></item><item><title>汇总c#.net常用函数和方法集</title><link>http://www.cnblogs.com/haowenbiao/archive/2008/07/05/1236290.html</link><dc:creator>恭喜发财</dc:creator><author>恭喜发财</author><pubDate>Sat, 05 Jul 2008 07:27:00 GMT</pubDate><guid>http://www.cnblogs.com/haowenbiao/archive/2008/07/05/1236290.html</guid><wfw:comment>http://www.cnblogs.com/haowenbiao/comments/1236290.html</wfw:comment><comments>http://www.cnblogs.com/haowenbiao/archive/2008/07/05/1236290.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/haowenbiao/comments/commentRss/1236290.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/haowenbiao/services/trackbacks/1236290.html</trackback:ping><description><![CDATA[<div class="postTitle"><strong>1、DateTime 数字型 </strong><br />
System.DateTime currentTime=new System.DateTime(); <br />
1.1 取当前年月日时分秒 <br />
currentTime=System.DateTime.Now; <br />
1.2 取当前年 <br />
int 年=currentTime.Year; <br />
1.3 取当前月 <br />
int 月=currentTime.Month; <br />
1.4 取当前日 <br />
int 日=currentTime.Day; <br />
1.5 取当前时 <br />
int 时=currentTime.Hour; <br />
1.6 取当前分 <br />
int 分=currentTime.Minute; <br />
1.7 取当前秒 <br />
int 秒=currentTime.Second; <br />
1.8 取当前毫秒 <br />
int 毫秒=currentTime.Millisecond; <br />
（变量可用中文） </div>
<div class="postText">
<p><strong>2、Int32.Parse(变量) Int32.Parse("常量") </strong><br />
字符型转换 转为32位数字型 </p>
<p><strong>3、 变量.ToString() <br />
</strong>字符型转换 转为字符串 <br />
12345.ToString("n"); //生成 12,345.00 <br />
12345.ToString("C"); //生成 ￥12,345.00 <br />
12345.ToString("e"); //生成 1.234500e+004 <br />
12345.ToString("f4"); //生成 12345.0000 <br />
12345.ToString("x"); //生成 3039 (16进制) <br />
12345.ToString("p"); //生成 1,234,500.00% </p>
<p><br />
<strong>4、变量.Length 数字型 </strong><br />
取字串长度： <br />
如： string str="中国"; <br />
int Len = str.Length ; //Len是自定义变量， str是求测的字串的变量名 </p>
<p><strong>5、System.Text.Encoding.Default.GetBytes(变量) <br />
</strong>字码转换 转为比特码 <br />
如：byte[] bytStr = System.Text.Encoding.Default.GetBytes(str); <br />
然后可得到比特长度： <br />
len = bytStr.Length; </p>
<p><strong>6、System.Text.StringBuilder("") </strong><br />
字符串相加，（+号是不是也一样？） <br />
如：System.Text.StringBuilder sb = new System.Text.StringBuilder(""); <br />
sb.Append("中华"); <br />
sb.Append("人民"); <br />
sb.Append("共和国"); </p>
<p><strong>7、变量.Substring(参数1,参数2); <br />
</strong>截取字串的一部分，参数1为左起始位数，参数2为截取几位。 <br />
如：string s1 = str.Substring(0,2); </p>
<p><strong>8、String user_IP=Request.ServerVariables["REMOTE_ADDR"].ToString(); </strong><br />
取远程用户IP地址 </p>
<p><strong>9、穿过代理服务器取远程用户真实IP地址： <br />
</strong>if(Request.ServerVariables["HTTP_VIA"]!=null){ <br />
string user_IP=Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); <br />
}else{ <br />
string user_IP=Request.ServerVariables["REMOTE_ADDR"].ToString(); <br />
} <br />
<br />
<strong>10、 Session["变量"]; </strong><br />
存取Session值； <br />
如，赋值： Session["username"]="小布什"; </p>
<p>取值： Object objName=Session["username"]; <br />
String strName=objName.ToString(); <br />
清空： Session.RemoveAll(); </p>
<p><strong>11、String str=Request.QueryString["变量"]; </strong><br />
用超链接传送变量。 <br />
如在任一页中建超链接:&lt;a href=Edit.aspx?fbid=23&gt;点击&lt;/a&gt; <br />
在Edit.aspx页中取值：String str=Request.QueryString["fdid"]; </p>
<p><strong>12、DOC对象.CreateElement("新建节点名"); </strong><br />
创建XML文档新节点 </p>
<p><strong>13、父节点.AppendChild(子节点)； </strong><br />
将新建的子节点加到XML文档父节点下 </p>
<p><strong>14、 父节点.RemoveChild(节点); </strong><br />
删除节点 </p>
<p><strong>15、Response </strong><br />
Response.Write("字串")； <br />
Response.Write(变量)； <br />
向页面输出。 </p>
<p>Response.Redirect("URL地址"）； <br />
跳转到URL指定的页面 </p>
<p><strong>16、char.IsWhiteSpce(字串变量，位数)——逻辑型 </strong><br />
查指定位置是否空字符； <br />
如： <br />
string str="中国 人民"; <br />
Response.Write(char.IsWhiteSpace(str,2)); //结果为：True, 第一个字符是0位，2是第三个字符。 </p>
<p><strong>17、char.IsPunctuation('字符') --逻辑型 </strong><br />
查字符是否是标点符号 <br />
如：Response.Write(char.IsPunctuation('A')); //返回：False </p>
<p><strong>18、(int)'字符' <br />
</strong>把字符转为数字，查代码点，注意是单引号。 <br />
如： <br />
Response.Write((int)'中'); //结果为中字的代码：20013 </p>
<p><strong>19、(char)代码 <br />
</strong>把数字转为字符，查代码代表的字符。 <br />
如： <br />
Response.Write((char)22269); //返回&#8220;国&#8221;字。 <br />
<br />
<strong>20、 Trim() <br />
</strong>清除字串前后空格 </p>
<p><strong>21 、字串变量.Replace("子字串","替换为") </strong><br />
字串替换 <br />
如： <br />
string str="中国"; <br />
str=str.Replace("国","央"); //将国字换为央字 <br />
Response.Write(str); //输出结果为&#8220;中央&#8221; </p>
<p>再如：（这个非常实用） </p>
<p>string str="这是&lt;script&gt;脚本"; <br />
str=str.Replace("&lt;","&lt;font&gt;&lt;&lt;/font&gt;"); //将左尖括号替换为&lt;font&gt; 与 &lt; 与 &lt;/font&gt; （或换为&lt;，但估计经XML存诸后，再提出仍会还原） <br />
Response.Write(str); //显示为：&#8220;这是&lt;script&gt;脚本&#8221; </p>
<p>如果不替换，&lt;script&gt;将不显示，如果是一段脚本，将运行；而替换后，脚本将不运行。 <br />
这段代码的价值在于：你可以让一个文本中的所有HTML标签失效，全部显示出来，保护你的具有交互性的站点。 <br />
具体实现：将你的表单提交按钮脚本加上下面代码： <br />
string strSubmit=label1.Text; //label1是你让用户提交数据的控件ID。 <br />
strSubmit=strSubmit.Replace("&lt;","&lt;font&gt;&lt;&lt;/font&gt;"); <br />
然后保存或输出strSubmit。 <br />
用此方法还可以简单实现UBB代码。 </p>
<p><strong>22、Math.Max(i,j) </strong><br />
取i与j中的最大值 <br />
如 int x=Math.Max(5,10); // x将取值 10 </p>
<p>加一点吧 23、字串对比...... <br />
加一点吧 </p>
<p><strong>23、字串对比一般都用: if(str1==str2){ } , 但还有别的方法: </strong></p>
<p>(1)、 <br />
string str1; str2 <br />
//语法: str1.EndsWith(str2); __检测字串str1是否以字串str2结尾,返回布尔值.如: <br />
if(str1.EndsWith(str2)){ Response.Write("字串str1是以"+str2+"结束的"); } </p>
<p>(2)、 <br />
//语法:str1.Equals(str2); __检测字串str1是否与字串str2相等,返回布尔值,用法同上. </p>
<p>(3)、 <br />
//语法 Equals(str1,str2); __检测字串str1是否与字串str2相等,返回布尔值,用法同上. </p>
<p><strong>24、IndexOf() 、LastIndexOf() </strong><br />
查找字串中指定字符或字串首次（最后一次）出现的位置,返回索引值，如： <br />
str1.IndexOf("字")； //查找&#8220;字&#8221;在str1中的索引值（位置） <br />
str1.IndexOf("字串")；//查找&#8220;字串&#8221;的第一个字符在str1中的索引值（位置） <br />
str1.IndexOf("字串",3,2)；//从str1第4个字符起，查找2个字符，查找&#8220;字串&#8221;的第一个字符在str1中的索引值（位置） </p>
<p><strong>25、Insert() </strong><br />
在字串中指定索引位插入指定字符。如： <br />
str1.Insert(1,"字");在str1的第二个字符处插入&#8220;字&#8221;，如果str1="中国"，插入后为&#8220;中字国&#8221;； </p>
<p><strong>26、PadLeft()、PadRight() </strong><br />
在字串左（或右）加空格或指定char字符，使字串达到指定长度，如： <br />
&lt;% <br />
string str1="中国人"; <br />
str1=str1.PadLeft(10,'1'); //无第二参数为加空格 <br />
Response.Write(str1); //结果为&#8220;1111111中国人&#8221; ， 字串长为10 <br />
%&gt; </p>
<p><strong>27、Remove() </strong><br />
从指定位置开始删除指定数的字符 <br />
字串对比一般都用: if(str1==str2){ } , 但还有别的方法: </p>
<p>1、 <br />
string str1; str2 <br />
//语法: str1.EndsWith(str2); __检测字串str1是否以字串str2结尾,返回布尔值.如: <br />
if(str1.EndsWith(str2)){ Response.Write("字串str1是以"+str2+"结束的"); } </p>
<p>2、 <br />
//语法:str1.Equals(str2); __检测字串str1是否与字串str2相等,返回布尔值,用法同上. </p>
<p>3、 <br />
//语法 Equals(str1,str2); __检测字串str1是否与字串str2相等,返回布尔值,用法同上. </p>
<p>IndexOf() <br />
查找字串中指定字符或字串首次出现的位置,返首索引值，如： <br />
str1.IndexOf("字")； //查找&#8220;字&#8221;在str1中的索引值（位置） <br />
str1.IndexOf("字串")；//查找&#8220;字串&#8221;的第一个字符在str1中的索引值（位置） <br />
str1.IndexOf("字串",3,2)；//从str1第4个字符起，查找2个字符，查找&#8220;字串&#8221;的第一个字符在str1中的索引值（位置） </p>
<p>1.9 取中文日期显示——年月日时分 <br />
string strY=currentTime.ToString("f"); //不显示秒 </p>
<p>1.10 取中文日期显示_年月 <br />
string strYM=currentTime.ToString("y"); </p>
<p>1.11 取中文日期显示_月日 <br />
string strMD=currentTime.ToString("m"); </p>
<p>1.12 取当前年月日，格式为：2003-9-23 <br />
string strYMD=currentTime.ToString("d"); </p>
<p>1.13 取当前时分，格式为：14：24 <br />
string strT=currentTime.ToString("t"); <br />
更新一下， 上面不能编辑： </p>
<p><strong><u>c#.net函数和方法集(大家一起来加啊) </u></strong></p>
<p><br />
<strong>1、DateTime 数字型 </strong><br />
System.DateTime currentTime=new System.DateTime(); <br />
1.1 取当前年月日时分秒 <br />
currentTime=System.DateTime.Now; <br />
1.2 取当前年 <br />
int 年=currentTime.Year; <br />
1.3 取当前月 <br />
int 月=currentTime.Month; <br />
1.4 取当前日 <br />
int 日=currentTime.Day; <br />
1.5 取当前时 <br />
int 时=currentTime.Hour; <br />
1.6 取当前分 <br />
int 分=currentTime.Minute; <br />
1.7 取当前秒 <br />
int 秒=currentTime.Second; <br />
1.8 取当前毫秒 <br />
int 毫秒=currentTime.Millisecond; <br />
（变量可用中文） </p>
<p>1.9 取中文日期显示——年月日时分 <br />
string strY=currentTime.ToString("f"); //不显示秒 </p>
<p>1.10 取中文日期显示_年月 <br />
string strYM=currentTime.ToString("y"); </p>
<p>1.11 取中文日期显示_月日 <br />
string strMD=currentTime.ToString("m"); </p>
<p>1.12 取中文年月日 <br />
string strYMD=currentTime.ToString("D"); </p>
<p>1.13 取当前时分，格式为：14：24 <br />
string strT=currentTime.ToString("t"); </p>
<p>1.14 取当前时间，格式为：2003-09-23T14:46:48 <br />
string strT=currentTime.ToString("s"); </p>
<p>1.15 取当前时间，格式为：2003-09-23 14:48:30Z <br />
string strT=currentTime.ToString("u"); </p>
<p>1.16 取当前时间，格式为：2003-09-23 14:48 <br />
string strT=currentTime.ToString("g"); <br />
<br />
1.17 取当前时间，格式为：Tue, 23 Sep 2003 14:52:40 GMT <br />
string strT=currentTime.ToString("r"); </p>
<p>1.18获得当前时间 n 天后的日期时间 <br />
DateTime newDay = DateTime.Now.AddDays(100); </p>
<p><strong>2、Int32.Parse(变量) Int32.Parse("常量") <br />
</strong>字符型转换 转为32位数字型 </p>
<p><strong>3、 变量.ToString() </strong><br />
字符型转换 转为字符串 <br />
12345.ToString("n"); //生成 12,345.00 <br />
12345.ToString("C"); //生成 ￥12,345.00 <br />
12345.ToString("e"); //生成 1.234500e+004 <br />
12345.ToString("f4"); //生成 12345.0000 <br />
12345.ToString("x"); //生成 3039 (16进制) <br />
12345.ToString("p"); //生成 1,234,500.00% </p>
<p><br />
<strong>4、变量.Length 数字型 </strong><br />
取字串长度： <br />
如： string str="中国"; <br />
int Len = str.Length ; //Len是自定义变量， str是求测的字串的变量名 </p>
<p><strong>5、System.Text.Encoding.Default.GetBytes(变量) </strong><br />
字码转换 转为比特码 <br />
如：byte[] bytStr = System.Text.Encoding.Default.GetBytes(str); <br />
然后可得到比特长度： <br />
len = bytStr.Length; </p>
<p><strong>6、System.Text.StringBuilder("") </strong><br />
字符串相加，（+号是不是也一样？） <br />
如：System.Text.StringBuilder sb = new System.Text.StringBuilder(""); <br />
sb.Append("中华"); <br />
sb.Append("人民"); <br />
sb.Append("共和国"); </p>
<p><strong>7、变量.Substring(参数1,参数2); </strong><br />
截取字串的一部分，参数1为左起始位数，参数2为截取几位。 <br />
如：string s1 = str.Substring(0,2); </p>
<p><strong>8、String user_IP=Request.ServerVariables["REMOTE_ADDR"].ToString(); </strong><br />
取<strong>远程用户IP地址 </strong></p>
<p><strong>9、穿过代理服务器取远程用户真实IP地址： <br />
</strong>if(Request.ServerVariables["HTTP_VIA"]!=null){ <br />
string user_IP=Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); <br />
}else{ <br />
string user_IP=Request.ServerVariables["REMOTE_ADDR"].ToString(); <br />
} <br />
<br />
<strong>10、 Session["变量"]; </strong><br />
存取Session值； <br />
如，赋值： Session["username"]="小布什"; </p>
<p>取值： Object objName=Session["username"]; <br />
String strName=objName.ToString(); <br />
清空： Session.RemoveAll(); </p>
<p><strong>11、String str=Request.QueryString["变量"]; <br />
</strong>用超链接传送变量。 <br />
如在任一页中建超链接:&lt;a href=Edit.aspx?fbid=23&gt;点击&lt;/a&gt; <br />
在Edit.aspx页中取值：String str=Request.QueryString["fdid"]; </p>
<p><strong>12、DOC对象.CreateElement("新建节点名"); </strong><br />
创建XML文档新节点 </p>
<p><strong>13、父节点.AppendChild(子节点)； <br />
</strong>将新建的子节点加到XML文档父节点下 </p>
<p><strong>14、 父节点.RemoveChild(节点); </strong><br />
删除节点 </p>
<p><strong>15、Response </strong><br />
Response.Write("字串")； <br />
Response.Write(变量)； <br />
向页面输出。 </p>
<p>Response.Redirect("URL地址"）； <br />
跳转到URL指定的页面 </p>
<p><strong>16、char.IsWhiteSpce(字串变量，位数)——逻辑型 </strong><br />
查指定位置是否空字符； <br />
如： <br />
string str="中国 人民"; <br />
Response.Write(char.IsWhiteSpace(str,2)); //结果为：True, 第一个字符是0位，2是第三个字符。 </p>
<p><strong>17、char.IsPunctuation('字符') --逻辑型 </strong><br />
查字符是否是标点符号 <br />
如：Response.Write(char.IsPunctuation('A')); //返回：False </p>
<p><strong>18、(int)'字符' </strong><br />
把字符转为数字，查代码点，注意是单引号。 <br />
如： <br />
Response.Write((int)'中'); //结果为中字的代码：20013 </p>
<p><strong>19、(char)代码 <br />
</strong>把数字转为字符，查代码代表的字符。 <br />
如： <br />
Response.Write((char)22269); //返回&#8220;国&#8221;字。 <br />
<br />
<strong>20、 Trim() <br />
</strong>清除字串前后空格 </p>
<p><strong>21 、字串变量.Replace("子字串","替换为") </strong><br />
字串替换 <br />
如： <br />
string str="中国"; <br />
str=str.Replace("国","央"); //将国字换为央字 <br />
Response.Write(str); //输出结果为&#8220;中央&#8221; </p>
<p>再如：（这个非常实用） </p>
<p>string str="这是&lt;script&gt;脚本"; <br />
str=str.Replace("&lt;","&lt;font&gt;&lt;&lt;/font&gt;"); //将左尖括号替换为&lt;font&gt; 与 &lt; 与 &lt;/font&gt; （或换为&lt;，但估计经XML存诸后，再提出仍会还原） <br />
Response.Write(str); //显示为：&#8220;这是&lt;script&gt;脚本&#8221; </p>
<p>如果不替换，&lt;script&gt;将不显示，如果是一段脚本，将运行；而替换后，脚本将不运行。 <br />
这段代码的价值在于：你可以让一个文本中的所有HTML标签失效，全部显示出来，保护你的具有交互性的站点。 <br />
具体实现：将你的表单提交按钮脚本加上下面代码： <br />
string strSubmit=label1.Text; //label1是你让用户提交数据的控件ID。 <br />
strSubmit=strSubmit.Replace("&lt;","&lt;font&gt;&lt;&lt;/font&gt;"); <br />
然后保存或输出strSubmit。 <br />
用此方法还可以简单实现UBB代码。 </p>
<p><strong>22、Math.Max(i,j) <br />
</strong>取i与j中的最大值 <br />
如 int x=Math.Max(5,10); // x将取值 10 </p>
<p><strong>23、字串对比一般都用: if(str1==str2){ } , 但还有别的方法: </strong></p>
<p>(1)、 <br />
string str1; str2 <br />
//语法: str1.EndsWith(str2); __检测字串str1是否以字串str2结尾,返回布尔值.如: <br />
if(str1.EndsWith(str2)){ Response.Write("字串str1是以"+str2+"结束的"); } </p>
<p>(2)、 <br />
//语法:str1.Equals(str2); __检测字串str1是否与字串str2相等,返回布尔值,用法同上. </p>
<p>(3)、 <br />
//语法 Equals(str1,str2); __检测字串str1是否与字串str2相等,返回布尔值,用法同上. </p>
<p><strong>24、IndexOf() 、LastIndexOf() </strong><br />
查找字串中指定字符或字串首次（最后一次）出现的位置,返回索引值，如： <br />
str1.IndexOf("字")； //查找&#8220;字&#8221;在str1中的索引值（位置） <br />
str1.IndexOf("字串")；//查找&#8220;字串&#8221;的第一个字符在str1中的索引值（位置） <br />
str1.IndexOf("字串",3,2)；//从str1第4个字符起，查找2个字符，查找&#8220;字串&#8221;的第一个字符在str1中的索引值（位置） </p>
<p><strong>25、Insert() </strong><br />
在字串中指定索引位插入指定字符。如： <br />
str1.Insert(1,"字");在str1的第二个字符处插入&#8220;字&#8221;，如果str1="中国"，插入后为&#8220;中字国&#8221;； </p>
<p><strong>26、PadLeft()、PadRight() <br />
</strong>在字串左（或右）加空格或指定char字符，使字串达到指定长度，如： <br />
&lt;% <br />
string str1="中国人"; <br />
str1=str1.PadLeft(10,'1'); //无第二参数为加空格 <br />
Response.Write(str1); //结果为&#8220;1111111中国人&#8221; ， 字串长为10 <br />
%&gt; </p>
<p><strong>27、Remove() </strong><br />
从指定位置开始删除指定数的字符 <br />
&lt;% <br />
string str1="我是萨达姆的崇拜者之一"; <br />
Response.Write(str1.Remove(5,4)); //结果为&#8220;我是萨达姆之一&#8221; <br />
%&gt;</p>
</div>
<img src ="http://www.cnblogs.com/haowenbiao/aggbug/1236290.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/41817/" target="_blank">[新闻]Intel首次公开展示Nehalem架构迅驰3平台</a>]]></description></item><item><title>在多线程中如何调用Winform</title><link>http://www.cnblogs.com/haowenbiao/archive/2008/06/29/1232051.html</link><dc:creator>恭喜发财</dc:creator><author>恭喜发财</author><pubDate>Sun, 29 Jun 2008 09:19:00 GMT</pubDate><guid>http://www.cnblogs.com/haowenbiao/archive/2008/06/29/1232051.html</guid><wfw:comment>http://www.cnblogs.com/haowenbiao/comments/1232051.html</wfw:comment><comments>http://www.cnblogs.com/haowenbiao/archive/2008/06/29/1232051.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/haowenbiao/comments/commentRss/1232051.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/haowenbiao/services/trackbacks/1232051.html</trackback:ping><description><![CDATA[<p><font face="Verdana">在多线程中如何调用Winform</font></p>
<p><font face="Verdana">我的WinForm程序中有一个用于更新主窗口的工作线程（worker thread），但文档中却提示我不能在多线程中调用这个form（为什么？），而事实上我在调用时程序常常会崩掉。请问如何从多线程中调用form中的方法呢？ </font></p>
<p><font face="Verdana">　　解答：</font></p>
<p><font face="Verdana">　　每一个从Control类中派生出来的WinForm类（包括Control类）都是依靠底层Windows消息和一个消息泵循环（message pump loop）来执行的。消息循环都必须有一个相对应的线程，因为发送到一个window的消息实际上只会被发送到创建该window的线程中去。其结果是，即使提供了同步（synchronization），你也无法从多线程中调用这些处理消息的方法。大多数plumbing是掩藏起来的，因为WinForm是用代理（delegate）将消息绑定到事件处理方法中的。WinForm将Windows消息转换为一个基于代理的事件，但你还是必须注意，由于最初消息循环的缘故，只有创建该form的线程才能调用其事件处理方法。如果你在你自己的线程中调用这些方法，则它们会在该线程中处理事件，而不是在指定的线程中进行处理。你可以从任何线程中调用任何不属于消息处理的方法。</font></p>
<p><font face="Verdana">　　Control类（及其派生类）实现了一个定义在System.ComponentModel命名空间下的接口 -- ISynchronizeInvoke，并以此来处理多线程中调用消息处理方法的问题：</font></p>
<p><font face="Verdana">public interface ISynchronizeInvoke<br />
{<br />
　object Invoke(Delegate　method,object[] args);<br />
　IAsyncResult BeginInvoke(Delegate　method,object[] args);<br />
　object EndInvoke(IAsyncResult result);<br />
　bool InvokeRequired {get;}<br />
} </font></p>
<p><font face="Verdana">　　ISynchronizeInvoke提供了一个普通的标准机制用于在其他线程的对象中进行方法调用。例如，如果一个对象实现了ISynchronizeInvoke，那么在线程T1上的客户端可以在该对象中调用ISynchronizeInvoke的Invoke()方法。Invoke()方法的实现会阻塞（block）该线程的调用，它将调用打包发送（marshal）到 T2，并在T2中执行调用，再将返回值发送会T1，然后返回到T1的客户端。Invoke()方法以一个代理来定位该方法在T2中的调用，并以一个普通的对象数组做为其参数。</font></p>
<p><font face="Verdana">　　调用者还可以检查InvokeRequired属性，因为你既可以在同一线程中调用ISynchronizeInvoke也可以将它重新定位（redirect）到其他线程中去。如果InvokeRequired的返回值是false的话，则调用者可以直接调用该对象的方法。</font></p>
<p><font face="Verdana">　　比如，假设你想要从另一个线程中调用某个form中的Close方法，那么你可以使用预先定义好的的MethodInvoker代理，并调用Invoke方法:</font></p>
<p><font face="Verdana">Form form;<br />
/* obtain a reference to the form, <br />
then: */<br />
ISynchronizeInvoke synchronizer;<br />
synchronizer = form;</font></p>
<p><font face="Verdana">if(synchronizer.InvokeRequired)<br />
{<br />
MethodInvoker invoker = new <br />
MethodInvoker(form.Close);<br />
synchronizer.Invoke(invoker,null);<br />
}<br />
else<br />
form.Close(); </font></p>
<p><font face="Verdana">　　ISynchronizeInvoke不仅仅用于WinForm中。例如，一个Calculator类提供了将两个数字相加的Add()方法，它就是通过ISynchronizeInvoke来实现的。用户必须确定ISynchronizeInvoke.Invoke()方法的调用是执行在正确的线程中的。</font></p>
<p><font face="Verdana">　　C# 在正确的线程中写入调用</font></p>
<p><font face="Verdana">　　列表A. Calculator类的Add()方法用于将两个数字相加。如果用户直接调用Add()方法，它会在该用户的线程中执行调用，而用户可以通过ISynchronizeInvoke.Invoke()将调用写入正确的线程中。 <br />
　　列表A:</font></p>
<font face="Verdana">
<p><br />
public class Calculator : ISynchronizeInvoke<br />
{<br />
　public int Add(int arg1,int arg2)<br />
　{　<br />
　　int threadID = Thread.CurrentThread.GetHashCode();<br />
　　Trace.WriteLine( "Calculator thread ID is " + threadID.ToString());<br />
　　return arg1 + arg2;<br />
　}<br />
　//ISynchronizeInvoke implementation <br />
　public object Invoke(Delegate method,object[] args)<br />
　{<br />
　　public IAsyncResult BeginInvoke(Delegate method,object[] args)<br />
　　{<br />
　　　public object EndInvoke(IAsyncResult result)<br />
　　　{<br />
　　　　public bool InvokeRequired<br />
　　　　{<br />
　　　　}<br />
　　　}<br />
　　　//Client-side code<br />
　　　public delegate int AddDelegate(int arg1,int arg2);</p>
<p>　　　　int threadID = Thread.CurrentThread.GetHashCode();<br />
　　　　Trace.WriteLine("Client thread ID is " + threadID.ToString());</p>
<p>　　　　Calculator calc;<br />
　　　　/* Some code to initialize calc */</p>
<p>　　　　AddDelegate addDelegate = new AddDelegate(calc.Add);</p>
<p>　　　　object[] arr = new object[2];<br />
　　　　arr[0] = 3;<br />
　　　　arr[1] = 4;</p>
<p>　　　　int sum = 0;<br />
　　　　sum = (int) calc.Invoke(addDelegate,arr);<br />
　　　　Debug.Assert(sum ==7);</p>
<p>　　　　/* Possible output:<br />
　　　　Calculator thread ID is 29<br />
　　　　Client thread ID is 30 <br />
　　　　*/<br />
&nbsp;</p>
<p>　　或许你并不想进行同步调用，因为它被打包发送到另一个线程中去了。你可以通过BeginInvoke()和EndInvoke()方法来实现它。你可以依照通用的.NET非同步编程模式（asynchronous programming model）来使用这些方法：用BeginInvoke()来发送调用，用EndInvoke()来实现等待或用于在完成时进行提示以及收集返回结果。</p>
<p>　　还值得一提的是ISynchronizeInvoke方法并非安全类型。 类型不符会导致在执行时被抛出异常，而不是编译错误。所以在使用ISynchronizeInvoke时要格外注意，因为编辑器无法检查出执行错误。</p>
<p>　　实现ISynchronizeInvoke要求你使用一个代理来在后期绑定（late binding）中动态地调用方法。每一种代理类型均提供DynamicInvoke()方法： public object DynamicInvoke(object[] <br />
args);</p>
<p>　　理论上来说，你必须将一个方法代理放到一个需要提供对象运行的真实的线程中去，并使Invoke() 和BeginInvoke()方法中的代理中调用DynamicInvoke()方法。ISynchronizeInvoke的实现是一个非同一般的编程技巧，本文附带的源文件中包含了一个名为Synchronizer的帮助类（helper class）和一个测试程序，这个测试程序是用来论证列表A中的Calculator类是如何用Synchronizer类来实现ISynchronizeInvoke的。Synchronizer是ISynchronizeInvoke的一个普通实现，你可以使用它的派生类或者将其本身作为一个对象来使用，并将ISynchronizeInvoke实现指派给它。 </p>
<p>　　用来实现Synchronizer的一个重要元素是使用一个名为WorkerThread的嵌套类（nested class）。WorkerThread中有一个工作项目（work item）查询。WorkItem类中包含方法代理和参数。Invoke()和BeginInvoke()用来将一个工作项目实例加入到查询里。WorkerThread新建一个.NET worker线程，它负责监测工作项目的查询任务。查询到项目之后，worker会读取它们，然后调用DynamicInvoke()方法。<br />
</font></p>
<img src ="http://www.cnblogs.com/haowenbiao/aggbug/1232051.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/41816/" target="_blank">[新闻]Pogo浏览器</a>]]></description></item><item><title>使用C#在进度条中显示复制文件的进度（转）</title><link>http://www.cnblogs.com/haowenbiao/archive/2008/06/29/1232050.html</link><dc:creator>恭喜发财</dc:creator><author>恭喜发财</author><pubDate>Sun, 29 Jun 2008 09:18:00 GMT</pubDate><guid>http://www.cnblogs.com/haowenbiao/archive/2008/06/29/1232050.html</guid><wfw:comment>http://www.cnblogs.com/haowenbiao/comments/1232050.html</wfw:comment><comments>http://www.cnblogs.com/haowenbiao/archive/2008/06/29/1232050.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/haowenbiao/comments/commentRss/1232050.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/haowenbiao/services/trackbacks/1232050.html</trackback:ping><description><![CDATA[<p><font face="Verdana">使用C#在进度条中显示复制文件的进度</font></p>
<p><font face="Verdana">Code List: <br />
------------------------------------------------------------------------- </font></p>
<p><font face="Verdana">/***************************************************************** <br />
** File Name: frmMain.cs <br />
** Copyright (c) 1999 -2003 <br />
** Creator: FirePhoenix <br />
** Created Date: 2004-11-13 15:24 <br />
** Modifier: <br />
** Modify Date: <br />
** Description: <br />
** Version:1.0 <br />
******************************************************************/ </font></p>
<p><font face="Verdana">#region Using Directives <br />
using System; <br />
using System.IO ; <br />
using System.Xml ; <br />
using System.Collections ; <br />
using System.Reflection ; <br />
using System.Text ; <br />
using System.Data ; <br />
using System.ComponentModel; <br />
using System.Windows.Forms; <br />
using System.Drawing; <br />
using System.Threading ; <br />
#endregion </font></p>
<p><font face="Verdana">namespace WindowsApplication4 <br />
{ <br />
/// &lt;summary&gt; <br />
/// Copy Large File <br />
/// &lt;/summary&gt; <br />
public class frmMain : System.Windows.Forms.Form <br />
{ <br />
#region <br />
private System.Windows.Forms.ProgressBar progressBar1; <br />
private System.Windows.Forms.Button btnCopy; <br />
/// &lt;summary&gt; <br />
/// 必需的设计器变量。 <br />
/// &lt;/summary&gt; <br />
private System.ComponentModel.Container components = null; </font></p>
<p><font face="Verdana">public frmMain() <br />
{ <br />
// <br />
// Windows 窗体设计器支持所必需的 <br />
// <br />
InitializeComponent(); </font></p>
<p><font face="Verdana">// <br />
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 <br />
// <br />
} </font></p>
<p><font face="Verdana">/// &lt;summary&gt; <br />
/// 清理所有正在使用的资源。 <br />
/// &lt;/summary&gt; <br />
protected override void Dispose( bool disposing ) <br />
{ <br />
if( disposing ) <br />
{ <br />
if (components != null) <br />
{ <br />
components.Dispose(); <br />
} <br />
} <br />
base.Dispose( disposing ); <br />
} </font></p>
<p><font face="Verdana">#region Initialize Components <br />
/// &lt;summary&gt; <br />
/// 设计器支持所需的方法 - 不要使用代码编辑器修改 <br />
/// 此方法的内容。 <br />
/// &lt;/summary&gt; <br />
private void InitializeComponent() <br />
{ <br />
this.progressBar1 = new System.Windows.Forms.ProgressBar(); <br />
this.btnCopy = new System.Windows.Forms.Button(); <br />
this.SuspendLayout(); <br />
// <br />
// progressBar1 <br />
// <br />
this.progressBar1.Location = new System.Drawing.Point(8, 16); <br />
this.progressBar1.Name = "progressBar1"; <br />
this.progressBar1.Size = new System.Drawing.Size(208, 16); <br />
this.progressBar1.TabIndex = 0; <br />
// <br />
// btnCopy <br />
// <br />
this.btnCopy.Location = new System.Drawing.Point(8, 48); <br />
this.btnCopy.Name = "btnCopy"; <br />
this.btnCopy.TabIndex = 1; <br />
this.btnCopy.Text = "Copy"; <br />
this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click); <br />
// <br />
// frmMain <br />
// <br />
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); <br />
this.ClientSize = new System.Drawing.Size(232, 77); <br />
this.Controls.Add(this.btnCopy); <br />
this.Controls.Add(this.progressBar1); <br />
this.Name = "frmMain"; <br />
this.Text = "Copy File"; <br />
this.ResumeLayout(false); </font></p>
<p><font face="Verdana">} <br />
#endregion </font></p>
<p><font face="Verdana">/// &lt;summary&gt; <br />
/// Entry Point <br />
/// &lt;/summary&gt; <br />
[STAThread] <br />
static void Main() <br />
{ <br />
Application.Run(new frmMain()); <br />
} </font></p>
<font face="Verdana">
<p><br />
#endregion </p>
<p>int totalSize; //Total Size <br />
int position; //Position <br />
const int BUFFER_SIZE = 4096; <br />
byte[] buffer; <br />
Stream stream; </p>
<p>private void btnCopy_Click(object sender, System.EventArgs e) <br />
{ <br />
string strFile = ""; </p>
<p>OpenFileDialog dlg = new OpenFileDialog(); <br />
if ( dlg.ShowDialog() == DialogResult.OK ) <br />
{ <br />
strFile = dlg.FileName ; <br />
} <br />
else <br />
{ <br />
return ; <br />
} </p>
<p>FileStream fs = new FileStream( strFile , FileMode.Open , FileAccess.Read ) ; <br />
totalSize = (int)fs.Length ; <br />
stream = fs; </p>
<p>//Delete file which aready exists. <br />
if ( File.Exists( "c:\\copyedFile.bin" ) ) <br />
File.Delete( "c:\\copyedFile.bin" ); </p>
<p>//Copy file while larger than 4KB. <br />
if ( totalSize &gt; BUFFER_SIZE ) <br />
{ <br />
buffer = new byte[ BUFFER_SIZE ]; </p>
<p>// Async Invoke <br />
stream.BeginRead( buffer , 0 , BUFFER_SIZE , new AsyncCallback( AsyncCopyFile ) , null ); <br />
} <br />
else <br />
{ <br />
fs.Close(); <br />
} </p>
<p>} </p>
<p>/// &lt;summary&gt; <br />
/// Asynchronously copy file <br />
/// &lt;/summary&gt; <br />
/// &lt;param name="ar"&gt;&lt;/param&gt; <br />
private void AsyncCopyFile( IAsyncResult ar ) <br />
{ <br />
int readedLength ; </p>
<p>// Lock FileStream <br />
lock( stream ) <br />
{ <br />
readedLength = stream.EndRead( ar ); // When stream endread, get readed length <br />
} </p>
<p>// Write to disk <br />
FileStream fsWriter = new FileStream( "C:\\copyedFile.bin" , FileMode.Append , FileAccess.Write ); <br />
fsWriter.Write( buffer , 0 , buffer.Length ); <br />
fsWriter.Close(); </p>
<p>// Current stream position <br />
position += readedLength; </p>
<p>// Response UI <br />
MethodInvoker m = new MethodInvoker( SynchProgressBar ); <br />
m.BeginInvoke( null , null ); </p>
<p>if ( position &gt;= totalSize ) // Read over. <br />
{ <br />
stream.Close(); //Close FileStream <br />
return ; <br />
} </p>
<p>// Continue to read and write <br />
lock ( stream ) <br />
{ <br />
int leftSize = totalSize - position; </p>
<p>if ( leftSize &lt; BUFFER_SIZE ) <br />
buffer = new byte[ leftSize ]; </p>
<p>stream.BeginRead( buffer , 0 , buffer.Length , new AsyncCallback( AsyncCopyFile ) , null ); </p>
<p>} <br />
} </p>
<p>private void SynchProgressBar() <br />
{ <br />
this.progressBar1.Maximum = totalSize; <br />
this.progressBar1.Value = position ; <br />
} </p>
<p>} <br />
} <br />
</font></p>
<img src ="http://www.cnblogs.com/haowenbiao/aggbug/1232050.html?type=1" width = "1" height = "1" /><br><br><a href="http://news.cnblogs.com/n/41816/" target="_blank">[新闻]Pogo浏览器</a>]]></description></item></channel></rss>