Stored procedures Sybase ASE命令执行SQL字符串
我在Sybase AdoNet驱动程序中发现了一个有趣的东西。我无法使用它正确执行dynamics sql语句 我有一个非常小的存储过程:Stored procedures Sybase ASE命令执行SQL字符串,stored-procedures,ado.net,execute,dynamic-sql,sap-ase,Stored Procedures,Ado.net,Execute,Dynamic Sql,Sap Ase,我在Sybase AdoNet驱动程序中发现了一个有趣的东西。我无法使用它正确执行dynamics sql语句 我有一个非常小的存储过程: CREATE PROCEDURE GetCustomerByName @customerFirstName nvarchar(50), @customerLastName nvarchar(50), @intParameter int AS /* Adaptive Server has expanded all '*'
CREATE PROCEDURE
GetCustomerByName
@customerFirstName nvarchar(50),
@customerLastName nvarchar(50),
@intParameter int AS
/* Adaptive Server has expanded all '*' elements in the following statement */
SELECT
Customer.Id,
Customer.FirstName,
Customer.LastName
FROM
Customer
WHERE
FirstName = @customerFirstName
AND LastName = @customerLastName
using (var aseConnection = new AseConnection(SybaseConnectionString))
using (var aseCommand = aseConnection.CreateCommand())
{
aseConnection.Open();
aseCommand.CommandText = @"
execute GetCustomerByName
@customerFirstName = ?,
@customerLastName = ?,
@intParameter = 1";
aseCommand.CommandType = CommandType.Text;
aseCommand.Parameters.Add(0, "John");
aseCommand.Parameters.Add(1, "Doe");
using (var dataReader = aseCommand.ExecuteReader())
{
while (dataReader.Read())
{
Console.WriteLine(
"Id: {0} - First Name: {1} - Last Name: {2}",
dataReader.GetInt32(0),
dataReader.GetString(1),
dataReader.GetString(2));
}
}
}
我想对以下存储过程执行动态sql语句:
CREATE PROCEDURE
GetCustomerByName
@customerFirstName nvarchar(50),
@customerLastName nvarchar(50),
@intParameter int AS
/* Adaptive Server has expanded all '*' elements in the following statement */
SELECT
Customer.Id,
Customer.FirstName,
Customer.LastName
FROM
Customer
WHERE
FirstName = @customerFirstName
AND LastName = @customerLastName
using (var aseConnection = new AseConnection(SybaseConnectionString))
using (var aseCommand = aseConnection.CreateCommand())
{
aseConnection.Open();
aseCommand.CommandText = @"
execute GetCustomerByName
@customerFirstName = ?,
@customerLastName = ?,
@intParameter = 1";
aseCommand.CommandType = CommandType.Text;
aseCommand.Parameters.Add(0, "John");
aseCommand.Parameters.Add(1, "Doe");
using (var dataReader = aseCommand.ExecuteReader())
{
while (dataReader.Read())
{
Console.WriteLine(
"Id: {0} - First Name: {1} - Last Name: {2}",
dataReader.GetInt32(0),
dataReader.GetString(1),
dataReader.GetString(2));
}
}
}
但不幸的是,我得到了一个例外:
Sybase.Data.AseClient.AseException was unhandled
HResult=-2146233087
Message=Must declare variable '@dr_ta0'.
Source=Sybase.AdoNet2.AseClient
StackTrace:
at Sybase.Data.AseClient1.AseCommand.CheckResult(Int32 res)
at Sybase.Data.AseClient1.AseCommand.Execute(CommandBehavior commandBehavior)
at Sybase.Data.AseClient1.AseCommand._ExecuteReader(CommandBehavior commandBehavior)
at Sybase.Data.AseClient1.AseCommand.ExecuteReader()
at Sybase.Data.AseClient.AseCommand.ExecuteReader()
at AseCommandTest.Program.Main(String[] args) in c:\Users\jkanczler\Documents\Visual Studio 2012\Projects\AseCommandTest\AseCommandTest\Program.cs:line 61
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
正如我看到的,Sybase不太支持传递给ASE命令的参数。使用AseCommand执行动态sql语句的最佳实践是什么?为什么要使用动态sql执行存储过程?因为我们使用的是NHibernate,但我们有一个包含大量存储过程的遗留数据库。我们不能一次从SPs导出所有业务逻辑,因此我们必须接受它。不幸的是,NHibernate将SPs作为具有不同参数顺序的动态SQL语句执行。因此,我们必须将这些参数映射到具有命名参数的特定参数。