收藏 分享(赏)

C#入门代码csharp.doc

上传人:HR专家 文档编号:11559932 上传时间:2020-06-24 格式:DOC 页数:16 大小:79KB
下载 相关 举报
C#入门代码csharp.doc_第1页
第1页 / 共16页
C#入门代码csharp.doc_第2页
第2页 / 共16页
C#入门代码csharp.doc_第3页
第3页 / 共16页
C#入门代码csharp.doc_第4页
第4页 / 共16页
C#入门代码csharp.doc_第5页
第5页 / 共16页
点击查看更多>>
资源描述

1、窗体顶端一、 从控制台读取东西代码片断: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

2、 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.ReadLine(); sr.Close(); fs.Close(); 三、写文件代码:using System; using System.IO; publ

3、ic class TestWriteFile public static void Main(String args) / Create a text file C:temptest.txt FileStream fs = new FileStream(c:temptest.txt , FileMode.OpenOrCreate, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); / Write to the file using StreamWriter class sw.BaseStream.Seek(0, SeekOri

4、gin.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;class TestMoveFile public static void Main() File.Move(c:te

5、mpabc.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.Enabled = true; while ( Console.Read() != q ) /- public

6、static void DisplayTimeEvent( object source, ElapsedEventArgs e ) Console.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;using System.Data.OleDb;class TestADO static voi

7、d 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

8、.ExecuteReader(); while (reader.Read() ) Console.WriteLine(First Name:0, Last Name:1, readerFirstName, readerLastName); 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() Sq

9、lConnection 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

10、, 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() St

11、ring 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 Da

12、taSet 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; usin

13、g 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

14、 OleDbConnection(strDSN); OleDbCommand cmd = new OleDbCommand( strSQL, conn ); try conn.Open(); cmd.ExecuteNonQuery(); catch (Exception e) Console.WriteLine(Oooops. I did it again:n0, e.Message); finally conn.Close(); 十二、使用OLEConn连接数据库:using System;using System.Data; using System.Data.OleDb; class T

15、estADO 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

16、(); cmd.Fill( ds, employee ); DataTable dt = ds.Tables0; foreach( DataRow dr in dt.Rows ) Console.WriteLine(First name: + drFirstName.ToString() + Last name: + drLastName.ToString(); conn.Close(); 十三、读取表的属性:using System;using System.Data; using System.Data.OleDb; class TestADO static void Main(strin

17、g 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 );

18、 DataTable dt = ds.Tables0; 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

19、程序: void Button1_Click(Object sender, EventArgs e) Label1.Text=TextBox1.Text; Enter your name: 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.Form

20、s.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) component

21、s.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 = Form1; this.button1 = new System.Windows.Forms.Button(); this.textBox1 =

22、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.Loca

23、tion = 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(29

24、2, 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 formSystem.Drawing.Icon ico =

25、 new System.Drawing.Icon(c:tempapp.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

26、(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 (

27、); 十九、取得机器名称: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

28、 = new MailMessage(); message.From = ; message.To = ; 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 mes

29、sage 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

30、(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

31、 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(

32、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.F

33、orms.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() XmlTextRe

34、ader 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(Bas

35、e 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(Attr

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 网络科技 > 计算机原理

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报