C#连接oracle数据库执行简单的增删改查操作

users表为例,有三个字段,自增长的编号idint类型;名称namenvarchar类型,密码pwdnvarchar类型
首先在vs2005中引入System.Data.OracleClient;命名空间

///<summary>

        ///增加

        ///</summary>

        ///<param name="name">姓名</param>

        ///<param name="pwd">密码</param>

        ///<returns></returns>

        public int Insert(string name, string pwd)

        {

            OracleConnection conn = new OracleConnection(@"Data Source=SBZX;User ID=simis;Password=zeda");//Data Source后面跟你数据库的名字,User ID为用户名,Password为密码

            conn.Open();

            string sql = "insert into users(name,pwd) values(:name,:pwd)";

            OracleCommand cmd = new OracleCommand(sql, conn);

            OracleParameter parn = new OracleParameter(":name", name);

            cmd.Parameters.Add(parn);

            OracleParameter parp = new OracleParameter(":pwd", name);

            cmd.Parameters.Add(parp);

            int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示添加成功

            conn.Close();

            cmd.Dispose();

            return result;         

        }

 

 

        ///<summary>

        ///删除

        ///</summary>

        ///<param name="name">姓名</param>

        ///<param name="pwd">密码</param>

        ///<returns></returns>

        public int Update(int id)

        {

            OracleConnection conn = new OracleConnection(@"Data Source=SBZX;User ID=simis;Password=zeda");//Data Source后面跟你数据库的名字,User ID为用户名,Password为密码

            conn.Open();

            string sql = "delete from users where id=:id";

            OracleCommand cmd = new OracleCommand(sql, conn);

            OracleParameter pari = new OracleParameter(":id", id);

            cmd.Parameters.Add(pari);      

            int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示添加成功

            conn.Close();

            cmd.Dispose();

            return result;          

 

        }

 

 

        ///<summary>

        ///修改

        ///</summary>

        ///<param name="name">姓名</param>

        ///<param name="pwd">密码</param>

        ///<returns></returns>

        public int Insert(string name, string pwd, int id)

        {

            OracleConnection conn = new OracleConnection(@"Data Source=SBZX;User ID=simis;Password=zeda");//Data Source后面跟你数据库的名字,User ID为用户名,Password为密码

            conn.Open();

            string sql = "update users set name=:name,pwd=:pwd where id=:id";

            OracleCommand cmd = new OracleCommand(sql, conn);

            OracleParameter parn = new OracleParameter(":name", name);

            cmd.Parameters.Add(parn);

            OracleParameter parp = new OracleParameter(":pwd", name);

            cmd.Parameters.Add(parp);        

            OracleParameter pari = new OracleParameter(":id", id);

            cmd.Parameters.Add(pari); 

            int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示添加成功

            conn.Close();

            cmd.Dispose();

            return result;        

 

        }

 

        ///<summary>

        ///查询

        ///</summary>

        ///<returns></returns>

        public DataTable Select()

        {

            OracleConnection conn = new OracleConnection(@"Data Source=SBZX;User ID=simis;Password=zeda");//Data Source后面跟你数据库的名字,User ID为用户名,Password为密码

            conn.Open();

            string sql = "select * from users";

            OracleCommand cmd = new OracleCommand(sql, conn);

            OracleDataAdapter oda = new OracleDataAdapter(cmd);

            DataTable dt = new DataTable();

            oda.Fill(dt);

            conn.Close();

            cmd.Dispose();

            return dt;        

        }

 

方法写好后,下面举一个查询的例子,在form窗体中拖一个DataGridView,然后在Load方法中
    private void Form1_Load(object sender, EventArgs e)
        {
              dataGridView1.DataSource = Select();
        }
这样一运行,DataGridView中就会显示数据了

 

 

 

展开阅读全文

没有更多推荐了,返回首页