C# 连接Oracle,并调用存储过程(存在返回值),C# 调用sql存储过程
1.获取Oracle表格信息
public OracleHelpers(string ConnStr)
{
ConnectionString = ConnStr;
conn = new OracleConnection(ConnectionString);
error = "";
//String connString = @"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.210)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL)));User Id=TEST;Password=TEST123";
//Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.1.50.23)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=未知)));User ID=gadata0004;Password=gold; }
public bool CheckConn(out string err)
{
try
{
conn.Open();
err = "";
return true;
}
catch (Exception ex)
{
err = ex.Message;
return false;
}
finally
{
conn.Close();
}
}
//获取表名列表
public DataTable GetConnectionTables()
{
return GetDataTable("SELECT TABLE_NAME 表名 FROM USER_ALL_TABLES");
} //获取表名列表
public DataTable GetConnectionTables(string sqlStr)
{
return GetDataTable(sqlStr);
} //获取Oracle某连接下的所有视图名
public DataTable GetConnectionViews()
{
return GetDataTable("select View_Name from user_views");
} //根据sql语句返回datatable
public DataTable GetDataTable(string sqlStr)
{
DataTable dt = null;
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
} OracleDataAdapter da = new OracleDataAdapter(sqlStr, conn); dt = new DataTable();
da.Fill(dt);
}
catch (Exception ex)
{
error += "GetConnectionViews:" + ex.Message + "\r\n";
MessageBox.Show(error);
}
finally
{
conn.Close();
}
return dt;
} /// <summary>
/// 返回DataSet对象
/// </summary>
/// <param name="sqlStr">sql语句</param>
/// <returns>DataSet对象</returns>
public DataSet GetDataSet(string sqlStr)
{
DataSet ds = null;
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
} OracleDataAdapter da = new OracleDataAdapter(sqlStr, conn); ds = new DataSet();
da.Fill(ds);
}
catch (Exception ex)
{
error += "GetConnectionViews:" + ex.Message + "\r\n";
MessageBox.Show(error);
}
finally
{
conn.Close();
}
return ds;
} /// <summary>
/// 获取表的字段名
/// </summary>
/// <param name="tableName">表名</param>
/// <returns>DataTable</returns>
public DataTable GetColumns(string tableName)
{
//string sSql = "SELE CT USER_TAB_COLS.COLUMN_NAME as 列名 FROM USER_TAB_COLS "
//+ "inner join user_col_comments on user_col_comments.TABLE_NAME = USER_TAB_COLS.TABLE_NAME "
//+ "and user_col_comments.COLUMN_NAME = USER_TAB_COLS.COLUMN_NAME and USER_TAB_COLS.TABLE_NAME = '" + tableName + "'";
string sSql = "select distinct COLUMN_NAME as 列名 from all_TAB_COLUMNS where table_name = '" + tableName + "'";
return GetDataTable(sSql); ////获取表信息的T-SQL语句备用
//string sql = "SELECT USER_TAB_COLS.TABLE_NAME as 表名, USER_TAB_COLS.COLUMN_NAME as 列名, "
// + "USER_TAB_COLS.DATA_TYPE as 数据类型, USER_TAB_COLS.DATA_LENGTH as 长度, "
// + "USER_TAB_COLS.NULLABLE as 是否为空, USER_TAB_COLS.COLUMN_ID as 列序号, user_col_comments.comments as 备注 "
// + "inner join user_col_comments on user_col_comments.TABLE_NAME = USER_TAB_COLS.TABLE_NAME "
// + "and user_col_comments.COLUMN_NAME = USER_TAB_COLS.COLUMN_NAME and USER_TAB_COLS.TABLE_NAME = 'T_COMPANY_DETAIL" + "'";
}
2.调用 三个存储过程 增、删、改
public void item_update(string myitemcode,
string myitemcode_new, string myitemname, string myitemstyle,
string myitemnaturename, string myitemtypename, string mypositionname,
out int intmark, out string strreason)
{
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(ConnectionString);
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "item_Update";
OracleParameter[] parm = new OracleParameter[];
parm[] = new OracleParameter("myitemcode", OracleType.VarChar);
parm[] = new OracleParameter("myitemcode_new", OracleType.VarChar);
parm[] = new OracleParameter("myitemname", OracleType.VarChar);
parm[] = new OracleParameter("myitemstyle", OracleType.VarChar);
parm[] = new OracleParameter("myitemnaturename", OracleType.VarChar);
parm[] = new OracleParameter("myitemtypename", OracleType.VarChar);
parm[] = new OracleParameter("mypositionname", OracleType.VarChar);
parm[] = new OracleParameter("intmark", OracleType.Int32);
parm[] = new OracleParameter("strreason", OracleType.VarChar,35);
for (int i = ; i < parm.Length - ; i++)
{
parm[i].Direction = ParameterDirection.Input;
}
//指定返回参数
parm[].Direction = ParameterDirection.Output;
parm[].Direction = ParameterDirection.Output;
//传入参数赋值
parm[].Value = myitemcode;
parm[].Value = myitemcode_new;
parm[].Value = myitemname;
parm[].Value = myitemstyle;
parm[].Value = myitemnaturename;
parm[].Value = myitemtypename;
parm[].Value = mypositionname;
//将对象放到cmd里面
for (int i = ; i < parm.Length; i++)
{
cmd.Parameters.Add(parm[i]);
}
if (conn.State != ConnectionState.Open)
conn.Open(); cmd.Connection = conn;
cmd.ExecuteNonQuery(); //返回参数
intmark = Convert.ToInt32(parm[].Value);//res
strreason = parm[].Value.ToString();//err
}
catch
{ intmark = ;
strreason = "";
} finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
conn.Dispose();
cmd.Parameters.Clear();
cmd.Dispose();
}
}
public void item_deleted(string itemcode, out int intmark, out string strreason)
{
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(ConnectionString);
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "item_deleted";
OracleParameter[] parm = new OracleParameter[];
parm[] = new OracleParameter("itemcode", OracleType.VarChar);
parm[] = new OracleParameter("intmark", OracleType.Int32);
parm[] = new OracleParameter("strreason", OracleType.VarChar,35);
for (int i = ; i < parm.Length - ; i++)
{
parm[i].Direction = ParameterDirection.Input;
}
parm[].Direction = ParameterDirection.Output;
parm[].Direction = ParameterDirection.Output; parm[].Value = itemcode;
for (int i = ; i < parm.Length; i++)
{
cmd.Parameters.Add(parm[i]);
}
if (conn.State != ConnectionState.Open)
conn.Open(); cmd.Connection = conn;
cmd.ExecuteNonQuery(); //返回参数
intmark = Convert.ToInt32(parm[].Value);//res
strreason = parm[].Value.ToString();//err
}
catch
{
intmark = ;
strreason = "";
} finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
conn.Dispose();
cmd.Parameters.Clear();
cmd.Dispose();
}
} public void item_insert(string myitemcode, string myitemname, string myitemstyle, string myitemnaturename,
string myitemtypename, string mypositionname, string myitemunitgroupname, string myunitname, string mycustomname0, string mycustomname2,
out int intmark, out string strreason)
{
//ConnectionString
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(ConnectionString);
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "item_insert";
OracleParameter[] parm = new OracleParameter[];
parm[] = new OracleParameter("myitemcode", OracleType.VarChar);
parm[] = new OracleParameter("myitemname", OracleType.VarChar);
parm[] = new OracleParameter("myitemstyle", OracleType.VarChar);
parm[] = new OracleParameter("myitemnaturename", OracleType.VarChar);
parm[] = new OracleParameter("myitemtypename", OracleType.VarChar);
parm[] = new OracleParameter("mypositionname", OracleType.VarChar);
parm[] = new OracleParameter("myitemunitgroupname", OracleType.VarChar);
parm[] = new OracleParameter("myunitname", OracleType.VarChar);
parm[] = new OracleParameter("mycustomname0", OracleType.VarChar);//out //out
parm[] = new OracleParameter("mycustomname2", OracleType.VarChar);//out
intmark = ;
strreason = "";
parm[] = new OracleParameter("intmark", OracleType.Int32);
parm[] = new OracleParameter("strreason", OracleType.VarChar,35);
for (int i = ; i < parm.Length - ; i++)
{
parm[i].Direction = ParameterDirection.Input;
}
parm[].Direction = ParameterDirection.Output;
parm[].Direction = ParameterDirection.Output; parm[].Value = myitemcode;
parm[].Value = myitemname;
parm[].Value = myitemstyle;
parm[].Value = myitemnaturename;
parm[].Value = myitemtypename;
parm[].Value = mypositionname;
parm[].Value = myitemunitgroupname;
parm[].Value = myunitname;
parm[].Value = mycustomname0;
parm[].Value = mycustomname2; for (int i = ; i < parm.Length; i++)
{
cmd.Parameters.Add(parm[i]);
}
if (conn.State != ConnectionState.Open)
conn.Open(); cmd.Connection = conn;
cmd.ExecuteNonQuery(); //返回参数
intmark = Convert.ToInt32(parm[].Value);//res
strreason = parm[].Value.ToString();//err
}
catch
{
intmark = ;
strreason = "";
} finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
conn.Dispose();
cmd.Parameters.Clear();
cmd.Dispose();
} }
3.三个存储过程整合成一个
public void OrecalNew(string CunChuName, OracleParameter[] parm, int OutCount, out List<string> Outlist)
{
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(ConnectionString);
try
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = CunChuName;
for (int i = ; i < parm.Length; i++)
{
cmd.Parameters.Add(parm[i]);
}
if (conn.State != ConnectionState.Open)
conn.Open(); cmd.Connection = conn;
cmd.ExecuteNonQuery(); //返回参数
if (OutCount != )
{
List<string> listOut = new List<string>();
for (int a = parm.Length - OutCount; a < parm.Length; a++)
{
listOut.Add(parm[a].Value.ToString().Trim());
}
Outlist = listOut;
}
else
{
Outlist = new List<string>();
}
}
catch
{
Outlist = new List<string>();
} finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
conn.Dispose();
cmd.Parameters.Clear();
cmd.Dispose();
} }
4.C# 调取本地SQL server 存储过程
注释:C# 在调取SQL存储过程时,与调取Oracle存储过程完全一致
SQL 存储过程
ALTER PROCEDURE [dbo].[Get_Data]
@QQ int,
@WW int,
@EE VARCHAR(50),
@RR VARCHAR(50),
@TT int out,
@YY int out
as
begin
set @TT = @QQ * @WW
set @YY = @WW
end
调取
public void Begin()
{
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123;database=DB_Config");
//建立一个空的SqlCommand对象
SqlCommand comm = new SqlCommand();
//制定调用类型为存储过程
comm.CommandType = CommandType.StoredProcedure;
//存储过程的名称
comm.CommandText = "Get_Data";
//定义数组
SqlParameter[] parm = new SqlParameter[];
//指定变量的类型
parm[] = new SqlParameter("QQ", SqlDbType.BigInt);
parm[] = new SqlParameter("WW", SqlDbType.BigInt);
parm[] = new SqlParameter("EE", SqlDbType.VarChar);
parm[] = new SqlParameter("RR", SqlDbType.VarChar);
parm[] = new SqlParameter("TT", SqlDbType.BigInt);
parm[] = new SqlParameter("YY", SqlDbType.BigInt);
//设置变量为传入参数还是传出参数
parm[].Direction = ParameterDirection.Input;
parm[].Direction = ParameterDirection.Input;
parm[].Direction = ParameterDirection.Input;
parm[].Direction = ParameterDirection.Input;
parm[].Direction = ParameterDirection.Output;
parm[].Direction = ParameterDirection.Output;
//给传入参数赋值
parm[].Value = TT1.Text.ToString();
parm[].Value = TT2.Text.ToString();
parm[].Value = TT3.Text.ToString();
parm[].Value = TT4.Text.ToString(); //将对象放到Comm里面
for (int a = ; a < parm.Length; a++)
{
comm.Parameters.Add(parm[a]);
}
//执行
if (conn.State != ConnectionState.Open)
conn.Open(); comm.Connection = conn;
comm.ExecuteNonQuery();
//执行完接收返回参数
MessageBox.Show("信息:"+ parm[].Value +"and"+parm[].Value);
}
亲测成功
C# 连接Oracle,并调用存储过程(存在返回值),C# 调用sql存储过程的更多相关文章
- 利用SQLServer查询分析器获取存储过程的返回值,检查测试存储过程
1.存储过程没有返回值的情况(即存储过程语句中没有return之类的语句)用方法 int count = ExecuteNonQuery(..)执行存储过程其返回值只有两种情况(1)如果通过查询分析器 ...
- 关于ExecuteNonQuery执行存储过程的返回值 、、实例讲解存储过程的返回值与传出参数、、、C#获取存储过程的 Return返回值和Output输出参数值
关于ExecuteNonQuery执行存储过程的返回值 用到过ExecuteNonQuery()函数的朋友们在开发的时候肯定这么用过. if(cmd.ExecuteNonQuery("xxx ...
- 在Delphi中如何获得SQL中存储过程的返回值?
示例存储过程:create procedure proc_loginusername varchar(20),password varchar(20)asdeclare @result intsele ...
- .net 接收存储过程的返回值 。。。。
.net 接收存储过程的返回值 .... Posted on 2009-06-10 20:26 且行且思 阅读(...) 评论(...) 编辑 收藏 例如在向数据库添加新数据时,需要检测是否有重复 本 ...
- C#获取执行存储过程的" 返回值"代码
以下是C#代码: /// <summary> /// 执行存储过程,返回" 返回值" /// </summary> /// <param name=& ...
- AJAX JQuery 调用后台方法返回值(不刷新页面)
AJAX JQuery 调用后台方法返回值(不刷新页面) (1)无参数返回值(本人亲试返回结果不是预期结果) javascript方法: $(function () { //无 ...
- SqlServer如何获取存储过程的返回值
1.Output参数返回值 1 CREATE PROCEDURE [dbo].[upInformation]( 2 @age int , 3 @id bigint OUTPUT 4 ) 5 AS 6 ...
- JDBC和JPA调用储存过程 接收存储过程有返回值
============jdbc==========================================--java代码:一个输出参数--String connURL = null;Con ...
- oracle的function和procedure返回值给shell
本文演示两个关于如何在shell中调用oracle的function和procedure,并将返回值返回给shell. 1.首在package中创建function和procedure,脚本如下: G ...
- Entity Framework中对存储过程的返回值的处理
很早就开始注意到EF了,但一直没有机会用,换了工作后,第一个项目就使用EF6进行开发. 项目不是很大,EF完全可以胜任. 但是开发过程中,难免还是会遇到一些复杂的运算,需要频繁访问数据库. 此时,想到 ...
随机推荐
- FFmpeg多媒体文件格式探测
FFmpeg版本:3.4 在FFmpeg中,每一种文件容器格式都对应一种AVInputFormat 结构,位于源码中libavformat文件夹中.当调用avformat_open_input的时候, ...
- shell 中可以for 循环的时间加减日期格式
..};do # LAST_HOUR=`date -d '-${num} hour' +%H` 不可for循环,报格式错误 LAST_HOUR=`date "+%H" -d -${ ...
- ubuntu linux常用指令(1)
序号 命令 说明 1 sudo su 从普通用户切换到root用户 2 su user 从root用户切换到普通用户 3 ls 列出当前目录的文件和目录,但是不包括隐藏文件和目录 4 ls -a 列出 ...
- MySQL内置功能之事务、函数和流程控制
主要内容: 一.事务 二.函数 三.流程控制 1️⃣ 事务 一.何谓事务? 事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性. # ...
- textarea文本域的高度随内容的变化而变化
用css控制textarea文本域的高度随内容的变化而变化,不出现滚动条. CSS代码: 复制代码 代码如下: .t_area{ width:300px; overflow-y:visible } & ...
- jsp实现查询与展示功能
今天做了一天的这个功能,开始是数据库一直打不开,显示Class not found,一直调试,调试了一下午.原来是mysql的jar包,必须要放到WEB-INF这个目录下的lib文件夹中,然后bui ...
- 83. Remove Duplicates from Sorted List (List)
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- Header add Access-Control-Allow-Origin: *
允许所有域名跨域 Header add Access-Control-Allow-Origin: *
- jquery页面初始化控件时间
this.InitControlTime = function () { if ($("#txt_Id_").val() == "") { var today ...
- pymsql的简单实用方法
在进行本文以下内容之前需要注意: 1.你有一个MySQL数据库,并且已经启动. 2.你有可以连接该数据库的用户名和密码 3.你有一个有权限操作的database 连接数据库 #导入pymsql imp ...