C#入门代码csharp.docx

上传人:b****2 文档编号:1129599 上传时间:2023-04-30 格式:DOCX 页数:24 大小:18.77KB
下载 相关 举报
C#入门代码csharp.docx_第1页
第1页 / 共24页
C#入门代码csharp.docx_第2页
第2页 / 共24页
C#入门代码csharp.docx_第3页
第3页 / 共24页
C#入门代码csharp.docx_第4页
第4页 / 共24页
C#入门代码csharp.docx_第5页
第5页 / 共24页
C#入门代码csharp.docx_第6页
第6页 / 共24页
C#入门代码csharp.docx_第7页
第7页 / 共24页
C#入门代码csharp.docx_第8页
第8页 / 共24页
C#入门代码csharp.docx_第9页
第9页 / 共24页
C#入门代码csharp.docx_第10页
第10页 / 共24页
C#入门代码csharp.docx_第11页
第11页 / 共24页
C#入门代码csharp.docx_第12页
第12页 / 共24页
C#入门代码csharp.docx_第13页
第13页 / 共24页
C#入门代码csharp.docx_第14页
第14页 / 共24页
C#入门代码csharp.docx_第15页
第15页 / 共24页
C#入门代码csharp.docx_第16页
第16页 / 共24页
C#入门代码csharp.docx_第17页
第17页 / 共24页
C#入门代码csharp.docx_第18页
第18页 / 共24页
C#入门代码csharp.docx_第19页
第19页 / 共24页
C#入门代码csharp.docx_第20页
第20页 / 共24页
亲,该文档总共24页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

C#入门代码csharp.docx

《C#入门代码csharp.docx》由会员分享,可在线阅读,更多相关《C#入门代码csharp.docx(24页珍藏版)》请在冰点文库上搜索。

C#入门代码csharp.docx

C#入门代码csharp

窗体顶端

一、从控制台读取东西代码片断:

usingSystem;

classTestReadConsole

{

   publicstaticvoidMain()

   {

       Console、Write(Enteryourname:

);

       stringstrName=Console、ReadLine();

       Console、WriteLine(Hi+strName);

   }

}

二、读文件代码片断:

usingSystem;

usingSystem、IO;

publicclassTestReadFile

{

   publicstaticvoidMain(String[]args)

   {

       //ReadtextfileC:

\temp\test、txt

       fs=new(@c:

\temp\test、txt,,);

       StreamReadersr=newStreamReader(fs); 

       

       Stringline=sr、ReadLine();

       while(line!

=null)

       {

           Console、WriteLine(line);

           line=sr、ReadLine();

       }  

       

       sr、Close();

       fs、Close();

   }

}

三、写文件代码:

usingSystem;

usingSystem、IO;

publicclassTestWriteFile

{

   publicstaticvoidMain(String[]args)

   {

       //CreateatextfileC:

\temp\test、txt

       fs=new(@c:

\temp\test、txt,,);

       StreamWritersw=newStreamWriter(fs);

       //WritetotheStreamWriterclass

       sw、BaseStream、Seek(0,SeekOrigin、End);

       sw、WriteLine(FirstLine);

       sw、WriteLine(SecondLine);

       sw、Flush();

   }

}

四、拷贝文件:

usingSystem;

usingSystem、IO;

classTestCopyFile

{

   publicstaticvoidMain()

   {

       (c:

\\temp\\source、txt,C:

\\temp\\dest、txt); 

   }

}

五、移动文件:

usingSystem;

usingSystem、IO;

classTestMoveFile

{

   publicstaticvoidMain()

   {

       (c:

\\temp\\abc、txt,C:

\\temp\\def、txt); 

   }

}

六、使用计时器:

usingSystem;

usingSystem、Timers;

classTestTimer

{

   publicstaticvoidMain()

   {

       Timertimer=newTimer();

       timer、Elapsed+=newElapsedEventHandler(DisplayTimeEvent);

       timer、Interval=1000;

       timer、Start();

       timer、Enabled=true;

       while(Console、Read()!

='q')

       {

            //-------------

       }

   }

   publicstaticvoidDisplayTimeEvent(objectsource,ElapsedEventArgse)

   {

       Console、Write(\r{0},DateTime、Now);

   }

}

七、调用外部程序:

classTest

{

   staticvoidMain(string[]args)

   {

       System、Diagnostics、Process、Start(notepad、exe);

   }

}

ADO、NET方面的:

八、连接Access数据库:

usingSystem;

usingSystem、Data;

usingSystem、Data、OleDb;

classTestADO

{

   staticvoidMain(string[]args)

   {

       stringstrDSN=Provider=Microsoft、Jet、OLEDB、4、0;DataSource=c:

\\test、mdb;

       stringstrSQL=SELECT*FROMemployees;

       OleDbConnectionconn=newOleDbConnection(strDSN);

       OleDbCommandcmd=newOleDbCommand(strSQL,conn);

       OleDbDataReaderreader=null;

       try

       {

           conn、Open();

           reader=cmd、ExecuteReader();

           while(reader、Read())

           {

               Console、WriteLine(FirstName:

{0},LastName:

{1},reader[FirstName],reader[LastName]);

           }

       }

       catch(Exceptione)

       {

           Console、WriteLine(e、Message);

       }

       finally

       {

           conn、Close();

       }

   }

}

九、连接SQLServer数据库:

usingSystem;

usingSystem、Data、SqlClient;

publicclassTestADO

{

   publicstaticvoidMain()

   {

       SqlConnectionconn=newSqlConnection(DataSource=localhost;IntegratedSecurity=SSPI;InitialCatalog=pubs);

       SqlCommand cmd=newSqlCommand(SELECT*FROMemployees,conn);

       try

       {       

           conn、Open();

           SqlDataReaderreader=cmd、ExecuteReader();           

           while(reader、Read())

           {

               Console、WriteLine(FirstName:

{0},LastName:

{1},reader、GetString(0),reader、GetString

(1));

           }

       

           reader、Close();

           conn、Close();

       }

       catch(Exceptione)

       {

           Console、WriteLine(ExceptionOccured-->>{0},e);

       }       

   }

}

十、从SQL内读数据到XML:

usingSystem;

usingSystem、Data;

usingSystem、Xml;

usingSystem、Data、SqlClient;

usingSystem、IO;

publicclassTestWriteXML

{

   publicstaticvoidMain()

   {

       Stringstr;

       SqlConnectionconn=newSqlConnection(server=localhost;uid=sa;pwd=;database=db);

       StringstrSql=SELECTFirstName,LastNameFROMemployees;

       SqlDataAdapteradapter=newSqlDataAdapter();

       adapter、SelectCommand=newSqlCommand(strSql,conn);

       //BuildtheDataSet

       DataSetds=newDataSet();

       adapter、Fill(ds,employees);

       //Getaobject

       fs=new(str);

       //ApplytheWriteXmlmethodtowriteanXMLdocument

       ds、WriteXml(fs);

       fs、Close();

   }

}

十一、用ADO添加数据到数据库中:

usingSystem;

usingSystem、Data;  

usingSystem、Data、OleDb;  

classTestADO

   staticvoidMain(string[]args) 

   { 

       stringstrDSN=Provider=Microsoft、Jet、OLEDB、4、0;DataSource=c:

\test、mdb; 

       stringstrSQL=INSERTINTOEmployee(FirstName,LastName)VALUES('FirstName','LastName'); 

                  

       //createObjectsofADOConnectionandADOCommand  

       OleDbConnectionconn=newOleDbConnection(strDSN); 

       OleDbCommandcmd=newOleDbCommand(strSQL,conn); 

       try 

       { 

           conn、Open(); 

           cmd、ExecuteNonQuery(); 

       } 

       catch(Exceptione) 

       { 

           Console、WriteLine(Oooops、Ididitagain:

\n{0},e、Message); 

       } 

       finally 

       { 

           conn、Close(); 

       }         

   }

十二、使用OLEConn连接数据库:

usingSystem;

usingSystem、Data;  

usingSystem、Data、OleDb;  

classTestADO

   staticvoidMain(string[]args) 

   { 

       stringstrDSN=Provider=Microsoft、Jet、OLEDB、4、0;DataSource=c:

\test、mdb; 

       stringstrSQL=SELECT*FROMemployee; 

       OleDbConnectionconn=newOleDbConnection(strDSN);

       OleDbDataAdaptercmd=newOleDbDataAdapter(strSQL,conn);

       conn、Open();

       DataSetds=newDataSet();

       cmd、Fill(ds,employee);

       DataTabledt=ds、Tables[0];

       foreach(DataRowdrindt、Rows)

       {

           Console、WriteLine(Firstname:

+dr[FirstName]、ToString()+Lastname:

+dr[LastName]、ToString());

       }

       conn、Close(); 

   }

十三、读取表的属性:

usingSystem;

usingSystem、Data;  

usingSystem、Data、OleDb;  

classTestADO

   staticvoidMain(string[]args) 

   { 

       stringstrDSN=Provider=Microsoft、Jet、OLEDB、4、0;DataSource=c:

\test、mdb; 

       stringstrSQL=SELECT*FROMemployee; 

       OleDbConnectionconn=newOleDbConnection(strDSN);

       OleDbDataAdaptercmd=newOleDbDataAdapter(strSQL,conn);

       conn、Open();

       DataSetds=newDataSet();

       cmd、Fill(ds,employee);

       DataTabledt=ds、Tables[0];

       Console、WriteLine(FieldNameDataTypeUniqueAutoIncrementAllowNull);

       Console、WriteLine(==================================================================);

       foreach(DataColumndcindt、Columns)

       {

           Console、WriteLine(dc、ColumnName+,+dc、DataType+,+dc、Unique+,+dc、AutoIncrement+,+dc、AllowDBNull);

       }

       conn、Close(); 

   }

}

ASP、NET方面的

十四、一个ASP、NET程序:

<%@PageLanguage=C#%>

  

   voidButton1_Click(Objectsender,EventArgse)

   {

       Label1、Text=TextBox1、Text;

   }

   

       

           

           Enteryourname:

TextBoxid=TextBox1runat=server>

TextBox>

       

       

           

Labelid=Label1runat=serverWidth=247px>

Label>

       

       

           

Buttonid=Button1onclick=Button1_Clickrunat=serverText=Submit>

Button>

       

   

WinForm开发:

十五、一个简单的WinForm程序:

usingSystem;

usingSystem、Drawing;

usingSystem、Collections;

usingSystem、ComponentModel;

usingSystem、Windows、Forms;

usingSystem、Data;

publicclassSimpleForm:

System、Windows、Forms、Form

{

   privateSystem、ComponentModel、Containercomponents=null;

   privateSystem、Windows、Forms、Buttonbutton1;

   privateSystem、Windows、Forms、TextBoxtextBox1;

   publicSimpleForm()

   {

       InitializeComponent();

   }

   protectedoverridevoidDispose(booldisposing)

   {

       if(disposing)

       {

           if(components!

=null)

           {

               components、Dispose();

           }

       }

       base、Dispose(disposing);

   }

   #regionWindowsFormDesignergeneratedcode

   privatevoidInitializeComponent()

   {

       this、components=newSystem、ComponentModel、Container();

       this、Size=newSystem、Drawing、Size(300,300);

       this、Text=Form1;

       this、button1=newSystem、Windows、Forms、Button();

       this、textBox1=newSystem、Windows、Forms、TextBox();

       this、SuspendLayout();

   //

   //button1

   //

   this、button1、Location=newSystem、Drawing、Point(8,16);

   this、button1、Name=button1;

   this、button1、Size=newSystem、Drawing、Size(80,24);

   this、button1、TabIndex=0;

   this、button1、Text=button1;

   //

   //textBox1

   //

   this、textBox1、Location=newSystem、Drawing、Point(112,16);

   this、textBox1、Name=textBox1;

   this、textBox1、Size=newSystem、Drawing、Size(160,20);

   this、textBox1、TabIndex=1;

   this、textBox1、Text=textBox1;

   //

   //Form1

   //

   this、AutoScaleBaseSize=newSystem、Drawing、Size(5,13);

   this、ClientSize=newSystem、Drawing、Size(292,273);

   this、Controls、AddRange(newSystem、Windows、Forms、Control[]{

   this、textBox1,

   this、button1});

   this、Name=Form1;

   this、Text=Form1;

   this、ResumeLayout(false);

   }

   #endregion

   

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

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2