亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

C#入門代碼

2019-11-18 16:44:40
字體:
來源:轉載
供稿:網友

     最近很忙,忙著學習,忙著出報紙,忙著寫總結,忙著計劃,忙著考試……
    沒有寫一些學習.NET的心得了,過一些天吧,今天看到一篇文章很不錯,特地轉載了一下

 

一、從控制臺讀取東西代碼片斷:
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 = 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的小例子:
<% @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.;
    }
}

http://www.49028c.com/lyj/archive/2007/01/09/616053.html


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
日韩大陆欧美高清视频区| 成人高清视频观看www| 日韩中文在线中文网在线观看| 久久精品免费电影| 国产日韩在线亚洲字幕中文| 亚洲国产高清福利视频| 欧美日韩精品在线播放| 亚洲欧美国产一区二区三区| 成人欧美一区二区三区在线湿哒哒| 自拍偷拍亚洲一区| 欧美激情中文字幕乱码免费| 中文日韩在线观看| 色狠狠av一区二区三区香蕉蜜桃| 日韩电影免费在线观看| 国产精品吹潮在线观看| 91精品国产91| 亚洲乱亚洲乱妇无码| 国产激情久久久| 欧美日本中文字幕| 成人激情免费在线| 欧美成人黄色小视频| 国产一区二区三区久久精品| 久久久精品国产网站| 欧美极品少妇全裸体| 26uuu久久噜噜噜噜| 久久久久久久久中文字幕| 亚洲在线第一页| 久久久久久久久久亚洲| 亚洲免费电影一区| 中文字幕久久亚洲| 国产成人精品av| 欧美性xxxxx极品| 欧美性xxxx18| 亚洲欧美制服丝袜| 欧洲精品在线视频| 97香蕉超级碰碰久久免费软件| 欧美日韩中文在线观看| 欧美理论电影在线播放| 国产成人综合一区二区三区| 伊人伊成久久人综合网站| 精品中文字幕视频| 欧美国产精品日韩| 亚洲a∨日韩av高清在线观看| 一区二区在线视频| 久久久国产91| 深夜福利亚洲导航| 国产精品爱啪在线线免费观看| 国产精品视频一区二区高潮| 国产一区二区三区三区在线观看| 欧美性一区二区三区| 国产成人在线亚洲欧美| 成人国产精品色哟哟| 久久成人在线视频| 18性欧美xxxⅹ性满足| 热99在线视频| 国产欧美最新羞羞视频在线观看| 国产精品美女免费视频| 成人精品网站在线观看| 国产精品久久久亚洲| 国产亚洲欧美日韩精品| 欧美日本亚洲视频| 中文字幕亚洲精品| 精品国产91久久久| 午夜剧场成人观在线视频免费观看| 亚洲free性xxxx护士hd| 国产成人久久久| 91理论片午午论夜理片久久| 国模gogo一区二区大胆私拍| 91精品国产乱码久久久久久蜜臀| 欧美国产日韩二区| 色婷婷久久av| 欧美第一黄色网| 少妇久久久久久| 国产精品网红福利| 亚洲欧美三级伦理| 欧美日韩在线视频观看| 亚洲影影院av| 在线看欧美日韩| 欧美一区第一页| 国产成人精品久久亚洲高清不卡| 一区二区三区久久精品| 九九九热精品免费视频观看网站| 国产午夜精品全部视频在线播放| 国产精品久久久久久久一区探花| 77777少妇光屁股久久一区| 日韩大陆欧美高清视频区| 亚洲精品一区中文字幕乱码| 久久久爽爽爽美女图片| 亚洲国产精品人人爽夜夜爽| 国产欧美久久久久久| 91精品91久久久久久| 国产精品2018| 欧美性一区二区三区| 日韩av电影在线免费播放| 精品久久久久久久久久久久久久| 欧美在线亚洲一区| 亚洲人成绝费网站色www| 久久伊人精品天天| 欧美大片在线看免费观看| 欧美激情免费在线| 欧美怡红院视频一区二区三区| 亚洲在线视频福利| 欧美激情一区二区三区在线视频观看| 中文字幕欧美国内| 在线观看国产欧美| 亚洲国产精品va在线看黑人| 日韩欧美综合在线视频| 亚洲男人天堂2019| 成人久久精品视频| 欧美国产日本高清在线| 九九热精品视频国产| 日韩大片在线观看视频| 亚洲国产精品久久久久秋霞不卡| 中文字幕成人在线| 亚洲福利影片在线| 亚洲美女性生活视频| 欧美日韩国产999| 日韩精品欧美激情| 亚洲aaaaaa| 一本色道久久综合狠狠躁篇的优点| 色爱av美腿丝袜综合粉嫩av| 欧美极品美女电影一区| 国产精品看片资源| 国产精品入口免费视频一| 日本免费在线精品| 日韩欧美一区二区三区| 热99在线视频| 欧美理论电影网| 国产精品一区二区久久久| 亚洲国产精品999| 不用播放器成人网| 国精产品一区一区三区有限在线| 日韩一级黄色av| 日韩av在线免费播放| 精品久久久久久久久国产字幕| 亚洲欧美国内爽妇网| 久久久人成影片一区二区三区观看| 亚洲在线www| 日韩va亚洲va欧洲va国产| 日韩电影免费观看中文字幕| 国产精品久久精品| 亚洲男人av电影| 国产人妖伪娘一区91| 日韩高清电影免费观看完整版| 91精品在线观| 992tv成人免费影院| 18久久久久久| 欧美在线一区二区视频| 欧洲美女免费图片一区| 超碰91人人草人人干| 日韩毛片在线看| 国产精品丝袜久久久久久不卡| 日本精品性网站在线观看| 国产欧美一区二区三区在线| 欧美色道久久88综合亚洲精品| 色婷婷成人综合| 亚洲老头老太hd| 日韩高清欧美高清| 久久人人爽人人| 久久久精品一区| 日韩av电影国产| 8050国产精品久久久久久| 国产69精品99久久久久久宅男| 日本中文字幕成人|