《c#入门代码.txt》由会员分享,可在线阅读,更多相关《c#入门代码.txt(5页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、 1.100020000 2. Gphotoshop3.G ,wordexcel chenvyong pdfWAP鲻TXT ( 1wowait0 docWAP鲻TXT C# 2006-03-06 05:41 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;
2、 public class TestReadFile public static void Main(String args) / Read text file C:temptest.txt FileStream fs = new FileStream(c:temptest.txt , FileMode.Open, FileAccess. Read); StreamReader sr = new StreamReader(fs); String line=sr.ReadLine(); while (line!=null) Console.WriteLine(line); line=sr.Rea
3、dLine(); sr.Close(); fs.Close(); using System; using System.IO; public class TestWriteFile public static void Main(String args) / Create a text file C:temptest.txt FileStream fs = new FileStream(c:temptest.txt , FileMode.OpenOrCreate, Fil eAccess.Write); StreamWriter sw = new StreamWriter(fs); / Wri
4、te 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:tempsource.txt, C:tempdest.txt ); 塢 using System; using System.IO
5、; class TestMoveFile public static void Main() File.Move(c:tempabc.txt, C:tempdef.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.E
6、nabled = true; while ( Console.Read() != q ) public static void DisplayTimeEvent( object source, ElapsedEventArgs e ) 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 = Pr
7、ovider=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()
8、 ) Console.WriteLine(First Name:0, Last Name:1, readerFirstName, re aderLastName); 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
9、Source=localhost; Integrated S ecurity=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
10、); 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; SqlConne
11、ction conn = new SqlConnection(server=localhost;uid=sa;pwd=;databa se=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, e
12、mployees); / 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(str
13、ing args) string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:test.mdb; string strSQL = INSERT INTO Employee(FirstName, LastName) VALUES(FirstNa me, LastName) ; / create Objects of ADOConnection and ADOCommand OleDbConnection conn = new OleDbConnection(strDSN); OleDbCommand cmd = new OleDb
14、Command( strSQL, conn ); try conn.Open(); cmd.ExecuteNonQuery(); catch (Exception e) Console.WriteLine(Oooops. I did it again:n0, e.Message); finally class TestADO static void Main(string args) string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:test.mdb; string strSQL = SELECT * FROM empl
15、oyee ; OleDbConnection conn = new OleDbConnection(strDSN); OleDbDataAdapter cmd = new OleDbDataAdapter( strSQL, conn ); conn.Open(); DataSet ds = new DataSet(); cmd.Fill( ds, employee ); DataTable dt = ds.Tables0; foreach( DataRow dr in dt.Rows ) Console.WriteLine(First name: + drFirstName.ToString(
16、) + Last name: + drLastName.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 OleD
17、bConnection(strDSN); OleDbDataAdapter cmd = new OleDbDataAdapter( strSQL, conn ); conn.Open(); DataSet ds = new DataSet(); cmd.Fill( ds, employee ); DataTable dt = ds.Tables0; Console.WriteLine(Field Name DataType Unique AutoIncrement AllowNull); Console.WriteLine(= =); foreach( DataColumn dc in dt.
18、Columns ) Console.WriteLine(dc.ColumnName+ , +dc.DataType + ,+dc.Unique + ,+dc. AutoIncrement+ ,+dc.AllowDBNull ); conn.Close(); ASP.NET ASP.NET Enter your name: WinForm 塢 塢 WinForm using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windo
19、ws.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 D
20、ispose( bool disposing ) if( disposing ) if (components != null) components.Dispose(); base.Dispose( disposing ); #region Windows Form Designer generated code private void InitializeComponent() ponents = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text =
21、 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.Ta
22、bIndex = 0; this.button1.Text = button1; / / textBox1 / this.textBox1.Location = new System.Drawing.Point(112, 16); this.textBox1.Name = textBox1; this.text / Form1 / this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.AddRange(
23、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:tempapp.ico); this.I
24、con = 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
25、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 vo
26、id 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; message.To message.Cc = pengyun;
27、= ; 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.B ase64); / Send the message SmtpMail.Send(message); System.Console.WriteLine(Message ha
28、s 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.Draw
29、ing; 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() Initialize protected override void Dispose( bool disposing ) if( disposi
30、ng ) 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 = F
31、orm1; 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);
32、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()
33、; 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(Attribut
34、e 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; u
35、sing System.Xml; public class TestWriteXMLFile public static int Main(string args) try / Creates an XML file is not exist XmlTextWriter writer = new XmlTextWriter(C:tempxmltest.xml, null); / Starts a new document writer.WriteStartDocument(); /Write comments writer.WriteComment(Commentss: XmlWriter T
36、est 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(M
37、ahesh); 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.; 1
限制150内