c#调用存储过程返回多组数据
c#调用存储过程返回多组数据然后把数据写入listView中的表格,麻烦给下代码可以吗?或者说一下具体过程也行...
c#调用存储过程返回多组数据然后把数据写入listView中的表格,麻烦给下代码可以吗?或者说一下具体过程也行
展开
3个回答
展开全部
根据你前面提到的问题:http://zhidao.baidu.com/question/455402427.html?oldq=1,解答如下:
oracle存储过程的定义:
create or replace procedure prc_get_tables(dept_cur out sys_refcursor, emp_cur out sys_refcursor) is
begin
open dept_cur for select deptno,dname from dept;
open emp_cur for select empno,ename from emp;
end prc_get_tables;
C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.OracleClient;
using System.Data;
/// <summary>///TestManager 的摘要说明/// </summary>public class TestManager
{
//连接字符串
private const string CONNECT_STR = "data source=sy;user id=scott;password=tiger;";
public static void GetAllTables(out List<Dept> deptList, out List<Emp> empList)
{
using (OracleConnection cnn = new OracleConnection(CONNECT_STR))
{
OracleCommand cmd = new OracleCommand(
"prc_get_tables", cnn); //调用自定义程序包中的过程
cmd.CommandType = CommandType.StoredProcedure;
OracleParameter p1 = new OracleParameter();
p1.ParameterName = "dept_cur";
p1.OracleType = OracleType.Cursor; //指定参数的数据类型为游标类型
p1.Direction = ParameterDirection.Output; //指定参数为输出参数
cmd.Parameters.Add(p1);
OracleParameter p2 = new OracleParameter();
p2.ParameterName = "emp_cur";
p2.OracleType = OracleType.Cursor; //指定参数的数据类型为游标类型
p2.Direction = ParameterDirection.Output; //指定参数为输出参数
cmd.Parameters.Add(p2);
cnn.Open();
OracleDataReader dr = cmd.ExecuteReader();
deptList = new List<Dept>();
while (dr.Read())
{
deptList.Add(new Dept { DeptNo = dr.GetInt32(0), DName = dr.GetString(1) });
}
dr.NextResult();
empList = new List<Emp>();
while (dr.Read())
{
empList.Add(new Emp { EmpNo = dr.GetInt32(0), EName = dr.GetString(1) });
}
dr.Close();
cnn.Close();
}
}
}
public class Dept
{
public int DeptNo { get; set; }
public string DName { get; set; }
}
public class Emp
{
public int EmpNo { get; set; }
public string EName { get; set; }
}
只给出了数据访问底层的代码,弄到listview里,自己应该能够完成了吧。
oracle存储过程的定义:
create or replace procedure prc_get_tables(dept_cur out sys_refcursor, emp_cur out sys_refcursor) is
begin
open dept_cur for select deptno,dname from dept;
open emp_cur for select empno,ename from emp;
end prc_get_tables;
C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.OracleClient;
using System.Data;
/// <summary>///TestManager 的摘要说明/// </summary>public class TestManager
{
//连接字符串
private const string CONNECT_STR = "data source=sy;user id=scott;password=tiger;";
public static void GetAllTables(out List<Dept> deptList, out List<Emp> empList)
{
using (OracleConnection cnn = new OracleConnection(CONNECT_STR))
{
OracleCommand cmd = new OracleCommand(
"prc_get_tables", cnn); //调用自定义程序包中的过程
cmd.CommandType = CommandType.StoredProcedure;
OracleParameter p1 = new OracleParameter();
p1.ParameterName = "dept_cur";
p1.OracleType = OracleType.Cursor; //指定参数的数据类型为游标类型
p1.Direction = ParameterDirection.Output; //指定参数为输出参数
cmd.Parameters.Add(p1);
OracleParameter p2 = new OracleParameter();
p2.ParameterName = "emp_cur";
p2.OracleType = OracleType.Cursor; //指定参数的数据类型为游标类型
p2.Direction = ParameterDirection.Output; //指定参数为输出参数
cmd.Parameters.Add(p2);
cnn.Open();
OracleDataReader dr = cmd.ExecuteReader();
deptList = new List<Dept>();
while (dr.Read())
{
deptList.Add(new Dept { DeptNo = dr.GetInt32(0), DName = dr.GetString(1) });
}
dr.NextResult();
empList = new List<Emp>();
while (dr.Read())
{
empList.Add(new Emp { EmpNo = dr.GetInt32(0), EName = dr.GetString(1) });
}
dr.Close();
cnn.Close();
}
}
}
public class Dept
{
public int DeptNo { get; set; }
public string DName { get; set; }
}
public class Emp
{
public int EmpNo { get; set; }
public string EName { get; set; }
}
只给出了数据访问底层的代码,弄到listview里,自己应该能够完成了吧。
来自:求助得到的回答
展开全部
public static DataTable GetTable(SqlConnection sqlConn, string strSelect)
{
SqlCommand sqlCmd = null;
SqlDataAdapter sda = null;
DataTable table = null;
try
{
sqlCmd = new SqlCommand(strSelect, sqlConn);
sda = new SqlDataAdapter(sqlCmd);
table = new DataTable();
sda.Fill(table);
return table;
}
catch (Exception ex)
{
return null;
throw ex;
}
finally
{
sqlCmd.Dispose();
sda.Dispose();
table.Dispose();
}
}
再将listview与 table绑定.
{
SqlCommand sqlCmd = null;
SqlDataAdapter sda = null;
DataTable table = null;
try
{
sqlCmd = new SqlCommand(strSelect, sqlConn);
sda = new SqlDataAdapter(sqlCmd);
table = new DataTable();
sda.Fill(table);
return table;
}
catch (Exception ex)
{
return null;
throw ex;
}
finally
{
sqlCmd.Dispose();
sda.Dispose();
table.Dispose();
}
}
再将listview与 table绑定.
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询