每天知道多一点(二)

  决定将一些简单的程序放在这里.可能很简单,但是要备忘.是今后学习的参考.主要还是积累.
  1:一段简单的控制台程序
 static void Main(string[] args)
        
{
            
bool b = SearchString();
            Console.WriteLine(
"The result is "+b);
        }

        
public static bool SearchString()
        
{
            
string FileContent;
            
//打开文件
            System.IO.FileStream mystream = new System.IO.FileStream("D:\\test.txt", System.IO.FileMode.Open);
            System.IO.StreamReader mystreamReader 
= new System.IO.StreamReader(mystream);
            
//读取文件

            FileContent 
= mystreamReader.ReadToEnd();
            
//查询特定的词汇
            int ind = FileContent.IndexOf("work");
            
if (ind>0)
                
return true;
            
else
                
return false;
            
//关闭
            mystreamReader.Close();

2:一段简单的数据库操作
 private void button1_Click(object sender, EventArgs e)
        
{
            openFileDialog1.InitialDirectory 
= @"C:\Program files\";
            openFileDialog1.Filter 
= "mdb files(*.mdb)|*.mdb|All files(*.*)|*.*";
            openFileDialog1.FileName 
= "";
            openFileDialog1.FilterIndex 
= 2;
            openFileDialog1.RestoreDirectory 
= true;

            
if (openFileDialog1.ShowDialog() == DialogResult.OK)
            
{
                textBox1.Text 
= openFileDialog1.FileName;
            }

        }


        
private void button2_Click(object sender, EventArgs e)
        
{
            
//OleDbConnection conn = new OleDbConnection();
            
//string str = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + textBox1.Text + "; " + "User ID=admin;Jet OLEDB:database Password=gongdiansuo;Persist Security Info=true";
            
//OleDbConnection conn = new OleDbConnection(str);
            
//string sql = "UPDATE a30 SET 表号 = 出厂编号;";
            
//OleDbCommand cmd = new OleDbCommand(sql,conn);
            
//conn.Open();
            if (textBox1.Text == "")
            
{
                MessageBox.Show(
"请选择数据库所在的位置""info");
            }

            
else
            
{
                
string str = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + textBox1.Text + "" + "User ID=admin;Jet OLEDB:database Password=gongdiansuo;Persist Security Info=true";
                OleDbConnection conn 
= new OleDbConnection(str);
                
string sql = "UPDATE a30 SET 表号 = 出厂编号;";
                OleDbCommand cmd 
= new OleDbCommand(sql, conn);
                
//conn.Open();
                if (checkBox1.Checked)
                
{
                    
try
                    
{
                        conn.Open();
                        cmd.ExecuteNonQuery();
                    }

                    
catch (Exception ex)
                    
{
                        MessageBox.Show(ex.Message);
                    }

                    
finally
                    
{
                        conn.Close();
                        MessageBox.Show(
"update successfully""info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }

                 
if(checkBox2.Checked)
                
{
                   
                    
string insert = "UPDATE A30 SET A30.[日光灯(kW)] = @light, A30.[日光灯(数量)] = @lightnum, A30.[电视机(kW)] = @tv, A30.[电视机(数量)] = @tvnum;";
                    OleDbCommand cmd1 
= new OleDbCommand(insert, conn);
                    cmd1.Parameters.AddWithValue(
"@light", textBox2.Text);
                    cmd1.Parameters.AddWithValue(
"@lightnum",textBox3.Text);
                    cmd1.Parameters.AddWithValue(
"@tv",textBox4.Text);
                    cmd1.Parameters.AddWithValue(
"@tvnum",textBox5.Text);

                    
try
                    
{
                        conn.Open();
                        cmd1.ExecuteNonQuery();
                    }

                    
catch (Exception ex)
                    
{
                        MessageBox.Show(ex.Message);
                    }

                    
finally
                    
{
                        conn.Close();
                        MessageBox.Show(
"insert successfully""info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }


                }

                
else
                
{
                    MessageBox.Show(
"未选择任何操作""info", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

            }

            

        }


        
private void button3_Click(object sender, EventArgs e)
        
{
            
this.Close();
        }


        
private void checkBox2_CheckedChanged(object sender, EventArgs e)
        
{
            
if (textBox2.Enabled == false)
                textBox2.Enabled 
= true;
            
else
                textBox2.Enabled 
= false;

            
if (textBox3.Enabled == false)
                textBox3.Enabled 
= true;
            
else
                textBox3.Enabled 
= false;

            
if (textBox4.Enabled == false)
                textBox4.Enabled 
= true;
            
else
                textBox4.Enabled 
= false;

            
if (textBox5.Enabled == false)
                textBox5.Enabled 
= true;
            
else
                textBox5.Enabled 
= false;
            
        }
posted @ 2008-06-17 20:29  L.Net  阅读(230)  评论(0编辑  收藏  举报