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完全可以胜任. 但是开发过程中,难免还是会遇到一些复杂的运算,需要频繁访问数据库. 此时,想到 ...
随机推荐
- 开始使⽤ZooKeeper的API
在之前的章节中,我们使用zkCli工具介绍了ZooKeeper的基本操作.从本章开始,我们将会看到在应用中如何通过API来进行操作.首先介绍一下如何使用ZooKeeper的API进行开发,展示如何创建 ...
- MyBatis 学习记录4 MyBatis的一级缓存
主题 分享记录一下MyBatis的一级缓存相关的学习. Demo public static void firstLevelCache() { init("mybatis-config.xm ...
- Py小技巧一:在列表,字典,集合中根据条件筛选数据
1.过滤掉列表中的某些项---列表解析 data=[1,4,2,8,5,-1] res=[] a.依次迭代列表中每一个项 for x in data: if >=0: res.append(x) ...
- Spark 性能相关参数配置详解-任务调度篇
随着Spark的逐渐成熟完善, 越来越多的可配置参数被添加到Spark中来, 本文试图通过阐述这其中部分参数的工作原理和配置思路, 和大家一起探讨一下如何根据实际场合对Spark进行配置优化. 由于篇 ...
- MYSQL 创建常见问题
1.创建函数时,报错: 出错信息:ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL D ...
- if-return 语句
if(A > B): return A+1 return A-1 or if(A > B): return A+1 else: return A-1 +++++++++++++++++++ ...
- Java 读取jar内的文件的超简便方法
坑爹的java课程设计,偏要用jar来运行 读取.存储jar内文件的支持也好低 存储方法: 进入jar文件其实没有说的那么困难,jar文件本质是一个zip格式的压缩文件,只是把文件后缀名改了,要用Ja ...
- win10 家庭版使用注册表关闭windows defender
管理员身份运行 reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender" /v " ...
- Golang 之 Base62 编码
Base62 编码用62个可见字符来编码信息,也就是所谓的62进制,可用于缩短地址之类的.实现起来也很简单.当然,这个实现跟别人家的有可能不一样,反正自己能编能解就行. package main im ...
- Inno Setup创建快捷方式跟快速运行栏快捷方式
[Tasks] Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescrip ...