C#调用Oracle数据库第1步 数据表
- student(
- studentid varchar2
- studentname varchar2
- )
数据:
studentid studentname
001 001
002 002
002 003
C#调用Oracle数据库第2步 建包
- CREATE OR REPLACE PACKAGE PKG_SELECT_STUDENT
- AS
- TYPE T_CURSOR IS REF CURSOR;
- PROCEDURE Getusername
- (
- us_id IN Varchar2,
- cur_name OUT T_CURSOR
- );
C#调用Oracle数据库第3步 建包体
- CREATE OR REPLACE PACKAGE BODY PKG_SELECT_STUDENT
- AS
- PROCEDURE Getusername
- (
- us_id IN varchar2,
- cur_name OUT T_CURSOR
- )
- IS
- BEGIN
- OPEN cur_name FOR
- SELECT * FROM student WHERE studentid=us_id;
- END Getusername;
- END PKG_select_student;
C#调用Oracle数据库第4步 我的webform.cs文件
在文本框输入ID,点击按钮查询,有多条数据填充显示在gatagrid里面
- private void Button3_Click(object sender, System.EventArgs e)
- {
- string usid=this.TextBox1.Text.Trim();
- Selectop st=new Selectop();
- this.DataGrid1.DataSource=st.GetSelectAll(usid);
- this.DataGrid1.DataBind();
- }
- public DataSet GetSelectAll(string usid)
- {
- OracleConnection con = DBoracle.CreateConnection();
- OracleCommand command = new OracleCommand();
- DataSet ds = new DataSet();
- try
- {
- command.Connection=con;
- command.CommandText="PKG_select_student.Getusername";
- command.CommandType=CommandType.StoredProcedure;
- command.Parameters.Add("us_id",OracleType.VarChar,10).Value=usid;
- command.Parameters.Add("cur_name",OracleType.Cursor);
- command.Parameters["cur_name"].Direction = ParameterDirection.Output;
- OracleDataAdapter adapter = new OracleDataAdapter(command);
- con.Open();
- //command.ExecuteNonQuery();
- //adapter.SelectCommand=command;
- adapter.Fill(ds);
- }
- catch(System.Exception ex)
- {
- throw ex;
- }
- finally
- {
- con.Close();
- command.Dispose();
- //adapter.Dispose();
- }
- return ds;
- }
【编辑推荐】