《C#入门代码,csharp.doc》由会员分享,可在线阅读,更多相关《C#入门代码,csharp.doc(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 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添加数据到数据库中: