.Net连接Oracle数据库

 --User类 用户操作--

 

 

  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using System.Data;
  5using System.Data.OracleClient;
  6using System.Configuration;
  7using System.Collections;
  8using System.Web;
  9using System.Web.Security;
 10using System.Web.UI;
 11using System.Web.UI.WebControls;
 12using System.Web.UI.WebControls.WebParts;
 13using System.Web.UI.HtmlControls;
 14
 15namespace Users
 16{
 17    public class Class1
 18    {
 19        OracleConnection conn = new OracleConnection(Dbconn.conn.ConnectString);
 20        DataSet ds = new DataSet();
 21        OracleDataAdapter da;
 22        public string error;
 23
 24        //取得所有用户
 25        public DataSet getUsers()
 26        {
 27            OracleCommand cmd = new OracleCommand(Dbconn.conn.sql_getUser, conn);
 28            da = new OracleDataAdapter(cmd);
 29            da.Fill(ds,"usertrueinfo_table");
 30            return ds;
 31        }

 32      //取得用户信息
 33        public DataSet getUserInfo(string name)
 34        {
 35          Users.Class1 uu=new Class1();
 36          int id=uu.getUserID(name);
 37       string sql="select *  from usertrueinfo_table where user_id ='"+id+"'";
 38         OracleCommand cmd = new OracleCommand(sql, conn);
 39          da = new OracleDataAdapter(cmd);
 40          da.Fill(ds, "usertrueinfo_table");
 41          return ds;
 42         }

 43        //得到用户ID
 44        public int  getUserID(string nama)
 45        {
 46            string sql = "select * from user_table where user_name='"+nama+"'";
 47            OracleCommand cmd = new OracleCommand(sql,conn);
 48            conn.Open(); ;
 49           int id= Convert.ToInt32(cmd.ExecuteScalar());
 50            conn.Close();
 51            return id;
 52        }

 53
 54        //执行SQL语句
 55        public void ExecuteSql(string sql)
 56        {
 57            OracleCommand cmd = new OracleCommand(sql, conn);
 58            conn.Open();
 59            cmd.ExecuteNonQuery();
 60            conn.Close();
 61        }

 62        //检验登录
 63        public bool CheckLogin(string name, string pwd)
 64        {
 65            try
 66            {
 67                OracleCommand cmd = new OracleCommand("select count(*) from user_table where USER_NAME='" + name + "' and PASSWORD='" + pwd + "'", conn);
 68                conn.Open();
 69                //  OracleDataReader odr = cmd.ExecuteReader();
 70                int count = -1;
 71                count = Convert.ToInt32(cmd.ExecuteScalar());
 72                if (count < 1)
 73                    return false;
 74
 75                return true;
 76            }

 77            catch (Exception ex)
 78            {
 79                error = ex.ToString();
 80                return false;
 81            }

 82            finally
 83            {
 84                conn.Close();
 85
 86            }

 87
 88        }

 89        //检验用户是否可以注册
 90        public bool checkreg(string username)
 91        {
 92            int flag = 1bool f;
 93            string sql = "select user_name from user_table";
 94            OracleCommand cmd = new OracleCommand(sql,conn);
 95            da = new OracleDataAdapter(cmd);
 96           if (cmd.Connection.State == System.Data.ConnectionState.Closed)
 97                cmd.Connection.Open();
 98            da.Fill(ds);
 99            DataTable dt = ds.Tables[0];
100            foreach (DataRow row in dt.Rows)
101            {
102                flag++;
103                if (username == row["user_name"].ToString())
104                    break;
105            }

106            if (flag > dt.Rows.Count)
107                f = true;
108            else
109                f = false;
110            return f;
111    }

112        
113
114        
115        //注册
116             
117    public void register(string name, string pwd,int flag)
118        {
119             string sql = "insert into  user_table(USER_NAME,PASSWORD,role_flag) values('" + name + "','" + pwd + "','"+flag+"')";
120              OracleCommand cmd = new OracleCommand(sql,conn);
121              if (cmd.Connection.State == System.Data.ConnectionState.Closed)
122                  cmd.Connection.Open();
123              cmd.ExecuteNonQuery();
124              conn.Close();     
125        Users.Class1 user = new Class1();
126             user.InsertUserTrueInfo(name);
127        
128        }

129        //注册时 插入usertrueinfo
130        public void InsertUserTrueInfo(string name)
131        {
132            Users.Class1 uu = new Class1();
133            int id=uu.getUserID(name);
134            string sql = "insert into usertrueinfo_table(user_id) values('"+id+"') ";
135            OracleCommand cmd = new OracleCommand(sql,conn);
136            conn.Open();
137            cmd.ExecuteNonQuery();
138            conn.Close();
139
140        }

141
142        //权限  
143        public string    user_role(string name, string pwd)
144        {   string sql= "select role_flag from user_table where user_name='"+name+"' and password='"+pwd+"'";
145            OracleCommand cmd = new OracleCommand(sql, conn);
146           // da = new OracleDataAdapter(cmd);
147          
148           //DataTable dt=ds.Tables[0];
149           //string  flag =dt.Rows[0]["role_flag"].ToString();
150            conn.Open();
151           string flag = cmd.ExecuteScalar().ToString();
152       ;
153         conn.Close();
154         return flag;
155     
156        }

157        
158        //修改用户
159        public void ModifyUser(string na,string r_name,string tel,string add)
160        {
161            try
162            {  Users.Class1 u = new Class1();
163                //string sqlID = String.Format("select id from user_table where user_name='{0}'", na);
164                //int ID1;
165                //string ID2=SelectDataToParameter(sqlID, "id");
166                //ID1 = Convert.ToInt32(ID2);
167                 int id = u.getUserID(na);
168                  string sql = "update usertrueinfo_table set realname='" + r_name + "',user_tel='" + tel + "',user_add='" + add + "' where user_id='" + id + "'";
169              
170                 OracleCommand cmd = new OracleCommand(sql, conn);
171                conn.Open();
172                cmd.ExecuteNonQuery();
173                conn.Close();
174            }

175
176            catch {}
177        
178        }

179        public void ModifyUser(string na, string pwd)
180        {
181            Users.Class1 u1 = new Class1();
182            string sql1 = "update user_table set password='"+pwd+"'where user_name='"+na+"'";
183            OracleCommand c = new OracleCommand(sql1, conn);
184            conn.Open();
185            c.ExecuteNonQuery();
186            conn.Close();
187              
188        }

189
190        //删除用户
191        public void DeleteUser(string id)
192        string sql="delete from user_table where id='"+id+"'";
193          OracleCommand cmd=new OracleCommand(sql,conn);
194         conn.Open();
195         cmd.ExecuteNonQuery();
196       
197         Users.Class1 u = new Class1();
198         u.DeleteUserPaper(id);
199         u.DeleteUserTrueInfo(id);
200         conn.Close();
201        }

202        //删除用户真实信息表
203        public void DeleteUserTrueInfo(string id)
204        {
205            string sql = "delete from usertrueinfo_table where USER_ID='" + id + "'";
206            OracleCommand cmd = new OracleCommand(sql, conn);
207            conn.Open();
208            cmd.ExecuteNonQuery();
209            conn.Close();
210        }

211//删除用户所有论文
212        public void DeleteUserPaper(string id)
213        {
214            string sql = "delete from paper_table where FIRSTAUTHOR_ID='" + id + "'";
215            OracleCommand cmd = new OracleCommand(sql, conn);
216            conn.Open();
217            cmd.ExecuteNonQuery();
218            conn.Close();
219        }

220
221
222    }

223}

224

posted on 2008-07-26 23:54    阅读(444)  评论(0编辑  收藏  举报

导航