日期:2014-05-18  浏览次数:20427 次

.net中关于oracle的多行sql语句怎么执行?
原来执行update或者insert的时候我用的是command.ExecuteNonQuery(),现在我的语句是:
begin
insert......;
update......;
end
对于这样的语句如何执行?只能转成存储过程?

------解决方案--------------------
StringBuilder()
有换行的话就Enviroment.Newline

不过对于增删改 一般还是放在存储过程里会比较好
不仅仅是维护方便 事务的话也比较好控制
------解决方案--------------------
在事务中进行处理比较好。
------解决方案--------------------
以下是我用企业库写的一个执行多个sql句话的函数,楼主可以改动一下,用不用企业库就只改一些小段代码,反正就是循环执行,加上事务。
/// <summary>
/// 批量执行Sql语句
/// </summary>
/// <param name= "BatchSql "> Sql语句数组 </param>
public static void ExecuteBatch(string[] BatchSql)
{
SqlDatabase db = GetSqlDatabase();//创建数据库连接
using (SqlConnection dbconn = (SqlConnection)db.CreateConnection())
{
//打开连接
dbconn.Open();

//创建事务
SqlTransaction dbtran = dbconn.BeginTransaction();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
try
{ //执行两个保存数据集的操作
for (int i = 0; i < BatchSql.Length; i++)
{
cmd.CommandText = BatchSql[i];
db.ExecuteNonQuery(cmd, dbtran);
}
//执行完成后提交事务
dbtran.Commit();
}
catch
{
//回滚事务
dbtran.Rollback();
throw;
}
finally
{
//关闭连接
dbconn.Close();
}
}
}
------解决方案--------------------
还是用存储过程比较好
------解决方案--------------------
报错吗?报什么错?
------解决方案--------------------
存储过程不是很方便吗?
------解决方案--------------------
换一种写法吧。现在这样只能在存储过程中用。
insert into tab1 (tablename,tabledef) select table_name,comments from user_tab_comments where table_name not in (select tablename from tab1);
delete tab1 where tablename not in (select table_name from user_tab_comments);
------解决方案--------------------
楼主 这样的语句块是可以执行的,不需要写proc

好象要掉最后一行 end ; 的分号
------解决方案--------------------
这个是我用的 部分代码,应该是可以用滴

Dim strSql As String = "Declare " & _
"Begin " & _
"Delete From provincestat where tjsj=:tjsj ; " & _
"Insert Into provincestat(fz,tjsj,number) Values( 'A ',:tjsj, '1 '); " & _
"Insert Into provincestat(fz,tjsj,number) Values( 'B ',:tjsj, '2 '); " & _
"Insert Into provincestat(fz,tjsj,number) Values( 'C ',:tjsj, '3 '); " & _
"Insert Into provincestat(fz,tjsj,number) Values( 'D ',:tjsj, '4 '); " & _