C#中使用Oracle

 1:使用存储过程

            string mConn = "Data Source=orcl;User Id=dzzj;Password=dzzj;";  //连接数据库
            conn = new OracleConnection(mConn);
            try
            {
                conn.Open();
                cmd = new OracleCommand("sp_file_zh",conn); 
                cmd.CommandType = CommandType.StoredProcedure;  //调用的是存储过程
                OracleParameter[] parameters ={
                                   new OracleParameter("msg",OracleType.NVarChar,20),
                                  new OracleParameter("c",OracleType.Int32),
                                   new OracleParameter("ref_cursor",OracleType.Cursor)
                                                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Direction = ParameterDirection.Output;
                parameters[2].Direction = ParameterDirection.Output;
                foreach (OracleParameter parameter in parameters)
                {
                    cmd.Parameters.Add(parameter);
                }
                OracleDataAdapter orda = new OracleDataAdapter(cmd);
                DataSet ds = new DataSet();
                orda.Fill(ds);
                return ds;  //返回结果集
            }
            catch (Exception e)
            {
                throw e;
            }

2:使用普通语句

        private string Login(string uName, string pwd)
        {
            string mark = "";
            string sqlstr = "select * from usr where loginName=:lName and pwd=:lPwd";
            OracleConnection orclCon = new OracleConnection(OrclConnection);
            OracleCommand Cmd = new OracleCommand(sqlstr, orclCon);
            Cmd.Parameters.AddWithValue(":lName", uName);
            Cmd.Parameters.AddWithValue(":lPwd", pwd);           
            orclCon.Open();
            OracleDataReader reader = Cmd.ExecuteReader();
            if (reader.Read())
            {
                HttpContext.Current.Session["UserName"] = uName;
                mark = "success";
            }
            else
            {
                mark = "fail";
            }
            return mark;
        }

posted @ 2012-09-03 17:39  jsping68  阅读(4295)  评论(0编辑  收藏  举报