青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Shuffy

不斷的學習,不斷的思考,才能不斷的進步.Let's do better together!
posts - 102, comments - 43, trackbacks - 0, articles - 19

C#入門代碼

Posted on 2007-08-31 12:34 Shuffy 閱讀(239) 評論(0)  編輯 收藏 引用 所屬分類: VC++/C/C++/C#瀏覽集合

【轉】http://blog.sjzj.com.cn/article.asp?id=567
一、從控制臺讀取東西代碼片斷: 
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(Objectsender, EventArgs e){Label1.Text=TextBox1.Text;} </script> <html> <head> </head> <body> <form runat="server"> <p> <br /> Enteryour 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 = "from@domain.com"; 
message.To = "pengyun@cobainsoft.com"; 
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的小例子: 
 

using System.Web.Services; 

public class TestWS : System.Web.Services.WebService 

[WebMethod()] 
public string StringFromWebService() 

return "This is a string from web service."; 


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            女人天堂亚洲aⅴ在线观看| 国产精品午夜在线观看| 亚洲另类自拍| 亚洲国产一区二区在线| 免费观看成人鲁鲁鲁鲁鲁视频| 午夜精品久久一牛影视| 一区二区日韩免费看| 亚洲欧美国产高清| 久久久久国产精品一区二区| 午夜在线观看欧美| 美乳少妇欧美精品| 国产精品国产a级| 国内精品一区二区三区| 亚洲欧洲偷拍精品| 亚洲欧美日韩国产综合在线| 久久国产精品99久久久久久老狼 | 欧美成人国产一区二区| 老司机一区二区| 欧美激情中文不卡| 国产精品亚发布| 亚洲福利一区| 亚洲女人小视频在线观看| 久久久国产精品一区二区三区| 亚洲精品一区二区三区福利| 一区二区三区国产在线| 久久久久久尹人网香蕉| 亚洲国产成人精品久久| 亚洲欧美视频一区二区三区| 欧美a级在线| 国产伊人精品| 亚洲精品永久免费| 久久精品亚洲一区| 亚洲美女av电影| 久久久亚洲高清| 国产精品一区二区久久| 99视频精品全国免费| 免费成人av资源网| 午夜精品久久久久久| 国产精品国产三级国产aⅴ9色| 欧美伦理一区二区| 经典三级久久| 欧美在线欧美在线| 一区二区三区日韩欧美| 欧美国产日韩二区| 在线播放日韩欧美| 久久久久综合网| 亚洲一级黄色片| 欧美日韩三级在线| 久久天天躁夜夜躁狠狠躁2022| 欧美精品一区二区三区四区| 国产精品伊人日日| 99re视频这里只有精品| 久久一区中文字幕| 亚洲欧美日韩一区二区| 欧美三级网址| 99视频有精品| 亚洲国产女人aaa毛片在线| 久久久久久久久岛国免费| 国产日韩精品视频一区二区三区| 国产欧美一区二区精品性| 一区二区三区免费看| 欧美成人久久| 久久影院午夜论| 亚洲欧洲日韩在线| 91久久精品国产91久久性色tv| 亚洲欧洲精品一区| 欧美成人免费在线观看| 在线看日韩欧美| 欧美国产免费| 欧美大片免费观看在线观看网站推荐| 欧美精品一区二区在线观看| 亚洲国产精品尤物yw在线观看| 激情久久一区| 久久视频免费观看| 久久色在线播放| 亚洲免费久久| 一本一本久久a久久精品牛牛影视| 中文高清一区| 国产精品资源| 久久久久久免费| 久久久久久久综合狠狠综合| 亚洲国产精品一区制服丝袜| 亚洲欧洲日本国产| 国产精品a久久久久久| 久久精品视频一| 麻豆91精品91久久久的内涵| 亚洲精品免费电影| 亚洲一区3d动漫同人无遮挡| 国产欧美精品xxxx另类| 免费日韩av片| 欧美亚洲第一区| 久久亚洲综合色一区二区三区| 亚洲精品1234| 欧美午夜激情在线| 久久精品视频在线| 欧美插天视频在线播放| 午夜精品久久| 久久精品亚洲一区二区| 日韩视频不卡中文| 香蕉成人伊视频在线观看| 亚洲经典视频在线观看| 亚洲一区二区三区欧美| 91久久精品国产91性色tv| 中文成人激情娱乐网| 在线看欧美视频| 亚洲一区二区精品| 亚洲欧洲精品一区| 小黄鸭精品aⅴ导航网站入口 | 欧美精品v国产精品v日韩精品| 国产精品亚洲精品| 欧美一级夜夜爽| 麻豆av一区二区三区久久| 亚洲黑丝在线| 亚洲欧美电影在线观看| 国产精品一区二区久久精品| 欧美一区二区三区在线观看| 欧美一区二区三区四区高清 | 久久xxxx| 国产亚洲一区二区三区| 亚洲国产成人av好男人在线观看| 亚洲裸体俱乐部裸体舞表演av| 日韩视频在线一区二区| 国产专区综合网| 亚洲午夜激情免费视频| 亚洲最快最全在线视频| 另类欧美日韩国产在线| 久久久噜噜噜久久久| 欧美精品1区2区| 久久日韩粉嫩一区二区三区| 欧美天天视频| 欧美激情久久久| 国际精品欧美精品| 日韩视频一区二区在线观看| 在线观看日产精品| 久久狠狠亚洲综合| 久久婷婷国产综合国色天香| 国产日韩亚洲欧美| 欧美一区二区三区四区高清| 久久精品视频网| 国产视频精品网| 性18欧美另类| 久久综合色婷婷| 亚洲国产美女| 欧美电影免费| 亚洲欧洲一区| 99国产精品久久久久久久成人热| 亚洲精品久久久久久一区二区| 一本久道久久综合狠狠爱| 在线日本欧美| 欧美激情五月| 9人人澡人人爽人人精品| 亚洲精品在线电影| 欧美午夜三级| 亚洲综合另类| 免费在线观看成人av| 亚洲国产成人精品视频| 欧美福利视频| 一区二区高清在线观看| 欧美伊久线香蕉线新在线| 狠狠爱综合网| 欧美成人精品激情在线观看 | 亚洲精品美女在线| 在线视频你懂得一区| 欧美视频精品一区| 亚洲破处大片| 国产手机视频精品| 久久久久综合网| 亚洲动漫精品| 亚洲激情六月丁香| 欧美日韩国产免费| 亚洲综合精品| 久久综合一区| 在线视频你懂得一区二区三区| 一区二区三区偷拍| 亚洲一区图片| 国内一区二区在线视频观看 | 亚洲福利专区| 欧美人妖在线观看| 亚洲欧美日韩在线不卡| 久久综合99re88久久爱| 亚洲精品久久久蜜桃| 国产一级揄自揄精品视频| 欧美久久成人| 久久午夜羞羞影院免费观看| 亚洲福利视频网| 亚洲欧美日韩在线高清直播| 国户精品久久久久久久久久久不卡| 男人的天堂亚洲| 日韩午夜av电影| 国产一区二区精品久久99| 久久人人爽人人爽| 中文欧美在线视频| 欧美激情免费在线| 亚洲欧美激情四射在线日 | 亚洲制服丝袜在线| 欧美高清在线一区二区| 欧美中在线观看| 99re6这里只有精品视频在线观看 99re6这里只有精品 | 国产真实久久| 国产精品爱久久久久久久|