资源描述:
《C#程序经典实例》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、1、已知有Toys玩具表(1)利用SqlConnection,SqlCommand,SqlDataReader等输出整张表的数据publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}OleDbConnectionAconnStr;privatevoidForm1_Load(objectsender,EventArgse){}privatevoidbutton1_Click(objectsender,EventArgse){stringAfile="Provider=Microsoft.Jet.OLEDB.
2、4.0;DataSource=Toys.accdb";AconnStr=newOleDbConnection(Afile);MessageBox.Show("连接成功!");}privatevoidbutton2_Click(objectsender,EventArgse){AconnStr.Open();OleDbCommandAcmd=newOleDbCommand("selecttop5*fromToysorderbyToyId;",AconnStr);OleDbDataReaderodr=null;try{odr=Acmd.ExecuteReader();}catch(Ex
3、ceptionex){if(ex!=null)MessageBox.Show("执行出错!");}if(odr!=null){listBox1.Items.Add("ToyIdtToyNametToyRate");while(odr.Read()){stringTotalInfo="";TotalInfo+=odr["ToyId"].ToString()+"t";TotalInfo+=odr["ToyName"].ToString()+"";TotalInfo+=odr["ToyRate"].ToString()+"";listBox1.Items.Add(Total
4、Info+"");}odr.Close();}AconnStr.Close();}}(2)利用SqlConnection,SqlAdapter,DataSet,DataTable,dataGridView等输出价格在10元以下的玩具信息。2、编程实现:一列数的规则如下:1、1、2、3、5、8、13、21、34......求第30位数是多少,用递归算法实现。classProgram{publicstaticintF(inti){if(i<=0)return0;elseif(i>0&&i<=2)return1;elsereturnF(i-1)+F(i-2);}staticvoidM
5、ain(string[]args){Console.WriteLine(F(30));Console.ReadKey();}}3、编程实现对复数操作符的重载。publicclassComplex{publicintreal;publicintimaginary;publicComplex(intreal,intimaginary){this.real=real;this.imaginary=imaginary;}publicstaticComplexoperator+(Complexc1,Complexc2){returnnewComplex(c1.real+c2.real,c1.
6、imaginary+c2.imaginary);}publicstaticComplexoperator-(Complexc1,Complexc2){returnnewComplex(c1.real-c2.real,c1.imaginary-c2.imaginary);}publicstaticComplexoperator*(Complexc1,Complexc2){returnnewComplex(c1.real*c2.real-c1.imaginary*c2.imaginary,c1.real*c2.imaginary+c1.imaginary*c2.real);}publi
7、cstaticComplexoperator/(Complexc1,Complexc2){returnnewComplex(-c1.real*c2.real+c1.imaginary*c2.imaginary,-c1.real*c2.imaginary+c1.imaginary*c2.real);}publicoverridestringToString(){return(String.Format("{0}+{1}i",real,imaginary));}}clas