




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、窗體頂端一、 從控制臺讀取東西代碼片斷:using System;class TestReadConsole public static void Main() Console.Write(Enter your name:); string strName = Console.ReadLine();
2、; Console.WriteLine( Hi + strName); 二、讀文件代碼片斷:using System; using System.IO; public class TestReadFile public static void Main(String args) / Read text file C:temptest.txt
3、; FileStream fs = new FileStream(c:temptest.txt , FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs); String line=sr.Rea
4、dLine(); while (line!=null) Console.WriteLine(line); line=sr.ReadLine();
5、; sr.Close(); fs.Close(); 三、寫文件代碼:using System; using System.IO; public class TestWriteFile
6、public static void Main(String args) / Create a text file C:temptest.txt FileStream fs = new FileStream(c:temptest.txt , FileMode.OpenOrCreate, FileAccess.Write);
7、160; StreamWriter sw = new StreamWriter(fs); / Write to the file using StreamWriter class sw.BaseStream.Seek(0, SeekOrigin.End); sw.WriteLine( First Line );
8、160; sw.WriteLine( Second Line); sw.Flush(); 2 / 22四、拷貝文件:using System;using System.IO;class TestCopyFile public static void Main()
9、0; File.Copy(c:tempsource.txt, C:tempdest.txt ); 五、移動文件:using System;using System.IO;class TestMoveFile public static void Main() File.Move(c:tempabc.txt, C:tempdef.txt );
10、 六、使用計時器:using System;using System.Timers;class TestTimer public static void Main() Timer timer = new Timer(); timer.Elapsed += new ElapsedEventHandler( DisplayTimeEvent ); &
11、#160; timer.Interval = 1000; timer.Start(); timer.Enabled = true; while ( Console.Read() != 'q' )
12、60; /- public static void DisplayTimeEvent( object source, ElapsedEventArgs e ) Consol
13、e.Write(r0, DateTime.Now); 七、調用外部程序:class Test static void Main(string args) System.Diagnostics.Process.Start(notepad.exe); ADO.NET方面的:八、連接Access數據庫:using System;using System.Data;us
14、ing 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
15、; OleDbConnection conn = new OleDbConnection(strDSN); OleDbCommand cmd = new OleDbCommand( strSQL, conn ); OleDbDataReader reader = null; &
16、#160; try conn.Open(); reader = cmd.ExecuteReader(); while
17、(reader.Read() ) Console.WriteLine(First Name:0, Last Name:1, readerFirstName, readerLastName);
18、 catch (Exception e) Console.WriteLine(e.Message); &
19、#160; finally conn.Close(); 九、連接SQL Server數據庫:using System;using System.Data.SqlClient;
20、public class TestADO public static void Main() SqlConnection conn = new SqlConnection(Data Source=localhost; Integrated Security=SSPI; Initial Catalog=pubs); SqlCommand cmd
21、= new SqlCommand(SELECT * FROM employees, conn); try conn.Open();
22、; SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()
23、0; Console.WriteLine(First Name: 0, Last Name: 1, reader.GetString(0), reader.GetString(1);
24、60; reader.Close(); conn.Close(); catch(Exception e)
25、 Console.WriteLine(Exception Occured ->> 0,e); 十、從SQL內讀數據到XML:using System;using System.Data;using System.Xml;using System.Data.SqlClien
26、t; 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=
27、;database=db); String strSql = SELECT FirstName, LastName FROM employees; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = new SqlCommand(strSql,conn)
28、; / Build the DataSet DataSet ds = new DataSet(); adapter.Fill(ds, employees); / Get a FileStream object
29、0; FileStream fs = new FileStream(strFileName,FileMode.OpenOrCreate,FileAccess.Write); / Apply the WriteXml method to write an XML document ds.WriteXml(fs); fs.Cl
30、ose(); 十一、用ADO添加數據到數據庫中:using System;using System.Data; using System.Data.OleDb; class TestADO static void Main(string args) string strDSN = Provider=Microsof
31、t.Jet.OLEDB.4.0;DataSource=c:test.mdb; string strSQL = INSERT INTO Employee(FirstName, LastName) VALUES('FirstName', 'LastName') ;
32、160; / create Objects of ADOConnection and ADOCommand OleDbConnection conn = new OleDbConnection(strDSN); OleDbCommand cmd = new OleDbCommand( strS
33、QL, conn ); try conn.Open(); cmd.ExecuteNonQuery();
34、0; catch (Exception e) Console.WriteLine(Oooops. I did it again:n0, e.Message);
35、; finally conn.Close();
36、; 十二、使用OLEConn連接數據庫:using System;using System.Data; using System.Data.OleDb; class TestADO static void Main(string args)
37、 string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:test.mdb; string strSQL = SELECT * FROM employee ; OleDbConnection conn = new OleDbConnection(strDSN); &
38、#160; OleDbDataAdapter cmd = new OleDbDataAdapter( strSQL, conn ); conn.Open(); DataSet ds = new DataSet(); cmd.Fill( ds, employee );
39、0; DataTable dt = ds.Tables0; foreach( DataRow dr in dt.Rows ) Console.WriteLine(First name: + drFirstName.ToString() + Last name: + drLa
40、stName.ToString(); conn.Close(); 十三、讀取表的屬性:using System;using System.Data; using System.Data.OleDb; class TestADO static void Main(str
41、ing args) string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:test.mdb; string strSQL = SELECT * FROM employee ; OleD
42、bConnection conn = new OleDbConnection(strDSN); OleDbDataAdapter cmd = new OleDbDataAdapter( strSQL, conn ); conn.Open(); DataSet ds = new DataSet();
43、 cmd.Fill( ds, employee ); DataTable dt = ds.Tables0; Console.WriteLine(Field Name DataType Unique AutoIncrement AllowNull); Console.WriteLine(=);
44、0; foreach( DataColumn dc in dt.Columns ) Console.WriteLine(dc.ColumnName+ , +dc.DataType + ,+dc.Unique + ,+dc.AutoIncrement+ ,+dc.AllowDBNull );
45、0; conn.Close(); ASP.NET方面的十四、一個ASP.NET程序:<% Page Language=C# %><script runat=server> void Button1_Click(Object sender, EventArgs e) &
46、#160; Label1.Text=TextBox1.Text; </script><html><head></head><body> <form runat=server> <p>
47、; <br /> Enter your name: <asp:TextBox id=TextBox1 runat=server></asp:TextBox> </p> <p>
48、60; <b><asp:Label id=Label1 runat=server Width=247px></asp:Label></b> </p> <p> <a
49、sp: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
50、.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.
51、TextBox textBox1; public SimpleForm() InitializeComponent(); protected override void Dispose( bool disposing ) if( disposin
52、g ) if (components != null) compon
53、ents.Dispose(); base.Dispose( disposing ); #region Windows Form Designer generated code priva
54、te void InitializeComponent() ponents = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = Form1;
55、 this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); / / button1 &
56、#160; / 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;
57、160; / / 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.TabInd
58、ex = 1; this.textBox1.Text = textBox1; / / Form1 / this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls
59、.AddRange(new System.Windows.Forms.Control this.textBox1, this.button1); this.Name = Form1; this.Text = Form1; this.ResumeLayout(false); #endregion STATh
60、read static void Main() Application.Run(new SimpleForm(); 十六、運行時顯示自己定義的圖標:/load icon and set to formSystem.Drawing.Icon ico = new System.Drawing.Icon(c:tempapp.ico);this.Icon = ico;十七、添加組件到ListBox中:priva
61、te 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
62、.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 (
63、localhost); IPAddress IpAddr = ipEntry.AddressList; for (int i = 0; i < IpAddr.Length; i+)
64、160; Console.WriteLine (IP Address 0: 1 , i, IpAddr.ToString (); 十九、取得機器名稱:using System;using System.Net;class GetIP public static void Main()
65、 Console.WriteLine (Host name : 0, Dns.GetHostName(); 二十、發送郵件:using System;using System.Web;using System.Web.Mail;public class TestSendMail public static void Main() try
66、0; / Construct a new mail message MailMessage message = new MailMessage(); message.From
67、= from; message.To = pengyun; message.Cc = ; message.Bcc = ;
68、 message.Subject = Subject; message.Body = Content of message;
69、60; /if you want attach file with this mail, add the line below message.Attachments.Add(new MailAttachment(c:attach.txt, MailEncoding.Base64);
70、/ Send the message SmtpMail.Send(message); System.Console.WriteLine(Message has been sent);
71、60; catch(Exception ex) System.Console.WriteLine(ex.Message.ToString(); 二十一、根據IP地址得出機器名稱:using System;using System.Net
72、;class ResolveIP public static void Main() IPHostEntry ipEntry = Dns.Resolve(); Console.WriteLine (Host name : 0, ipEntry.HostName); &
73、#160; 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.Compon
74、entModel.Container components = null; public Form1() InitializeComponent(); protected override void Dispose( bool disposing )
75、0; if( disposing ) if (components != null)
76、60; components.Dispose(); base.Dispose( disposing ); #region Windows Form Designer generated code
77、160; private void InitializeComponent() this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273);
78、 this.Name = Form1; this.Text = Form1; this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); #endregion STAThread static
79、void Main() Application.Run(new Form1(); private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) Graphics g=e.Graphic
80、s; 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); XM
81、L方面的:二十三、讀取XML文件:using System;using System.Xml; class TestReadXML public static void Main() XmlTextReader reader = new XmlTextReader(C:test.xml);
82、60; reader.Read(); while (reader.Read()
83、 reader.MoveToElement(); Console.WriteLine(XmlTextReader Properties Test); Console.WriteLine(=);
84、; / Read this properties of element and display them on console Console.WriteLine(Name: + reader.Name); Console.WriteLine(Base URI: + reader.BaseURI);
85、 Console.WriteLine(Local Name: + reader.LocalName); Console.WriteLine(Attribute Count: + reader.AttributeCount.ToString();
86、0; 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();
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 中醫治療激素臉
- 2025年中國漁桿市場調查研究報告
- 2025年中國拔梢桿市場調查研究報告
- 2025年中國變徑法蘭市場調查研究報告
- 2025年中國PET環保收縮膜市場調查研究報告
- 護理個案畢業答辯匯報框架
- 心臟介入患者護理
- 靜脈導管置換護理操作規范
- 2025至2030年中國魚菌靈行業發展研究報告
- 2025至2030年中國高壓噴霧送水機行業發展研究報告
- 《低碳技術與節能減排》課程教學大綱
- 孕前口腔檢查精講課件
- 腹部帶蒂皮瓣醫學課件
- 幼兒園園長(高級)理論考試題庫(含答案)
- 美的職位與職銜管理手冊
- 華聯學院日語能力考試N5試題二及參考答案
- 《交通運輸系統分析》課程教學大綱
- 大學新生社團招新報名表通用版
- 中國足球現狀PPT
- EN60745標準理解
- 文化藝術中心裝飾裝修工程施工方案(144頁)
評論
0/150
提交評論