C#入门代码csharp.docx
- 文档编号:1129599
- 上传时间:2023-04-30
- 格式:DOCX
- 页数:24
- 大小:18.77KB
C#入门代码csharp.docx
《C#入门代码csharp.docx》由会员分享,可在线阅读,更多相关《C#入门代码csharp.docx(24页珍藏版)》请在冰点文库上搜索。
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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 入门 代码 csharp