csharp db2 sql查询使用c ## db2#c#

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp db2 sql查询使用c ## db2#c#相关的知识,希望对你有一定的参考价值。

using IBM.Data.DB2;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System;
using System.Reflection;

namespace ConsoleApp3
{
    class Program
    {


       static string connectionStringNew = "Server = 127.0.0.1:50000;Database = TESTB; UID = terefere ; PWD = dupskoblade";

        static void Main(string[] args)
        {
           

           var dt = LoadFromDB2(connectionStringNew, prepareQueryString());

           var list = ListFromDB2<Settings>(connectionStringNew, prepareQueryString());
        }



        private static string prepareQueryString()
        {
            return "select * from(                                                                                           " +
                   "     SELECT GRP,                                                                                         " +
                   "       MAX(CASE WHEN OPIS2 = 'ID konfiguracji'  THEN WART END) AS Id,                                    " +
                   "       MAX(CASE WHEN OPIS2 = 'Hasło do certyfikatu'   THEN WART END) AS HasloCertyfikatu,                " +
                   "       MAX(CASE WHEN OPIS2 = 'Nazwa pliku certyfikatu' THEN WART END) AS NazwaPlikuCertyfikatu,          " +
                   "       MAX(CASE WHEN OPIS2 = 'Nazwa użytkownika' THEN WART END) AS NazwaUzytkownika,                     " +
                   "       MAX(CASE WHEN OPIS2 = 'Hasło użytkownika' THEN WART END) AS HasloUzytkownika                      " +
                   "     FROM                                                                                                " +
                   "     (                                                                                                   " +
                   "     select SUB(OPIS, 1, INSTR(OPIS, ']', 1)) GRP, OPIS, TRIM(SUB(OPIS, INSTR(OPIS, ']', 1) + 1,         " +
                   " LENGTH(OPIS) - INSTR(OPIS, ']', 1))) OPIS2, WART from conf where lp = 700                               " +
                   "     )A                                                                                                  " +
                   "     where WART <> '' and WART is not null                                                               " +
                   "     group by GRP                                                                                        " +
                   "     )B                                                                                                  " +
                   "     where ID is not null;                                                                               ";
        }

        private static DataTable LoadFromDB2(string connectionString, string queryString)
        {
            var dt = new DataTable();
            using (DB2Connection connection = new DB2Connection(connectionString))
            {
                DB2Command command = new DB2Command(queryString, connection);
                //  command.Parameters.AddWithValue("@tPatSName", "Your-Parm-Value");
                connection.Open();
                DB2DataReader reader = command.ExecuteReader();
                try
                {
                    dt.Load(reader);
                }
                finally
                {
                    // Always call Close when done reading.
                    reader.Close();
                }
            }
            return dt;
        }

        private static List<T> ListFromDB2<T>(string connectionString, string queryString)
        {
            List<T> list = new List<T>();
            using (DB2Connection connection = new DB2Connection(connectionString))
            {
                DB2Command command = new DB2Command(queryString, connection);
                //  command.Parameters.AddWithValue("@tPatSName", "Your-Parm-Value");
                connection.Open();
                DB2DataReader reader = command.ExecuteReader();
                try
                {

                    T obj = default(T);
                    while (reader.Read())
                    {
                        obj = Activator.CreateInstance<T>();
                        foreach (PropertyInfo prop in obj.GetType().GetProperties())
                        {
                            if (!object.Equals(reader[prop.Name], DBNull.Value))
                            {
                                prop.SetValue(obj, reader[prop.Name], null);
                            }
                        }
                        list.Add(obj);
                    }
                }
                finally
                {

                    // Always call Close when done reading.
                    reader.Close();

                }
            }
            return list;
        }

    }

    class Settings
    {
        public string Id { get; set; }
        public string HasloCertyfikatu { get; set; }
        public string NazwaPlikuCertyfikatu { get; set; }
        public string NazwaUzytkownika { get; set; }
        public string HasloUzytkownika { get; set; }
    }
}

以上是关于csharp db2 sql查询使用c ## db2#c#的主要内容,如果未能解决你的问题,请参考以下文章

DB2 - C 嵌入式 SQL 不会插入一行

db2数据库怎么查询数据中含有特殊字符的

db2数据库查询所有用户的sql语句

db2 怎样查一个sql连接执行过的所有语句?

DB2 OLAP函数的使用

sqlserver 链接服务器 连接db2 查询速度慢