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

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

C#入門代碼

轉自:http://www.cnblogs.com/lyj/archive/2007/01/09/616053.html

一、從控制臺讀取東西代碼片斷:

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.;
    }
}

posted on 2008-06-18 12:08 夢在天涯 閱讀(6931) 評論(4)  編輯 收藏 引用 所屬分類: C#/.NET

評論

# re: C#入門代碼 2008-07-17 22:12 honeymorning

很好,很強大。  回復  更多評論   

# re: C#入門代碼 2008-10-28 23:14 JimmyQi

值得學習。如果每種里面有更多的例子和更多的解釋就更好了。JimmyQI  回復  更多評論   

# re: C#入門代碼 2011-05-06 15:44 re: C#入門代碼

有注解就更好了!  回復  更多評論   

# re: C#入門代碼 2012-06-29 15:08 窩窩

假期輕松在家做兼職,一單一結,80/小時
職位要求:
1.有上網條件,兼職/專職均可,在家上網兼職
2.每天需保證2-3小時的上網時間
3.有簡單網絡知識和網購網站應用的基礎如:下載都不會勿擾
4.操作網購任務,一單只需要花費你5-15分鐘的時間。
一個任務酬勞為5元-40元不等,操作完一單 即刻發放薪酬,
絕無拖欠工資! 操作簡單易懂,只要你在網購購過物的朋友
都能輕易學會,并且熟練操作,加快您的操作速度,您的薪資
就會相應的提升,想兼職,又不想影響正職朋友。
淘寶客服QQ: 1163466921 陳小姐  回復  更多評論   

公告

EMail:itech001#126.com

導航

統計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1814983
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              99精品视频免费在线观看| 在线中文字幕不卡| 可以看av的网站久久看| 亚洲欧美国内爽妇网| 午夜免费久久久久| 欧美一区二区日韩| 久久www成人_看片免费不卡| 久久国产精品一区二区三区| 午夜综合激情| 久久久777| 欧美激情bt| 国产精品久久77777| 国产欧美日韩91| 国产精品久久一区二区三区| 亚洲理伦在线| 亚洲一卡久久| 欧美一级播放| 欧美电影专区| 国产美女一区| 亚洲欧洲一区| 午夜精品久久久久| 欧美成人按摩| 亚洲午夜一级| 久久天天躁狠狠躁夜夜av| 欧美精品日韩| 国产亚洲毛片| 夜夜嗨一区二区| 久久精品在线观看| 亚洲乱码国产乱码精品精98午夜| 亚洲欧美日韩区| 欧美精品www在线观看| 国产欧美日韩亚洲精品| 亚洲三级网站| 久久一二三国产| 一区二区欧美在线| 美日韩精品免费| 国产视频一区免费看| 一二三区精品| 欧美黄色aa电影| 午夜精品一区二区三区在线视| 欧美大片在线看免费观看| 国产日韩欧美在线播放| 一本色道久久综合亚洲精品高清 | 久久视频精品在线| 欧美午夜不卡影院在线观看完整版免费| 国产又爽又黄的激情精品视频 | 久久精品91| 一区二区黄色| 欧美精品在线一区二区| 亚洲国产高清自拍| 久久精品一级爱片| 亚洲永久在线| 国产精品久久久久久久久| 亚洲精品免费在线| 免费成人黄色av| 久久婷婷丁香| 亚洲成色精品| 久久久久久尹人网香蕉| 午夜一区二区三区不卡视频| 欧美天天在线| 亚洲欧美日韩精品在线| 在线亚洲+欧美+日本专区| 欧美日韩免费高清| 中文日韩在线视频| 一区二区欧美在线观看| 国产精品成人免费精品自在线观看| 91久久精品美女| 久久久91精品国产一区二区精品| 亚洲国产精品va在线看黑人| 欧美一区二区在线看| 国产精品高潮呻吟久久av无限| 一本色道久久综合精品竹菊| 99re6热只有精品免费观看 | 中文av字幕一区| 国产精品自在线| 久久久精品2019中文字幕神马| 午夜精品久久久久久久99黑人| 国产亚洲激情视频在线| 老司机精品福利视频| 美女国内精品自产拍在线播放| 亚洲黄色免费| 亚洲三级色网| 国产欧美一区二区精品秋霞影院| 久久精品中文字幕一区二区三区| 久久久午夜电影| 99精品国产99久久久久久福利| 99国产精品久久久| 国模精品娜娜一二三区| 亚洲激情视频在线播放| 国产精品日韩精品欧美精品| 美女黄色成人网| 欧美日韩在线直播| 老司机午夜免费精品视频| 欧美激情bt| 久久亚洲不卡| 国产精品日韩一区| 亚洲福利久久| 国产小视频国产精品| 亚洲国产mv| 国产亚洲精品7777| 亚洲免费观看在线观看| 国产中文一区| 一区二区高清在线| 亚洲国产成人av好男人在线观看| 99视频精品免费观看| 在线日本成人| 亚洲一区二区三区精品动漫| 亚洲人精品午夜| 久久精品导航| 欧美一区二区三区在线视频| 欧美激情1区2区3区| 久久婷婷综合激情| 国产精品99一区| 亚洲激情在线| 亚洲国产一二三| 久久另类ts人妖一区二区| 羞羞漫画18久久大片| 欧美日韩亚洲一区二区| 欧美福利在线| 亚洲第一主播视频| 久久爱另类一区二区小说| 午夜精品久久| 欧美网站在线| 99热精品在线| 中文av字幕一区| 欧美日韩1区2区| 亚洲精品一二| 一本色道久久综合亚洲精品不卡 | 国产亚洲在线观看| 一个色综合av| 一区二区三区视频在线看| 免费看亚洲片| 欧美激情乱人伦| 在线观看视频一区二区| 久久精品99国产精品日本| 久久精品视频va| 国产一区二区三区观看| 亚洲欧美日韩国产综合精品二区| 亚洲性色视频| 国产精品一级久久久| 亚洲欧美日韩直播| 久久精品官网| 1024精品一区二区三区| 久久综合久久综合久久| 亚洲成色www久久网站| 亚洲九九精品| 欧美日韩国产一级| 一本色道88久久加勒比精品| 亚洲在线免费视频| 国产精品丝袜91| 久久精品成人一区二区三区 | 亚洲先锋成人| 国产精品一区毛片| 欧美制服丝袜第一页| 另类天堂av| 99人久久精品视频最新地址| 欧美网站在线| 久久国产精品72免费观看| 欧美777四色影视在线| 亚洲精品九九| 国产精品国产三级国产| 欧美伊人久久| 亚洲国产精品电影| 亚洲综合色丁香婷婷六月图片| 国产精品一级久久久| 久久青草欧美一区二区三区| 亚洲电影天堂av| 亚洲摸下面视频| 亚洲二区在线观看| 欧美日韩一区二区三区免费| 亚洲综合色激情五月| 农村妇女精品| 亚洲欧美一区二区视频| 极品少妇一区二区| 欧美片在线播放| 久久国产一二区| 日韩视频免费观看高清在线视频| 亚洲欧美另类中文字幕| 在线观看一区二区视频| 欧美日韩美女在线| 久久久青草青青国产亚洲免观| 亚洲日本成人| 久久深夜福利| 欧美精品日韩| 欧美一区二区三区播放老司机| 欧美激情中文不卡| 小黄鸭精品aⅴ导航网站入口| 亚洲国产1区| 国产一区二区三区四区hd| 欧美日韩国产大片| 久久在线精品| 香港成人在线视频| 一本色道久久综合狠狠躁篇的优点 | 久久久久这里只有精品| 亚洲日本电影| 黄色成人91| 国产欧美精品在线| 欧美日韩一区二区国产| 欧美aⅴ99久久黑人专区| 久久福利视频导航|