一、从控制台读取东西代码片断:
using system;
class testreadconsole
{
public static void main()
{
console.write(enter your name:);
string strname = console.readline();
console.writeline( hi + strname);
}
}
二、读文件代码片断:
using system;
using system.io;
public class testreadfile
{
public static void main(string[] args)
{
// read text file c:/temp/test.txt
filestream fs = new filestream(@c:/temp/test.txt , filemode.open, fileaccess.read);
streamreader sr = new streamreader(fs);
string line=sr.readline();
while (line!=null)
{
console.writeline(line);
line=sr.readline();
}
sr.close();
fs.close();
}
}
三、写文件代码:
using system;
using system.io;
public class testwritefile
{
public static void main(string[] args)
{
// create a text file c:/temp/test.txt
filestream fs = new filestream(@c:/temp/test.txt , filemode.openorcreate, fileaccess.write);
streamwriter sw = new streamwriter(fs);
// write to the file using streamwriter class
sw.basestream.seek(0, seekorigin.end);
sw.writeline( first line );
sw.writeline( second line);
sw.flush();
}
}
四、拷贝文件:
using system;
using system.io;
class testcopyfile
{
public static void main()
{
file.copy(c://temp//source.txt, c://temp//dest.txt );
}
}
五、移动文件:
using system;
using system.io;
class testmovefile
{
public static void main()
{
file.move(c://temp//abc.txt, c://temp//def.txt );
}
}
六、使用计时器:
using system;
using system.timers;
class testtimer
{
public static void main()
{
timer timer = new timer();
timer.elapsed += new elapsedeventhandler( displaytimeevent );
timer.interval = 1000;
timer.start();
timer.enabled = true;
while ( console.read() != 'q' )
{
}
}
public static void displaytimeevent( object source, elapsedeventargs e )
{
console.write(/r{0}, datetime.now);
}
}
七、调用外部程序:
class test
{
static void main(string[] args)
{
system.diagnostics.process.start(notepad.exe);
}
}
ado.net方面的:
八、连接access数据库:
using system;
using system.data;
using system.data.oledb;
class testado
{
static void main(string[] args)
{
string strdsn = provider=microsoft.jet.oledb.4.0;data source=c://test.mdb;
string strsql = select * from employees ;
oledbconnection conn = new oledbconnection(strdsn);
oledbcommand cmd = new oledbcommand( strsql, conn );
oledbdatareader reader = null;
try
{
conn.open();
reader = cmd.executereader();
while (reader.read() )
{
console.writeline(first name:{0}, last name:{1}, reader[firstname], reader[lastname]);
}
}
catch (exception e)
{
console.writeline(e.message);
}
finally
{
conn.close();
}
}
}
九、连接sql server数据库:
using system;
using system.data.sqlclient;
public class testado
{
public static void main()
{
sqlconnection conn = new sqlconnection(data source=localhost; integrated security=sspi; initial catalog=pubs);
sqlcommand cmd = new sqlcommand(select * from employees, conn);
try
{
conn.open();
sqldatareader reader = cmd.executereader();
while (reader.read())
{
console.writeline(first name: {0}, last name: {1}, reader.getstring(0), reader.getstring(1));
}
reader.close();
conn.close();
}
catch(exception e)
{
console.writeline(exception occured -->> {0},e);
}
}
}
十、从sql内读数据到xml:
using system;
using system.data;
using system.xml;
using system.data.sqlclient;
using system.io;
public class testwritexml
{
public static void main()
{
string strfilename=c:/temp/output.xml;
sqlconnection conn = new sqlconnection(server=localhost;uid=sa;pwd=;database=db);
string strsql = select firstname, lastname from employees;
sqldataadapter adapter = new sqldataadapter();
adapter.selectcommand = new sqlcommand(strsql,conn);
// build the dataset
dataset ds = new dataset();
adapter.fill(ds, employees);
// get a filestream object
filestream fs = new filestream(strfilename,filemode.openorcreate,fileaccess.write);
// apply the writexml method to write an xml document
ds.writexml(fs);
fs.close();
}
}
十一、用ado添加数据到数据库中:
using system;
using system.data;
using system.data.oledb;
class testado
{
static void main(string[] args)
{
string strdsn = provider=microsoft.jet.oledb.4.0;datasource=c:/test.mdb;
string strsql = insert into employee(firstname, lastname) values('firstname', 'lastname') ;
// create objects of adoconnection and adocommand
oledbconnection conn = new oledbconnection(strdsn);
oledbcommand cmd = new oledbcommand( strsql, conn );
try
{
conn.open();
cmd.executenonquery();
}
catch (exception e)
{
console.writeline(oooops. i did it again:/n{0}, e.message);
}
finally
{
conn.close();
}
}
}
十二、使用oleconn连接数据库:
using system;
using system.data;
using system.data.oledb;
class testado
{
static void main(string[] args)
{
string strdsn = provider=microsoft.jet.oledb.4.0;datasource=c:/test.mdb;
string strsql = select * from employee ;
oledbconnection conn = new oledbconnection(strdsn);
oledbdataadapter cmd = new oledbdataadapter( strsql, conn );
conn.open();
dataset ds = new dataset();
cmd.fill( ds, employee );
datatable dt = ds.tables[0];
foreach( datarow dr in dt.rows )
{
console.writeline(first name: + dr[firstname].tostring() + last name: + dr[lastname].tostring());
}
conn.close();
}
}
十三、读取表的属性:
using system;
using system.data;
using system.data.oledb;
class testado
{
static void main(string[] args)
{
string strdsn = provider=microsoft.jet.oledb.4.0;datasource=c:/test.mdb;
string strsql = select * from employee ;
oledbconnection conn = new oledbconnection(strdsn);
oledbdataadapter cmd = new oledbdataadapter( strsql, conn );
conn.open();
dataset ds = new dataset();
cmd.fill( ds, employee );
datatable dt = ds.tables[0];
console.writeline(field name datatype unique autoincrement allownull);
console.writeline(==================================================================);
foreach( datacolumn dc in dt.columns )
{
console.writeline(dc.columnname+ , +dc.datatype + ,+dc.unique + ,+dc.autoincrement+ ,+dc.allowdbnull );
}
conn.close();
}
}
asp.net方面的
十四、一个asp.net程序:
<%@ page language=c# %>
<script runat=server>
void button1_click(object sender, eventargs e)
{
label1.text=textbox1.text;
}
</script>
<html>
<head>
</head>
<body>
<form runat=server>
<p>
<br />
enter your name: <asp:textbox id=textbox1 runat=server></asp:textbox>
</p>
<p>
<b><asp:label id=label1 runat=server width=247px></asp:label></b>
</p>
<p>
<asp:button id=button1 onclick=button1_click runat=server text=submit></asp:button>
</p>
</form>
</body>
</html>
winform开发:
十五、一个简单的winform程序:
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
public class simpleform : system.windows.forms.form
{
private system.componentmodel.container components = null;
private system.windows.forms.button button1;
private system.windows.forms.textbox textbox1;
public simpleform()
{
initializecomponent();
}
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows form designer generated code
private void initializecomponent()
{
this.components = new system.componentmodel.container();
this.size = new system.drawing.size(300,300);
this.text = form1;
this.button1 = new system.windows.forms.button();
this.textbox1 = new system.windows.forms.textbox();
this.suspendlayout();
//
// button1
//
this.button1.location = new system.drawing.point(8, 16);
this.button1.name = button1;
this.button1.size = new system.drawing.size(80, 24);
this.button1.tabindex = 0;
this.button1.text = button1;
//
// textbox1
//
this.textbox1.location = new system.drawing.point(112, 16);
this.textbox1.name = textbox1;
this.textbox1.size = new system.drawing.size(160, 20);
this.textbox1.tabindex = 1;
this.textbox1.text = textbox1;
//
// form1
//
this.autoscalebasesize = new system.drawing.size(5, 13);
this.clientsize = new system.drawing.size(292, 273);
this.controls.addrange(new system.windows.forms.control[] {
this.textbox1,
this.button1});
this.name = form1;
this.text = form1;
this.resumelayout(false);
}
#endregion
[stathread]
static void main()
{
application.run(new simpleform());
}
}
十六、运行时显示自己定义的图标:
//load icon and set to form
system.drawing.icon ico = new system.drawing.icon(@c:/temp/app.ico);
this.icon = ico;
十七、添加组件到listbox中:
private void form1_load(object sender, system.eventargs e)
{
string str = first item;
int i = 23;
float flt = 34.98f;
listbox1.items.add(str);
listbox1.items.add(i.tostring());
listbox1.items.add(flt.tostring());
listbox1.items.add(last item in the list box);
}
网络方面的:
十八、取得ip地址:
using system;
using system.net;
class getip
{
public static void main()
{
iphostentry ipentry = dns.gethostbyname (localhost);
ipaddress [] ipaddr = ipentry.addresslist;
for (int i = 0; i < ipaddr.length; i++)
{
console.writeline (ip address {0}: {1} , i, ipaddr.tostring ());
}
}
}
十九、取得机器名称:
using system;
using system.net;
class getip
{
public static void main()
{
console.writeline (host name : {0}, dns.gethostname());
}
}
二十、发送邮件:
using system;
using system.web;
using system.web.mail;
public class testsendmail
{
public static void main()
{
try
{
// construct a new mail message
mailmessage message = new mailmessage();
message.from = [email protected];
message.to = [email protected];
message.cc = ;
message.bcc = ;
message.subject = subject;
message.body = content of message;
//if you want attach file with this mail, add the line below
message.attachments.add(new mailattachment(c://attach.txt, mailencoding.base64));
// send the message
smtpmail.send(message);
system.console.writeline(message has been sent);
}
catch(exception ex)
{
system.console.writeline(ex.message.tostring());
}
}
}
二十一、根据ip地址得出机器名称:
using system;
using system.net;
class resolveip
{
public static void main()
{
iphostentry ipentry = dns.resolve(172.29.9.9);
console.writeline (host name : {0}, ipentry.hostname);
}
}
gdi+方面的:
二十二、gdi+入门介绍:
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
public class form1 : system.windows.forms.form
{
private system.componentmodel.container components = null;
public form1()
{
initializecomponent();
}
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows form designer generated code
private void initializecomponent()
{
this.autoscalebasesize = new system.drawing.size(5, 13);
this.clientsize = new system.drawing.size(292, 273);
this.name = form1;
this.text = form1;
this.paint += new system.windows.forms.painteventhandler(this.form1_paint);
}
#endregion
[stathread]
static void main()
{
application.run(new form1());
}
private void form1_paint(object sender, system.windows.forms.painteventargs e)
{
graphics g=e.graphics;
g.drawline(new pen(color.blue),10,10,210,110);
g.drawrectangle(new pen(color.red),10,10,200,100);
g.drawellipse(new pen(color.yellow),10,150,200,100);
}
}
xml方面的:
二十三、读取xml文件:
using system;
using system.xml;
class testreadxml
{
public static void main()
{
xmltextreader reader = new xmltextreader(c://test.xml);
reader.read();
while (reader.read())
{
reader.movetoelement();
console.writeline(xmltextreader properties test);
console.writeline(===================);
// read this properties of element and display them on console
console.writeline(name: + reader.name);
console.writeline(base uri: + reader.baseuri);
console.writeline(local name: + reader.localname);
console.writeline(attribute count: + reader.attributecount.tostring());
console.writeline(depth: + reader.depth.tostring());
console.writeline(line number: + reader.linenumber.tostring());
console.writeline(node type: + reader.nodetype.tostring());
console.writeline(attribute count: + reader.value.tostring());
}
}
}
二十四、写xml文件:
using system;
using system.xml;
public class testwritexmlfile
{
public static int main(string[] args)
{
try
{
// creates an xml file is not exist
xmltextwriter writer = new xmltextwriter(c://temp//xmltest.xml, null);
// starts a new document
writer.writestartdocument();
//write comments
writer.writecomment(commentss: xmlwriter test program);
writer.writeprocessinginstruction(instruction,person record);
// add elements to the file
writer.writestartelement(p, person, urn:person);
writer.writestartelement(lastname,);
writer.writestring(chand);
writer.writeendelement();
writer.writestartelement(firstname,);
writer.writestring(mahesh);
writer.writeendelement();
writer.writeelementint16(age,, 25);
// ends the document
writer.writeenddocument();
}
catch (exception e)
{
console.writeline (exception: {0}, e.tostring());
}
return 0;
}
}
web service方面的:
二十五、一个web service的小例子:
<% @webservice language=c# class=testws %>
using system.web.services;
public class testws : system.web.services.webservice
{
[webmethod()]
public string stringfromwebservice()
{
return this is a string from web service.;
}
}网站运营seo文章大全提供全面的站长运营经验及seo技术!