信息系统项目管理师_2024年软考学习应考交流_信息系统项目管理师考试

 找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 2443|回复: 3
打印 上一主题 下一主题

[转帖]C#入门代码集

[复制链接]

该用户从未签到

升级  0%

跳转到指定楼层
楼主
发表于 2006-3-29 08:28:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
<p>一、从控制台读取东西代码片断: using System; class TestReadConsole { public static void Main() { Console.Write("Enter your name:"; string strName = Console.ReadLine(); Console.WriteLine(" Hi "+ strName); } } </p><p>二、读文件代码片断: 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(); } } </p><p>三、写文件代码: 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(); } } </p><p>四、拷贝文件: using System; using System.IO; class TestCopyFile { public static void Main() { File.Copy("c:\\temp\\source.txt", "C:\\temp\\dest.txt" ; } } </p><p>五、移动文件: 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); } } </p><p>七、调用外部程序: class Test { static void Main(string[] args) { System.Diagnostics.Process.Start("notepad.exe"; } } ADO.NET方面的: </p><p>八、连接Access数据库: using System; using System.Data; using System.Data.OleDb; class TestADO { static void Main(string[] args) { string strDSN = "rovider=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(); } } } </p><p>九、连接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 --&gt;&gt; {0}",e); } } } </p><p>十、从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(); } }</p>
[此贴子已经被翔羽于2006-3-29 8:32:50编辑过]

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 顶 踩

该用户从未签到

升级  0%

沙发
 楼主| 发表于 2006-3-29 08:30:07 | 只看该作者
<p>晕了 发上来怎么是这样的啊</p><p></p>

该用户从未签到

升级  30.8%

藤椅
发表于 2006-3-29 08:32:05 | 只看该作者
先放到文本编辑器里面,然后从文本编辑器里面重新复制出来。这个是由于文本被处理过造成的。

该用户从未签到

升级  0%

板凳
发表于 2006-4-16 21:11:48 | 只看该作者
呵呵,看不明白
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

小黑屋|手机版|Archiver|信息系统项目管理师_软考交流平台. ( 鄂ICP备11002878号-1  公安备案号:42011102001150

GMT+8, 2025-7-5 23:15

Software by Discuz! X3.2

© 2001-2013 SKIN BY DSVUE

快速回复 返回顶部 返回列表