SybaseHelper 类?

发布于 2024-09-16 12:07:57 字数 200 浏览 2 评论 0原文

我正在尝试连接到 sybase ASE 15 并调用执行某些 DML 的 SP。我想知道是否有人有任何指向类似于 SQLhelper.cs 的 sybase 帮助程序类的指针,或者是否有人有任何指向任何博客/示例代码的指针。

最终,我将把解决方案迁移到 SQl Server 2008 R2(几个月后),所以我想创建一个通用的实现,即使在迁移之后也无需太多更改即可使用。

I am trying to connect to sybase ASE 15 and call a SP that does some DML. I was wondering if anyone has any pointers to a sybase helper class analogous to SQLhelper.cs or if someone has any pointers to any blog / sample code.

Eventually I will be migrating the solution to SQl Server 2008 R2 (couple of months from now) so I want to create a generic implementation which can be used without much change even after migration.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一张白纸 2024-09-23 12:07:57
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);
            }
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

}
}

Interface

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);
    }
}

--Calling it this way will invoke the expliciti garbage Collection

using (ISybaseDBHelper _DBHelper = ObjectFactory.GetDBHelper())
            {
                _DBHelper.InsertDataUsingStoredProcedure("usp_load_fx_spot_rate_into_staging", DatabaseEnum.Staging, input);
            }
策马西风 2024-09-23 12:07:57
  1. 如果您正在为 Sybase(或 MS)开发 SQL,您最好使用开发工具,例如 SQLProgrammer 或 Rapid SQL。它通过消除体力劳动来提高生产力。

  2. 如果你没有这个,那么Sybase附带的实用程序就足够了,但你需要学习使用目录。字符界面的 isql 或在 GUI 界面中提供命令和结果集窗口的 DBISQL。

  3. 在服务器上打开会话后,键入以下 SQL 命令。它们执行查询目录的系统存储过程:

exec sp_help (object_name) -- 包括表和存储过程

exec sp_helptext

有 30 个这样的询问。

我想您已经意识到存储过程可以做的不仅仅是简单的插入;处理各种数据类型作为参数,并且您发布的代码只是一个简单的示例,而不是完整的代码。一般来说,为了使存储过程能够从 Java 应用程序执行,我们需要提供一个包装器(在 SQL 端)。

  1. If you're developing SQL for Sybase (or MS) you are best off using a Developer tool, such as SQLProgrammer or Rapid SQL. It improves productivity by eliminating the manual labour.

  2. If you do not have that, then the utilities that come with Sybase are quite adequate, but you need to learn to use the catalogue. Either isql for a character interface or DBISQL which provides Command and ResultSet windows in a GUI interface.

  3. Once you have a session open on the server, type the following SQL commands. These execute system stored procedures that interrogate the catalogue:

exec sp_help (object_name) -- including tables and sprocs

exec sp_helptext

There are 30 such interrogatories.

I take it you realise that sprocs can do much more than simple INSERTS; handle various Datatypes as parameters, and that your posted code is only a simple example, not the full quid. Generally, to enable stored procs for execution from Java apps, we need to provide a wrapper (on the SQL side).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文