.net连接oracle数据库

来源:互联网 发布:遗传算法 计算梯度 编辑:程序博客网 时间:2024/04/27 17:53

 学了差不多快一周了.net,想用它来练级oracle数据库,做

//    添加为一个按钮

  private void button1_Click(object sender, EventArgs e)
        {
           //连接字符串,data source 为连接的数据库名称,user 为数据库用户名,password为密码
            String ConnectionString = "Data Source=sundun;user=sundun;password=sundun";
            //建立一个连接
  OracleConnection conn = new OracleConnection(ConnectionString);
            try {
                //打开连接
                conn.Open();
                //创建查询命令
                OracleCommand cmd=conn.CreateCommand();
                cmd.CommandText="select * from usertable where name='"+this.textBox1.Text+"' and password='"+this.textBox2.Text+"'";
               //查询结果,由datareader读出
                OracleDataReader reader=cmd.ExecuteReader();
                if (reader.Read())
                {
                    MessageBox.Show(this,"登陆成功");
                }
                else {
                    MessageBox.Show(this,"用户名或则密码错误");
                }
            }
            catch(Exception EE){
                MessageBox.Show("连接错误"+EE.Message);
            }
            finally{
                conn.Close();
            }

        }
    }