C#调用Oracle存储过程的代码如下所示:
1 using System; 2 using System.Collections.Generic; 3 using System.Collections.ObjectModel; 4 using System.ComponentModel; 5 using System.Configuration; 6 using System.Data; 7 using System.Data.OracleClient; 8 using System.Drawing; 9 using System.Linq; 10 using System.Reflection; 11 using System.Text; 12 using System.Threading.Tasks; 13 using System.Windows.Forms; 14 15 namespace ExecuteProcByOracle 16 { 17 public partial class Form1 : Form 18 { 19 public Form1() 20 { 21 InitializeComponent(); 22 } 23 24 private void Btn_LoadData_Click(object sender, EventArgs e) 25 { 26 // 存储过程名称 27 string strProcName = "usp_yngr_getInfectionCard"; 28 // 存储过程参数 29 OracleParameter[] parameters = { 30 new OracleParameter("V_BeginTime",OracleType.VarChar), 31 new OracleParameter("V_EndTime",OracleType.VarChar), 32 new OracleParameter("V_DateType",OracleType.Number), 33 new OracleParameter("V_PtName",OracleType.VarChar), 34 new OracleParameter("V_PtChartNo",OracleType.VarChar), 35 new OracleParameter("V_DeptCode",OracleType.VarChar), 36 new OracleParameter("V_CheckedStatus",OracleType.VarChar), 37 // 返回值的类型是游标类型 38 new OracleParameter("cur_out",OracleType.Cursor) 39 }; 40 // 设置存储过程参数数组的值和参数的类型 41 parameters[0].Value = "2017-06-01"; 42 parameters[0].Direction = ParameterDirection.Input; 43 parameters[1].Value = "2017-07-31"; 44 parameters[1].Direction = ParameterDirection.Input; 45 parameters[2].Value = 1; 46 parameters[2].Direction = ParameterDirection.Input; 47 parameters[3].Value = ""; 48 parameters[3].Direction = ParameterDirection.Input; 49 parameters[4].Value = ""; 50 parameters[4].Direction = ParameterDirection.Input; 51 parameters[5].Value = ""; 52 parameters[5].Direction = ParameterDirection.Input; 53 parameters[6].Value = "1"; 54 parameters[6].Direction = ParameterDirection.Input; 55 parameters[7].Direction = ParameterDirection.Output; 56 57 this.dgv_Demo.DataSource = LoadData(strProcName, parameters); 58 } 59 60 private DataTable LoadData(string strProcName, params OracleParameter[] parameters) 61 { 62 63 DataTable dt = new DataTable(); 64 string strConn = ConfigurationManager.ConnectionStrings["HealthHospInfection"].ConnectionString; 65 using (OracleConnection conn = new OracleConnection(strConn)) 66 { 67 try 68 { 69 70 OracleCommand cmd = new OracleCommand(); 71 cmd.CommandText = strProcName; 72 cmd.CommandType = CommandType.StoredProcedure; 73 cmd.Connection = conn; 74 conn.Open(); 75 if (parameters != null) 76 { 77 // 添加参数 78 cmd.Parameters.AddRange(parameters); 79 } 80 81 // 取数据 82 using (OracleDataAdapter adapter = new OracleDataAdapter(cmd)) 83 { 84 85 adapter.Fill(dt); 86 } 87 } 88 catch (Exception ex) 89 { 90 MessageBox.Show("错误:" + ex.Message + "/r/n跟踪:" + ex.StackTrace); 91 } 92 finally 93 { 94 conn.Close(); 95 } 96 } 97 98 return dt; 99 } 100 } 101 }
示例代码下载地址:https://files.cnblogs.com/files/dotnet261010/CSharp%E6%89%A7%E8%A1%8COracle%E5%AD%98%E5%82%A8%E8%BF%87%E7%A8%8B.rar
时间: 2024-11-17 01:06:30