我很疯狂Sybase JDBC驱动程序如何使用mixed IN和OUTparameters 处理存储过程.看看这个简单的存储过程:
CREATE OR REPLACE PROCEDURE p (IN i1 INT, OUT o1 INT, IN i2 INT, OUT o2 INT)
BEGIN
set o1 = i1;
set o2 = i2;
END
Run Code Online (Sandbox Code Playgroud)
以下是我用JDBC调用它的方法:
CallableStatement c = connection.prepareCall("{ call dba.p(?, ?, ?, ?) }");
c.setInt(1, 1);
c.setInt(3, 2);
c.registerOutParameter(2, Types.INTEGER);
c.registerOutParameter(4, Types.INTEGER);
c.execute();
System.out.println(c.getObject(2));
System.out.println(c.getObject(4));
Run Code Online (Sandbox Code Playgroud)
但这导致了
1
null
Run Code Online (Sandbox Code Playgroud)
这是怎么回事??这是JDBC驱动程序中的一个非常邪恶的错误还是我完全错过了什么?通过反复试验,我发现这是一种工作方式:
c.setInt(1, 1);
c.setInt(2, 2);
c.registerOutParameter(3, Types.INTEGER);
c.registerOutParameter(4, Types.INTEGER);
c.execute();
System.out.println(c.getObject(3));
System.out.println(c.getObject(4));
Run Code Online (Sandbox Code Playgroud)
现在的结果是
1
2
Run Code Online (Sandbox Code Playgroud)
JDBC驱动程序是否秘密重新排序IN和OUT参数?
我正在使用SQL …
在公共API中返回函数调用的结果有一些不同的常见模式.目前尚不清楚哪种方法最好.是否对最佳实践达成了普遍共识,或者至少是令人信服的理由为什么一种模式比其他模式更好?
更新通过公共API,我指的是暴露于依赖程序集的公共成员.我并不是指公开作为Web服务公开的API.我们可以假设客户端正在使用.NET.
我在下面写了一个示例类来说明返回值的不同模式,我已经注释了它们表达了我对每个模型的关注.
这是一个很长的问题,但我确信我不是唯一一个考虑过这个问题的人,希望这个问题对其他人来说很有意思.
public class PublicApi<T> // I am using the class constraint on T, because
where T: class // I already understand that using out parameters
{ // on ValueTypes is discouraged (http://msdn.microsoft.com/en-us/library/ms182131.aspx)
private readonly Func<object, bool> _validate;
private readonly Func<object, T> _getMethod;
public PublicApi(Func<object,bool> validate, Func<object,T> getMethod)
{
if(validate== null)
{
throw new ArgumentNullException("validate");
}
if(getMethod== null)
{
throw new ArgumentNullException("getMethod");
}
_validate = validate;
_getMethod = getMethod;
}
// This is the most …Run Code Online (Sandbox Code Playgroud) public class Foo
{
public void DoFoo()
{
int x;
var coll = TheFunc("bar", out x);
}
public Func<string, int, ICollection<string>> TheFunc { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
错误:"参数2不应与'out'关键字一起传递."
public class Foo
{
public void DoFoo()
{
int x;
var coll = TheFunc("bar", out x);
}
public Func<string, out int, ICollection<string>> TheFunc { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
错误:"无效的方差修饰符.只能将接口和委托类型参数指定为变体."
如何在此函数中获取out参数?
我正在重构一个现有的DAL,它有一个用户调用的外观和一个内部类,它根据ADO.Net提供程序进行实际工作,例如SqlProvider,我正在努力确保代码干,我'通过使用Func完成了所以我可以这样做:
return RunCommand(c => c.ExecuteNonQuery(commandText, parameters));
Run Code Online (Sandbox Code Playgroud)
RunCommand方法如下所示:
private T RunCommand<T>(Func<Commands, T> toRun)
{
return toRun(CreateCommand());
}
Run Code Online (Sandbox Code Playgroud)
该CreateCommand()方法只是构建要使用的命令对象,这允许我有一个方法来处理所有只返回预期类型的调用,例如DataSet,DataReader等
我遇到的问题是,在立面上的几个调用提供了一个out参数,我知道应该可以删除重复的代码,如果我可以使用委托,但经过大量的谷歌搜索和实验我没有设法弄清楚如何.代码是:
Commands commands = CreateCommand();
return commands.ExecuteNonQuery(out cmd, commandText, parameters);
Run Code Online (Sandbox Code Playgroud)
我真正想做的是能够打电话:
return RunCommand(c => c.ExecuteNonQuery(out cmd, commandText, parameters));
Run Code Online (Sandbox Code Playgroud)
我已经看到了这个现有问题,但对于我的生活,我无法弄清楚如何将其变成我需要的东西.
这个代表似乎是我需要的,private delegate V TestOutParameter<T, U, V>(T a, out U b, V c);但我调用它的代码是不正确的:
private V RunCommand<T, U, V>(TestOutParameter<Commands, DbCommand, V> commandToExecute)
{
DbCommand cmd;
return (V)commandToExecute(CreateCommand(), out cmd);
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我,因为这让我疯了一个星期!
我正在尝试在使用 Spring(非引导)配置的 JUnit 测试中创建一个带有 out 参数的存储过程。我从文档和示例中了解到,我必须为静态方法设置别名以响应存储过程调用。但是,我找不到提到它是如何在没有参数的情况下工作的。静态方法的方法签名中有什么内容?
package com.example;
import static java.sql.Types.INTEGER;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.h2.tools.SimpleResultSet;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { H2tests.TestConfiguration.class })
public class H2tests {
@Autowired
DataSource dataSource;
public static ResultSet executeMyProc(int a) {
return new SimpleResultSet();
}
// what should the type of the second parameter be?
public static ResultSet executeMyProc(int a, int …Run Code Online (Sandbox Code Playgroud) 更新所以完全拉动了一个工具时刻.我的意思是参考与Out/Ref.任何说"参考"的东西我的意思都是参考
SomeMethod(Object someObject)
与
SomeMethod(out someObject)
抱歉.只是不想更改代码,所以答案已经有意义.
据我所知,不像ref那样它"复制"指针并在堆栈上创建一个新空间来使用该指针,但不会改变指针:
SomeMethod()
{
SomeThing outer = new SomeThing();
RefMethod(ref outer)
}
RefMethod(ref inner) //new space on stack created and uses same pointer as outer
{
inner.Hi = "There"; //updated the object being pointed to by outer
inner = new SomeThing();//Given a new pointer, no longer shares pointer with outer
//New object on the heap
}
Run Code Online (Sandbox Code Playgroud)
Out复制指针并可以操作它指向的位置:
SomeMethod()
{
SomeThing outer = new SomeThing();
RefMethod(out outer)
}
RefMethod(out inner) //same pointer shared
{
inner …Run Code Online (Sandbox Code Playgroud) 这段代码有效,但效率低下,因为它重复查找ignored字典。如何TryGetValue()在 LINQ 语句中使用字典方法使其更高效?
IDictionary<int, DateTime> records = ...
IDictionary<int, ISet<DateTime>> ignored = ...
var result = from r in records
where !ignored.ContainsKey(r.Key) ||
!ignored[r.Key].Contains(r.Value)
select r;
Run Code Online (Sandbox Code Playgroud)
问题是我不确定如何在 LINQ 语句中声明一个用于 out 参数的变量。
我只是遇到一个奇怪的问题,我无法检索sql存储过程输出参数值.我解决了这个问题将近2个小时.
代码很简单
using (var con = new SqlConnection(connectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("sp_mgsearach", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param1 = new SqlParameter("@SearchTerm", SqlDbType.VarChar);
param1.Value = searchTerm;
param1.Direction = ParameterDirection.Input;
cmd.Parameters.Add(param1);
SqlParameter param2 = new SqlParameter("@start", SqlDbType.Int);
param2.Value = start;
param2.Direction = ParameterDirection.Input;
cmd.Parameters.Add(param2);
SqlParameter param3 = new SqlParameter("@end", SqlDbType.Int);
param3.Value = end;
param3.Direction = ParameterDirection.Input;
cmd.Parameters.Add(param3);
SqlParameter param4 = new SqlParameter("@total", SqlDbType.Int);
param4.Direction = ParameterDirection.InputOutput;
param4.Value = 0;
cmd.Parameters.Add(param4);
var reader = cmd.ExecuteReader();
LoadHits(reader);
if (lstHits.Count > 0) …Run Code Online (Sandbox Code Playgroud) 我正在编写一个包含一些哈希函数的库.
我希望其中一个函数返回为哈希使用而生成的哈希(byte [])和随机盐(byte []).什么是最友好,最直观的方式?
我有这一点,通过返回的哈希值,然后传回的盐作为out参数,它完美的作品工作的C#版本,但Java不给我出来参数的奢侈品.
有什么想法吗?
Apple的方法为什么不被NSError**宣布为NSError * __autoreleasing *?
在过渡到ARC发行说明似乎表明,他们应该是(?).
例如,NSFileManager.h中的所有例程.但我实际上并没有看到任何 Apple标题使用过渡到ARC发行说明中所述的内容:
and the method declaration would typically be:
-(BOOL)performOperationWithError:(NSError * __autoreleasing *)error;
Run Code Online (Sandbox Code Playgroud)
可能是因为支持所有NSError * error = nil;声明所暗示的遗留代码库,strong所以如果Apple放在__autoreleasing那里它会导致__autoreleasing每次都创建一个临时局部变量?我唯一能想到的就是.
out-parameters ×10
c# ×5
java ×2
ado.net ×1
api-design ×1
c#-4.0 ×1
cocoa ×1
delegates ×1
func ×1
h2 ×1
heap ×1
jconnect ×1
jdbc ×1
linq ×1
objective-c ×1
out ×1
return-value ×1
sql-server ×1
sqlanywhere ×1
stack ×1
trygetvalue ×1
tryparse ×1
tuples ×1