我正在尝试连接到 sybase ASE 15 并调用执行某些 DML 的 SP。我想知道是否有人有任何指向类似于 SQLhelper.cs 的 sybase 帮助器类的指针,或者是否有人有任何指向任何博客/示例代码的指针。
最终,我会将解决方案迁移到 SQl Server 2008 R2(从现在开始的几个月),因此我想创建一个通用实现,即使在迁移之后也可以使用它而无需太多更改。
public class SybaseDBHelper : ISybaseDBHelper
{
private AseConnection conn;
private AseCommand cmd;
private AseDataAdapter adapter;
private DataSet outDS;
protected static readonly ILog _logger = LogManager.GetLogger(typeof (SybaseDBHelper));
#region InsertData
public int InsertDataUsingStoredProcedure(string storedProcedureName, DatabaseEnum dbName, List<AseParameter> parameters)
{
var connFactory = new ConnectionFactory();
int _errorCode = 0;
string connectionString = connFactory.GetConnectionString(dbName);
using (conn = connFactory.GetAseConnectionString(connectionString))
{
try
{
conn.Open();
if (conn.State == ConnectionState.Open)
{
using (cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = storedProcedureName;
if (parameters != null )
{
foreach (AseParameter param in parameters)
{
cmd.Parameters.Add(param);
}
}
_errorCode = cmd.ExecuteNonQuery();
}
}
}
catch (AseException ex)
{
_logger.ErrorFormat("Error Inserting Data into Database {0}", ex);
throw;
}
finally
{
conn.Close();
}
}
return _errorCode;
}
#endregion
#region IDisposable Members
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
conn.Dispose();
conn = null;
GC.SuppressFinalize(this);
}
}
#endregion
} }
界面
using System;
using System.Collections.Generic;
using System.Data;
using LiabilitiesMI.Common.DataObjects;
using Sybase.Data.AseClient;
namespace LiabilitiesMI.Common.Interfaces
{
public interface ISybaseDBHelper : IDisposable
{
DataSet GetDataUsingStoredProcedure(string storedProcedureName, DatabaseEnum dbName, List<AseParameter> parameters);
int InsertDataUsingStoredProcedure(string storedProcedureName, DatabaseEnum dbName, List<AseParameter> parameters);
}
}
-- 以这种方式调用它会调用显式垃圾回收
using (ISybaseDBHelper _DBHelper = ObjectFactory.GetDBHelper())
{
_DBHelper.InsertDataUsingStoredProcedure("usp_load_fx_spot_rate_into_staging", DatabaseEnum.Staging, input);
}
如果您正在为 Sybase(或 MS)开发 SQL,则最好使用开发人员工具,例如 SQLProgrammer 或 Rapid SQL。它通过消除手工劳动来提高生产力。
如果您没有,那么 Sybase 附带的实用程序就足够了,但是您需要学习使用该目录。用于字符界面的 isql 或在 GUI 界面中提供命令和结果集窗口的 DBISQL。
在服务器上打开会话后,键入以下 SQL 命令。这些执行询问目录的系统存储过程:
exec sp_help (object_name) -- 包括表和存储过程
执行 sp_helptext
有30个这样的询问。
我认为您意识到 sprocs 可以做的不仅仅是简单的 INSERTS;将各种数据类型作为参数处理,并且您发布的代码只是一个简单的示例,而不是完整的交换。通常,要启用存储过程以从 Java 应用程序执行,我们需要提供一个包装器(在 SQL 端)。