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

 找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 2282|回复: 6
打印 上一主题 下一主题

[分享]C#入门代码集(2)

[复制链接]

该用户从未签到

升级  0%

跳转到指定楼层
楼主
发表于 2006-3-29 08:32:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
<p>十一、用ADO添加数据到数据库中: <br/>using System; <br/>using System.Data;&nbsp; <br/>using System.Data.OleDb;&nbsp; </p><p>class TestADO <br/>{&nbsp; <br/>static void Main(string[] args)&nbsp; <br/>{&nbsp; <br/>string strDSN = "rovider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.mdb";&nbsp; <br/>string strSQL = "INSERT INTO Employee(FirstName, LastName) valueS('FirstName', 'LastName')" ;&nbsp; </p><p>// create Objects of ADOConnection and ADOCommand&nbsp; </p><p>OleDbConnection conn = new OleDbConnection(strDSN);&nbsp; <br/>OleDbCommand cmd = new OleDbCommand( strSQL, conn ;&nbsp; <br/>try&nbsp; <br/>{&nbsp; <br/>conn.Open();&nbsp; <br/>cmd.ExecuteNonQuery();&nbsp; <br/>}&nbsp; <br/>catch (Exception e)&nbsp; <br/>{&nbsp; <br/>Console.WriteLine("Oooops. I did it again:\n{0}", e.Message);&nbsp; <br/>}&nbsp; <br/>finally&nbsp; <br/>{&nbsp; <br/>conn.Close();&nbsp; <br/>}&nbsp; <br/>}&nbsp; <br/>}<br/>&nbsp; <br/>十二、使用OLEConn连接数据库: <br/>using System; <br/>using System.Data;&nbsp; <br/>using System.Data.OleDb;&nbsp; </p><p>class TestADO <br/>{&nbsp; <br/>static void Main(string[] args)&nbsp; <br/>{&nbsp; <br/>string strDSN = "rovider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.mdb";&nbsp; <br/>string strSQL = "SELECT * FROM employee" ;&nbsp; </p><p>OleDbConnection conn = new OleDbConnection(strDSN); <br/>OleDbDataAdapter cmd = new OleDbDataAdapter( strSQL, conn ;&nbsp; </p><p>conn.Open(); <br/>DataSet ds = new DataSet(); <br/>cmd.Fill( ds, "employee" ; <br/>DataTable dt = ds.Tables[0]; </p><p>foreach( DataRow dr in dt.Rows&nbsp; <br/>{ <br/>Console.WriteLine("First name: "+ dr["FirstName"].ToString() + " Last name: "+ dr["LastName"].ToString()); <br/>} <br/>conn.Close();&nbsp; <br/>}&nbsp; <br/>}<br/>&nbsp; <br/>十三、读取表的属性: <br/>using System; <br/>using System.Data;&nbsp; <br/>using System.Data.OleDb;&nbsp; </p><p>class TestADO <br/>{&nbsp; <br/>static void Main(string[] args)&nbsp; <br/>{&nbsp; <br/>string strDSN = "rovider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.mdb";&nbsp; <br/>string strSQL = "SELECT * FROM employee" ;&nbsp; </p><p>OleDbConnection conn = new OleDbConnection(strDSN); <br/>OleDbDataAdapter cmd = new OleDbDataAdapter( strSQL, conn ;&nbsp; </p><p>conn.Open(); <br/>DataSet ds = new DataSet(); <br/>cmd.Fill( ds, "employee" ; <br/>DataTable dt = ds.Tables[0]; </p><p>Console.WriteLine("Field Name DataType Unique AutoIncrement AllowNull"; <br/>Console.WriteLine("=================================================================="; <br/>foreach( DataColumn dc in dt.Columns&nbsp; <br/>{ <br/>Console.WriteLine(dc.ColumnName+" , "+dc.DataType +" ,"+dc.Unique +" ,"+dc.AutoIncrement+" ,"+dc.AllowDBNull ; <br/>} <br/>conn.Close();&nbsp; <br/>}&nbsp; <br/>}&nbsp; </p><p>ASP.NET方面的 <br/>十四、一个ASP.NET程序: </p><p>&lt;<a href="mailto:%@Page">%@Page</a> Language="C#" %&gt;&lt;script runat="server"&gt; void Button1_Click(Objectsender, EventArgs e){Label1.Text=TextBox1.Text;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;form runat="server"&gt; &lt;p&gt; &lt;br /&gt; Enteryour name: &lt;asp:TextBox id="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;/p&gt; &lt;p&gt; &lt;b&gt;&lt;aspabel id="Label1" runat="server" Width="247px"&gt;&lt;/aspabel&gt;&lt;/b&gt; &lt;/p&gt; &lt;p&gt; &lt;asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Submit"&gt;&lt;/asp:Button&gt;&lt;/p&gt; &lt;/form&gt;&nbsp; WinForm开发: <br/>十五、一个简单的WinForm程序: <br/>using System; <br/>using System.Drawing; <br/>using System.Collections; <br/>using System.ComponentModel; <br/>using System.Windows.Forms; <br/>using System.Data; </p><p><br/>public class SimpleForm : System.Windows.Forms.Form <br/>{ </p><p>private System.ComponentModel.Container components = null; <br/>private System.Windows.Forms.Button button1; <br/>private System.Windows.Forms.TextBox textBox1; <br/>public SimpleForm() <br/>{ <br/>InitializeComponent(); <br/>} </p><p>protected override void Dispose( bool disposing&nbsp; <br/>{ <br/>if( disposing&nbsp; <br/>{ <br/>if (components != null) <br/>{ <br/>components.Dispose(); <br/>} <br/>} <br/>base.Dispose( disposing ; <br/>} </p><p>#region Windows Form Designer generated code </p><p>private void InitializeComponent() <br/>{ </p><p>this.components = new System.ComponentModel.Container(); <br/>this.Size = new System.Drawing.Size(300,300); <br/>this.Text = "Form1"; </p><p>this.button1 = new System.Windows.Forms.Button(); <br/>this.textBox1 = new System.Windows.Forms.TextBox(); <br/>this.SuspendLayout();&nbsp; <br/>// </p><p>// button1 </p><p>// </p><p><br/>this.button1.Location = new System.Drawing.Point(8, 16); <br/>this.button1.Name = "button1"; <br/>this.button1.Size = new System.Drawing.Size(80, 24); <br/>this.button1.TabIndex = 0; <br/>this.button1.Text = "button1"; </p><p>// </p><p>// textBox1 </p><p>// </p><p>this.textBox1.Location = new System.Drawing.Point(112, 16); <br/>this.textBox1.Name = "textBox1"; <br/>this.textBox1.Size = new System.Drawing.Size(160, 20); <br/>this.textBox1.TabIndex = 1; <br/>this.textBox1.Text = "textBox1"; <br/>// </p><p>// Form1 </p><p>// </p><p><br/>this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); <br/>this.ClientSize = new System.Drawing.Size(292, 273); <br/>this.Controls.AddRange(new System.Windows.Forms.Control[] { <br/>this.textBox1, <br/>this.button1}); <br/>this.Name = "Form1"; <br/>this.Text = "Form1"; <br/>this.ResumeLayout(false);&nbsp; </p><p>} <br/>#endregion </p><p><br/>[STAThread] <br/>static void Main() <br/>{ <br/>Application.Run(new SimpleForm()); <br/>}&nbsp; <br/>} </p><p>十六、运行时显示自己定义的图标: <br/>//load icon and set to form </p><p>System.Drawing.Icon ico = new System.Drawing.Icon(@"c:\temp\app.ico"; <br/>this.Icon = ico; </p><p>十七、添加组件到ListBox中: <br/>private void Form1_Load(object sender, System.EventArgs e) <br/>{ <br/>string str = "First item"; <br/>int i = 23; <br/>float flt = 34.98f;&nbsp; <br/>listBox1.Items.Add(str); <br/>listBox1.Items.Add(i.ToString()); <br/>listBox1.Items.Add(flt.ToString()); <br/>listBox1.Items.Add("Last Item in the List Box"; <br/>}</p>
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 顶 踩

该用户从未签到

升级  0%

沙发
 楼主| 发表于 2006-3-29 08:33:42 | 只看该作者
<p>网络方面的: <br/>十八、取得IP地址: <br/>using System; <br/>using System.Net; </p><p>class GetIP <br/>{ <br/>public static void Main() <br/>{ <br/>IPHostEntry ipEntry = Dns.GetHostByName ("localhost"; <br/>IPAddress [] IpAddr = ipEntry.AddressList; <br/>for (int i = 0; i &lt; IpAddr.Length; i++) <br/>{&nbsp; <br/>Console.WriteLine ("IP Address {0}: {1} ", i, IpAddr.ToString ()); <br/>} <br/>} <br/>}</p><p>十九、取得机器名称: <br/>using System; <br/>using System.Net; </p><p>class GetIP <br/>{ <br/>public static void Main() <br/>{ <br/>Console.WriteLine ("Host name : {0}", Dns.GetHostName()); <br/>} <br/>} </p><p>二十、发送邮件: <br/>using System; <br/>using System.Web; <br/>using System.Web.Mail; </p><p>public class TestSendMail <br/>{ <br/>public static void Main() <br/>{ <br/>try <br/>{ <br/>// Construct a new mail message&nbsp; </p><p>MailMessage message = new MailMessage(); <br/>message.From = "<a href="mailto:from@domain.com">from@domain.com</a>"; <br/>message.To = "<a href="mailto:pengyun@cobainsoft.com">pengyun@cobainsoft.com</a>"; <br/>message.Cc = ""; <br/>message.Bcc = ""; <br/>message.Subject = "Subject"; <br/>message.Body = "Content of message"; </p><p>//if you want attach file with this mail, add the line below </p><p>message.Attachments.Add(new MailAttachment("c:\\attach.txt", MailEncoding.Base64)); </p><p>// Send the message </p><p>SmtpMail.Send(message);&nbsp; <br/>System.Console.WriteLine("Message has been sent"; <br/>} </p><p>catch(Exception ex) <br/>{ <br/>System.Console.WriteLine(ex.Message.ToString()); <br/>} </p><p>} <br/>}</p><p>二十一、根据IP地址得出机器名称: <br/>using System; <br/>using System.Net; </p><p>class ResolveIP <br/>{ <br/>public static void Main() <br/>{ <br/>IPHostEntry ipEntry = Dns.Resolve("172.29.9.9"; <br/>Console.WriteLine ("Host name : {0}", ipEntry.HostName);&nbsp; <br/>} <br/>}</p><p>GDI+方面的: <br/>二十二、GDI+入门介绍: <br/>using System; <br/>using System.Drawing; <br/>using System.Collections; <br/>using System.ComponentModel; <br/>using System.Windows.Forms; <br/>using System.Data; </p><p>public class Form1 : System.Windows.Forms.Form <br/>{ <br/>private System.ComponentModel.Container components = null; </p><p>public Form1() <br/>{ <br/>InitializeComponent(); <br/>} </p><p>protected override void Dispose( bool disposing&nbsp; <br/>{ <br/>if( disposing&nbsp; <br/>{ <br/>if (components != null)&nbsp; <br/>{ <br/>components.Dispose(); <br/>} <br/>} <br/>base.Dispose( disposing ; <br/>} </p><p>#region Windows Form Designer generated code </p><p>private void InitializeComponent() <br/>{ <br/>this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); <br/>this.ClientSize = new System.Drawing.Size(292, 273); <br/>this.Name = "Form1"; <br/>this.Text = "Form1"; <br/>this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); <br/>} <br/>#endregion </p><p><br/>[STAThread] <br/>static void Main()&nbsp; <br/>{ <br/>Application.Run(new Form1()); <br/>} </p><p>private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) <br/>{ <br/>Graphics g=e.Graphics; <br/>g.DrawLine(new Pen(Color.Blue),10,10,210,110); <br/>g.DrawRectangle(new Pen(Color.Red),10,10,200,100); <br/>g.DrawEllipse(new Pen(Color.Yellow),10,150,200,100); <br/>} <br/>}</p><p>XML方面的: <br/>二十三、读取XML文件: <br/>using System; <br/>using System.Xml;&nbsp; </p><p>class TestReadXML <br/>{ <br/>public static void Main() <br/>{ </p><p>XmlTextReader reader = new XmlTextReader("C:\\test.xml"; <br/>reader.Read(); </p><p>while (reader.Read()) <br/>{&nbsp; <br/>reader.MoveToElement(); <br/>Console.WriteLine("XmlTextReader Properties Test"; <br/>Console.WriteLine("===================";&nbsp; </p><p>// Read this properties of element and display them on console </p><p>Console.WriteLine("Name:" + reader.Name); <br/>Console.WriteLine("Base URI:" + reader.BaseURI); <br/>Console.WriteLine("Local Name:" + reader.LocalName); <br/>Console.WriteLine("Attribute Count:" + reader.AttributeCount.ToString()); <br/>Console.WriteLine("Depth:" + reader.Depth.ToString()); <br/>Console.WriteLine("Line Number:" + reader.LineNumber.ToString()); <br/>Console.WriteLine("Node Type:" + reader.NodeType.ToString()); <br/>Console.WriteLine("Attribute Count:" + reader.value.ToString()); <br/>}&nbsp; <br/>}&nbsp; <br/>} </p><p>二十四、写XML文件: <br/>using System;&nbsp; <br/>using System.Xml;&nbsp; </p><p>public class TestWriteXMLFile&nbsp; <br/>{&nbsp; <br/>public static int Main(string[] args)&nbsp; <br/>{&nbsp; <br/>try&nbsp; <br/>{&nbsp; <br/>// Creates an XML file is not exist&nbsp; </p><p>XmlTextWriter writer = new XmlTextWriter("C:\\temp\\xmltest.xml", null);&nbsp; <br/>// Starts a new document&nbsp; </p><p>writer.WriteStartDocument();&nbsp; <br/>//Write comments&nbsp; </p><p>writer.WriteComment("Commentss: XmlWriter Test Program";&nbsp; <br/>writer.WriteProcessingInstruction("Instruction","erson Record";&nbsp; <br/>// Add elements to the file&nbsp; </p><p>writer.WriteStartElement("p", "person", "urnerson";&nbsp; <br/>writer.WriteStartElement("LastName","";&nbsp; <br/>writer.WriteString("Chand";&nbsp; <br/>writer.WriteEndElement();&nbsp; <br/>writer.WriteStartElement("FirstName","";&nbsp; <br/>writer.WriteString("Mahesh";&nbsp; <br/>writer.WriteEndElement();&nbsp; <br/>writer.WriteElementInt16("age","", 25);&nbsp; <br/>// Ends the document&nbsp; </p><p>writer.WriteEndDocument();&nbsp; <br/>}&nbsp; <br/>catch (Exception e)&nbsp; <br/>{&nbsp; <br/>Console.WriteLine ("Exception: {0}", e.ToString());&nbsp; <br/>}&nbsp; <br/>return 0;&nbsp; <br/>}&nbsp; <br/>}&nbsp; </p>

该用户从未签到

升级  30.8%

藤椅
发表于 2006-3-29 08:33:50 | 只看该作者
这组帖子也非常好,大家可能更需要这样的代码积累。

该用户从未签到

升级  0%

板凳
 楼主| 发表于 2006-3-29 08:36:10 | 只看该作者
为什么发新帖,下面还要4个选项,什么html什么的&nbsp;,选不对,发出来的格式都不一样啊!搞的忒复杂了点了吧!

该用户从未签到

升级  30.8%

报纸
发表于 2006-3-29 08:37:34 | 只看该作者
一般情况下你可以不用管,按照默认的就可以了。

该用户从未签到

升级  0%

地板
发表于 2006-3-31 18:23:52 | 只看该作者
不错的东西,等两天看C SHARP的时候借鉴哈了![em04]

该用户从未签到

升级  0%

7
发表于 2006-4-16 20:13:13 | 只看该作者
谢谢楼主,获益菲浅
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2025-7-5 20:30

Software by Discuz! X3.2

© 2001-2013 SKIN BY DSVUE

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