Index 扉へ 首頁
人類の品種・宇宙人・系外銀河など
C#でデータベースの操作をしやすくしてくれるライブラリです
0   商湯      2026年7月26日 日 7:38
これではデータベースは操作しやすくなってくれます

using System;
using System.Collections.Generic;
using System.Web;
using System.Data.Odbc;
using System.Text;

public class SqlParam
{
   public OdbcType Type;
   public object Value;

   private SqlParam() { }

   public SqlParam(OdbcType type, object value)
   {
       Type = type;
       Value = value;
   }

   public static SqlCmd operator +(SqlParam a, SqlParam b)
   {
       return (SqlCmd)a + b;
   }
   
   public static SqlCmd operator +(SqlParam a, string b)
   {
       return (SqlCmd)a + b;
   }
   
   public static SqlCmd operator +(SqlParam a, char b)
   {
       return (SqlCmd)a + b;
   }

   public static SqlCmd operator +(string a, SqlParam b)
   {
       return (SqlCmd)a + b;
   }

   public static SqlCmd operator +(char a, SqlParam b)
   {
       return (SqlCmd)a + b;
   }
}

public class SqlCmd
{
   private List<object> Cmd = new List<object>();

   private SqlCmd() { }

   public static implicit operator SqlCmd(string x)
   {
       if (x == null)
           return null;

       SqlCmd ret = new SqlCmd();

       ret.Cmd.Add(x);

       return ret;
   }

   public static implicit operator SqlCmd(char x)
   {
       SqlCmd ret = new SqlCmd();

       ret.Cmd.Add(x);

       return ret;
   }

   public static implicit operator SqlCmd(SqlParam x)
   {
       if (x == null)
           return null;

       SqlCmd ret = new SqlCmd();

       ret.Cmd.Add(x);

       return ret;
   }

   public static SqlCmd operator +(SqlCmd a, SqlCmd b)
   {
       if (a == null)
           return b;

       if (b == null)
           return a;

       SqlCmd ret = new SqlCmd();

       ret.Cmd.AddRange(a.Cmd);
       ret.Cmd.AddRange(b.Cmd);

       return ret;
   }

   public OdbcCommand SetCommand(OdbcCommand cmd)
   {
       cmd.CommandText = TakakuG.For(0, Cmd.Count - 1, new StringBuilder(),
           (sum, i) =>
           {
               if (Cmd[i] is string || Cmd[i] is char) return sum.Append(Cmd[i]);
               else if (Cmd[i] is SqlParam) return sum.Append('?');
               else throw new Exception();
           }).ToString();

       cmd.Parameters.Clear();

       foreach (object i in Cmd)
           if (i is SqlParam)
               cmd.Parameters.Add("", ((SqlParam)i).Type).Value = ((SqlParam)i).Value;

       return cmd;
   }

   public void ExecNQ(OdbcConnection conn)
   {
       SetCommand(conn.CreateCommand()).ExecuteNonQuery();
   }

   public object ExecS(OdbcConnection conn)
   {
       return SetCommand(conn.CreateCommand()).ExecuteScalar();
   }

   public OdbcDataReader ExecR(OdbcConnection conn)
   {
       return SetCommand(conn.CreateCommand()).ExecuteReader();
   }
}

そこに用いられているFor関数の実装は下記のとおりです:
using System;
using System.Collections;
using System.Collections.Generic;

public static partial class TakakuG
{
   public delegate T _For_FAdd<T>(T a, T b);
   public delegate T _For_FElem<I, T>(I i);
   public delegate T _For_FAddElem<T, I>(T sum, I i);

   public static T For<T>(int from, int to, _For_FAdd<T> fAdd, _For_FElem<int, T> fElem)
   {
       if (from > to)
           throw new Exception();

       T ret = fElem(from);

       for (int i = from + 1; i <= to; i++)
           ret = fAdd(ret, fElem(i));

       return ret;
   }

   public static T For<T>(int from, int to, T zero, _For_FAdd<T> fAdd, _For_FElem<int, T> fElem)
   {
       if (from > to)
           return zero;

       T ret = fElem(from);

       for (int i = from + 1; i <= to; i++)
           ret = fAdd(ret, fElem(i));

       return ret;
   }

   public static T For<T>(int from, int to, T sum0, _For_FAddElem<T, int> fAddElem)
   {
       T ret = sum0;

       for (int i = from; i <= to; i++)
           ret = fAddElem(ret, i);

       return ret;
   }

   public static T For<I, T>(IEnumerable<I> _in, _For_FAdd<T> fAdd, _For_FElem<I, T> fElem)
   {
       IEnumerator<I> i = _in.GetEnumerator();

       if (!i.MoveNext())
           throw new Exception();

       T ret = fElem(i.Current);

       while (i.MoveNext())
           ret = fAdd(ret, fElem(i.Current));

       return ret;
   }

   public static T For<I, T>(IEnumerable<I> _in, T zero, _For_FAdd<T> fAdd, _For_FElem<I, T> fElem)
   {
       IEnumerator<I> i = _in.GetEnumerator();

       if (!i.MoveNext())
           return zero;

       T ret = fElem(i.Current);

       while (i.MoveNext())
           ret = fAdd(ret, fElem(i.Current));

       return ret;
   }

   public static T For<I, T>(IEnumerable<I> _in, T sum0, _For_FAddElem<T, I> fAddElem)
   {
       T ret = sum0;

       foreach (I i in _in)
           ret = fAddElem(ret, i);

       return ret;
   }

   public static T For<T>(IEnumerable I, _For_FAdd<T> fAdd, _For_FElem<object, T> fElem)
   {
       IEnumerator i = I.GetEnumerator();

       if (!i.MoveNext())
           throw new Exception();

       T ret = fElem(i.Current);

       while (i.MoveNext())
           ret = fAdd(ret, fElem(i.Current));

       return ret;
   }

   public static T For<T>(IEnumerable I, T zero, _For_FAdd<T> fAdd, _For_FElem<object, T> fElem)
   {
       IEnumerator i = I.GetEnumerator();

       if (!i.MoveNext())
           return zero;

       T ret = fElem(i.Current);

       while (i.MoveNext())
           ret = fAdd(ret, fElem(i.Current));

       return ret;
   }

   public static T For<T>(IEnumerable I, T sum0, _For_FAddElem<T, object> fAddElem)
   {
       T ret = sum0;

       foreach (object i in I)
           ret = fAddElem(ret, i);

       return ret;
   }
}


使用の例です:

("insert into tbl_posts_cache values ("
   + newOrder.ToString() + ','
   + new SqlParam(OdbcType.Binary, guid.ToByteArray()) + ','
   + new SqlParam(OdbcType.VarBinary, Encoding.UTF8.GetBytes(G.SearchNormalize(userName))) + ','
   + "0, "
   + "'" + postDateTime.ToString("yyyy/MM/dd HH:mm:ss") + "', "
   + TakakuG.For(0, 3, (SqlCmd)"",
       (sum, i) => sum + (SqlCmd)new SqlParam(OdbcType.VarBinary,
           Encoding.UTF8.GetBytes(i < locationEn.Count ? G.SearchNormalize(locationEn[i]) : "")) + ',')
   + TakakuG.For(0, 3, (SqlCmd)"",
       (sum, i) => sum + (SqlCmd)new SqlParam(OdbcType.VarBinary,
           Encoding.UTF8.GetBytes(i < location.Count ? G.SearchNormalize(location[i]) : "")) + ',')
   + date.ToBinary().ToString() + ','
   + new SqlParam(OdbcType.VarBinary, Encoding.UTF8.GetBytes(G.SearchNormalize(placeEn))) + ','
   + new SqlParam(OdbcType.VarBinary, Encoding.UTF8.GetBytes(G.SearchNormalize(place))) + ','
   + ((byte)ageFrom).ToString() + ','
   + ((byte)ageTo).ToString() + ','
   + new SqlParam(OdbcType.VarBinary, Encoding.UTF8.GetBytes(G.SearchNormalize(languageEn[0]))) + ','
   + new SqlParam(OdbcType.VarBinary, Encoding.UTF8.GetBytes(languageEn.Count > 1 ? G.SearchNormalize(languageEn[1]) : "")) + ','
   + new SqlParam(OdbcType.VarBinary, Encoding.UTF8.GetBytes(G.SearchNormalize(language[0]))) + ','
   + new SqlParam(OdbcType.VarBinary, Encoding.UTF8.GetBytes(language.Count > 1 ? G.SearchNormalize(language[1]) : "")) + ','
   + fileDesc.ID.ToString()
   + ')').ExecNQ(conn);
名前      編集用パスワード(省略可)    パスワードの記憶
数式 αβγ IPA http:// 画像 B I U 上 下
(添付ファイルは http://www.jkofsp.net/up/ にアップロードしてください)