为项目组写了几个Howto的Case。

本文描述了一个用C#实现了在Windows Form或者Windows Console下调用Web Service的例子。
这是一个较复杂的例子。解决方案由三个项目组成:

  • ClassLibrary:定义了两个自定义类,编译成Windows Class Library(DLL),并由另外两个项目引用
  • WebServiceHowto:Web Service的实现,主要是一个GetStudent的WebMethod,接受一个自定义类Dorm为参数,返回另一个自定义类Student。
  • UseWebServiceHowto:Windows Console(也可以是Windows Form),实现一个Proxy,通过添加Service Reference调用Web Service。

1. ClassLibrary
Dorm.cs
[code=’c#’]
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace ClassLibrary
{
[Serializable, XmlRoot(Namespace = “http://its.hpcc.tongji.edu.cn”)]
public class Dorm
{
[XmlAttribute]
public string Name;
[XmlElement]
public string Number;
}
}
[/code]

Student.cs
[code=’c#’]
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace ClassLibrary
{
[Serializable, XmlRoot(Namespace = “http://its.hpcc.tongji.edu.cn”)]
public class Student
{
[XmlElement]
public long Number;
[XmlElement]
public string Name;
[XmlElement]
public bool enabled;
[XmlElement]
public Dorm dorm;
}
}
[/code]

2. WebServiceHowto
ITSTestService.asmx.cs
[code=’c#’]
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using ClassLibrary;

namespace WebServiceHowto
{
///

/// Summary description for ITSTestService
///

[WebService(Namespace = “http://hpcc.tongji.edu.cn/”)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class ITSTestService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return “Hello World”;
}

[WebMethod]
public Student GetStudent(Dorm d)
{
Student student = new Student();

student.dorm = d;
student.enabled = true;
student.Number = 222222;
student.Name = “maojun”;

return student;
}
}
}
[/code]

3. UseWebServiceHowto
Program.cs
[code=’c#’]
using System;
using System.Collections.Generic;
using System.Text;
using ClassLibrary;

namespace UseWebServiceHowto
{
class Program
{
static void Main(string[] args)
{
WebServiceProxy proxy = new WebServiceProxy();
Dorm dorm = new Dorm();

dorm.Name = “geng der drom”;
dorm.Number = “16-501-5”;

Student s = proxy.GetStudent(dorm);
Console.WriteLine(“Student Name={0}, Dorm={1}”, s.Name, s.dorm.Name);
}
}
}
[/code]

WebServiceProxy.cs
[code=’c#’]
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Services.Protocols;
using System.Web.Services;
using System.Diagnostics;
using System.ComponentModel;
using ClassLibrary;

namespace UseWebServiceHowto
{
[WebServiceBinding(Name = “TestWebServiceSoap”, Namespace = “http://hpcc.tongji.edu.cn/”), DebuggerStepThrough, DesignerCategory(“code”)]
class WebServiceProxy : SoapHttpClientProtocol
{
public WebServiceProxy()
{
base.Url = “http://localhost:3489/ITSTestService.asmx”;
}

[SoapDocumentMethod(“http://hpcc.tongji.edu.cn/GetStudent”, RequestNamespace = “http://hpcc.tongji.edu.cn/”, ResponseNamespace = “http://hpcc.tongji.edu.cn/”, Use = System.Web.Services.Description.SoapBindingUse.Default, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Default)]
public Student GetStudent(Dorm d)
{
return (Student)base.Invoke(“GetStudent”, new object[] { d })[0];
}
}
}
[/code]

下载:用Visual Studio 2008打开
http://download.nocoo.us/Download/Archive/CSharpHowto/SQLServer.rar

为项目组写了几个Howto的Case。

本文描述了一个用C#实现类的XML序列化及反序列化。
功能是生成一个类的实例,然后序列化成XML文件形式。
或者将一个已经序列化成文件的XML反序列化成类的实例。

为了充分展示Framework功能,在一个类里面嵌入另一个类,并且XML属性字段混杂。

Student.cs
[code=’c#’]
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace XMLHowto
{
[Serializable,XmlRoot(Namespace=”http://its.hpcc.tongji.edu.cn”)]
public class Student
{
[XmlElement]
public long Number;
[XmlElement]
public string Name;
[XmlElement]
public bool enabled;
[XmlElement]
public Dorm dorm;
}
}
[/code]

Dorm.cs
[code=’c#’]
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace XMLHowto
{
[Serializable, XmlRoot(Namespace = “http://its.hpcc.tongji.edu.cn”)]
public class Dorm
{
[XmlAttribute]
public string Name;
[XmlElement]
public string Number;
}
}
[/code]

Program.cs
[code=’c#’]
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace XMLHowto
{
class Program
{
static void Main(string[] args)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Student));

//序列化
FileStream fs1 = new FileStream(“./student.xml”, FileMode.Create, FileAccess.Write);
Student student = new Student();
Dorm dorm = new Dorm();

dorm.Name = “der drom”;
dorm.Number = “16-501-1”;

student.dorm = dorm;
student.enabled = true;
student.Number = 222222;
student.Name = “maojun”;

xmlSerializer.Serialize(fs1, student);

//反序列化
FileStream fs2 = new FileStream(“./student2.xml”, FileMode.Open, FileAccess.Read);
Student student2 = (Student)xmlSerializer.Deserialize(fs2);
}
}
}
[/code]

下载:用Visual Studio 2008打开
http://download.nocoo.us/Download/Archive/CSharpHowto/XML.rar

为项目组写了几个Howto的Case。

本文描述了一个用C#实现的简单的SQL Server查询。
功能是每次执行插入一行记录,然后输出全部记录。

Program.cs
[code=’c#’]
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace SQLServerHowto
{
class Program
{
static void Main(string[] args)
{
SqlConnection myConnection = new SqlConnection(“Data Source=itsdbserver;user id=its;password=its;database=ITSTestDB”);
myConnection.Open();

string sql = “insert into news (news) values (@value)”;
SqlCommand sc = new SqlCommand(sql, myConnection);
SqlParameter param = new SqlParameter(“value”, “现在是” + DateTime.Now.ToString() + “,但毛君还是垃圾”);
sc.Parameters.Add(param);

int count = sc.ExecuteNonQuery();
Console.WriteLine(“Line inserted: ” + count.ToString());

string sql2 = “select * from News”;
SqlCommand sc2 = new SqlCommand(sql2, myConnection);
SqlDataReader reader = sc2.ExecuteReader();

while (reader.Read())
{
Console.WriteLine(String.Format(“ID={0}, news={1}”, reader[0], reader[1]));
}
}
}
}
[/code]

下载:用Visual Studio 2008打开
http://download.nocoo.us/Download/Archive/CSharpHowto/SQLServer.rar

为项目组写了几个Howto的Case。

本文描述了一个用C#实现的简单的Windows Form。
功能是先出现一个登陆窗口,然后用户输入用户名密码,登陆窗口隐藏,加载主窗口。

LoginForm.cs
[code=’c#’]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormHowto
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = “”;
textBox2.Text = “”;
}

private void button1_Click(object sender, EventArgs e)
{
MainForm mainForm = new MainForm(textBox1.Text);
this.Hide();
mainForm.Show();
}
}
}
[/code]

下载:用Visual Studio 2008打开
http://download.nocoo.us/Download/Archive/CSharpHowto/WindowsForm.rar

为项目组写了几个Howto的Case。

本文描述了一个用C#实现的简单的Windows Service。
功能是在启动和停止服务的时候,在Windows事件查看器(Event Log)里添加一条log。

在Visual Studio 2008中建立Windows Service项目之后,会自动生成一个Service1的服务,它的名字默认是Service1,我先把它的文件名从Service1.cs改成ITSTestService.cs,然后需要在ITSTestService.cs的设计器视图的属性里,把Service Name改成ITSTestService,不然添加完服务后,启动服务时会报出这样的错误:

由于下列错误,ITS Test Service 服务启动失败:
配置成在该可执行程序中运行的这个服务不能执行该服务。

因为服务安装的名字是Service1。

Windows系统添加服务:
[code=’c#’]sc create ITSTestService binpath= “PATH TO SERVICE EXE” type= share start= auto displayname= “ITS Test Service”[/code]

Windows系统删除服务:
[code=’c#’]sc delete ITSTestService[/code]

ITSTestService.cs
[code=’c#’]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;

namespace WindowsServiceHowto
{
public partial class ITSTestService : ServiceBase
{
public ITSTestService()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
WriteLog(“ITSTestService Started.”);
}

protected override void OnStop()
{
WriteLog(“ITSTestService Stopped.”);
}

private void WriteLog(string eventString)
{
string source;
string log;
source = “ITS Test Windows Service”;
log = “Application”;

if (!EventLog.SourceExists(source))
{
EventLog.CreateEventSource(source, log);
}

EventLog.WriteEntry(source, eventString);
}
}
}
[/code]

下载:用Visual Studio 2008打开
http://download.nocoo.us/Download/Archive/CSharpHowto/WindowsService.rar

1. Windows API
[code=’c#’]
[DllImport(“User32.dll”, CharSet = CharSet.Auto)]
public static extern
IntPtr SetClipboardViewer(IntPtr hWnd);

[DllImport(“User32.dll”, CharSet = CharSet.Auto)]
public static extern
bool ChangeClipboardChain( IntPtr hWndRemove, IntPtr hWndNewNext);
[/code]

2. 注册
要想使程序加入系统剪贴操作的消息队列,要调用RegisterClipboardViewer()。这样,系统会在每次剪贴操作后发消息给程序。如果要停止监控,调用UnregisterClipboardViewer()移除即可。
[code=’c#’]
private void RegisterClipboardViewer()
{
ClipboardViewerNext = SetClipboardViewer(this.Handle);
}

private void UnregisterClipboardViewer()
{
ChangeClipboardChain(this.Handle, ClipboardViewerNext);
}
[/code]

3. 事件处理
[code=’c#’]
protected override void WndProc(ref Message m)
{
switch ((int)m.Msg)
{
case 0x308: //WM_DRAWCLIPBOARD
{
IDataObject data = Clipboard.GetDataObject();
// 这里的content就是这次剪贴操作,系统剪贴板里的内容的string形式。
// 取得这个内容之后就可以写处理函数。
string content = (String)data.GetData(DataFormats.Text);
break;
}
default:
{
base.WndProc(ref m);
break;
}
}
}
[/code]

1. C语言
毫无疑问, 第一门语言是C语言. 我上大一的时候一样有疑问, 为什么中国的高校那么迂腐, 都什么年代了, 外面都Java了, 我们还在C… 还不是C++. 其实, 事实证明, 后来毕业的时候, 被人称作计算机的强人都是当年C语言程序设计考试考优的那几个人. 一个人对于C语言的态度, 其实一定程度上反映了这个人对于计算机科学和程序设计的热衷程度. 我们二三年级的班主任经常讲, 从事计算机工作, 一定要对编程抱有兴趣, 否则是无论如何也学不好的. 如果你发现你对编程根本没有兴趣, 建议趁早转专业. C语言考试当中如果有超过5处地方有问题, 就是不合格. 对于谭浩强版C语言书里面的知识点, 不能有任何疑问. 这也就是为什么谭浩强版C语言教材至今仍在中国书店计算机及网络类畅销排行第二名的原因.

2. C++
C++是不能绕开的一门语言. C++是一种思想, 不学C++不知道Java或者C#的好. 苦其心志是必要的, 虽然我对于C++依然反感的很… 太复杂太混乱太危险了. 不过, 我同样认为, 对于计算机系学生来说, 经过自己的体验知道C++为什么太复杂太混乱太危险是很有必要的. 此外, C++应该是你从TC转向Visual Studio 2005的跳板. C++是一门系统编程语言, 不适合用来写应用程序.

3. HTML/XML/CSS
必须精通的技术. 迟早一天, 所有程序都被XML化. 目前的趋势是WPF已经把应用程序界面表示XML化, DBMS也以支持XML作为重要的大版本特性. 何况, 计算机专业要想挣钱, 网站是必须要做的… 之于CSS, 初学者就不要经历我们走过的那些阴沟了吧. 听W3C的没错. 说起来我做网页也有10个年头了, 初中就开始写HTML, 完全是看HTML标准成长起来的一代人, 那时候还幼稚的很, 当发现可以用表格组织网页框架结构的时候, 欣喜若狂, 自认是个天才, 四处找人想分享经验, 才发现高处不胜寒. 如今, Div+CSS已经变成了我标准的工作语言, FrontPage和Dreamweaver则是早就早就不用了的古董了. UltraEdit是我的最爱.

4. JavaScript/AJAX
如果想走网站这条路, 精通JavaScript是必要的. JavaScript是一门类C语言, 学起来比较轻松, 而且因为JavaScript出了名的难以调试, 可以锻炼程序调试技术. JavaScript技术路线走下去就是大红大紫的Ajax了. 其实没什么, JavaScript和DOM戏法而已.

5. 对Visual Basic的态度
我不建议学习Visual Basic. 将VB纳入.NET框架实际上是一个失误. 从那一刻起, VB就已经死去. 计算机系的态度是, 各科课程设计不能使用VB编写.

6. ASP或PHP
网站路线必经之路. 因为ASP是类VB的, 所以我觉得不太值得为了ASP学VB. 因此, 类C的PHP更有价值, 而且PHP这么多年还活得好好的. 有必要把PHP作为轻量级的后台编程语言.

7. Database
MySQL和SQL Server以及Oracle最好都学习一下. 重点是前两个. Oracle实在是用不起. 占用资源太大, 不适合在自己机器上做试验. MySQL是PHP支持最好的数据库, 走网站路线的建议选择.

8. WWW和Hosting
网站路线的人不可避免的要接触域名系统和托管之类的事情. 这其实是一门学问, 书上很少讲, 却是很大很深的学问. 主要包括, 域名系统, 域名怎么解析, A记录, CNAME, MX记录是怎么回事; DNS怎么工作; 什么是虚拟主机等等. 注册一个属于自己的域名, 会对你将来找工作有很大的作用的. 用网站技术建立自己的个人主页, 然后才能搞明白Google好在哪里(Apps), 什么是SEO(又是一门大学问).

9. Java和C#
Java是一头大象了. Servlet, Swing, JSF, JSP… 名称太多太多了点… 还是C#干净点… 选择那条路, 与其说是技术问题, 不如说是政治问题, 我觉得C#好一点, 必经Sun终归要被并购掉… 微软语言的成败, 不是有语言特性决定, 而是微软平台的成败决定. 因此, C#也有风险. Java路线的话, 后面有网站后台的Servlet和JSP, C#路线则是ASP.NET. 不学PHP也无所谓. 就是主机贵一些.

10. 操作系统
这也是一个政治问题… Windows 2003/2008是肯定要会玩的, 必经和XP/Vista那么近似, 如何在机器上架设Web服务器, 安装动态语言, 架设DBMS是基本功. 因此, 需要精通IIS/Apache/Tomcat. Linux肯定要学. 哪个发行版本我不在乎, 基本Shell命令要烂熟于心.

11. 计算机硬件
有了这些, 可以出去打工赚点小钱, 出去打工, 为自己配一台好电脑吧. 搞清楚CPU阵营, 显卡阵营, 主板阵营, 硬盘品种, 怎么超频. 组成原理, 系统结构, 汇编语言是要好好学的. 为了你将来的水平成长.

12. 设计模式
编程到这里, 基本已经饱受不恰当的系统设计折磨之苦了. 看看设计模式, 看看代码大全, 让自己变成一个爱干净的人, 代码里没有意义的空行甚至空格也不要有. 注释, 用英文写起来啦. 这是, 你就是别人眼里的高手了.

13. 算法
为了生存, 为了面试, 算法是必须精通的一步. 算法导论是一个不错的选择, 别问我英文版好还是中文版好, 都买下来, 随着MIT视频教程, 仔仔细细拿出一年时间来, 学好算法.

差不多了. 毕业吧.