Files
Groupware/SubProject/FCOMMON/DataBaseManager.cs
chikyun.kim 327adb3779 10-01
2018-10-02 08:24:16 +09:00

167 lines
5.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FCOMMON
{
public static class DBM
{
public static System.Data.SqlClient.SqlConnection getCn()
{
string cs = FCOMMON.info.CS;
System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection();
cn.ConnectionString = cs;
return cn;
}
public struct sItemInfo
{
public int idx;
public string sid;
public string model;
public string supply;
}
public static sItemInfo getItemInfo(int idx)
{
var cn = getCn();
cn.Open();
var retval = new sItemInfo();
retval.idx = -1;
string sql = "select * from Items where idx = " + idx.ToString();
var cmd = new System.Data.SqlClient.SqlCommand(sql, cn);
var rdr = cmd.ExecuteReader();
while(rdr.Read())
{
retval.idx = (int)rdr["idx"];
if(rdr["sid"] != DBNull.Value) retval.sid = rdr["sid"].ToString();
if (rdr["model"] != DBNull.Value) retval.model = rdr["model"].ToString();
if (rdr["supply"] != DBNull.Value) retval.supply = rdr["supply"].ToString();
}
cn.Close();
cn.Dispose();
return retval;
}
public static Dictionary<string,string> getUserList(int baseLevel = 1)
{
string where = "isnull(level,0) >= " + baseLevel.ToString();
return getTwoColumnList("Users", "id", "name", where, "name");
}
public static System.Data.DataTable getUserTable(int baseLevel = 1)
{
var list = getUserList(baseLevel);
return MakeDataTable(list);
}
private static Dictionary<string,string> MakeDataTable(List<string> list)
{
var retval = new Dictionary<string, string>();
foreach (var item in list)
retval.Add(item, item);
return retval;
}
private static System.Data.DataTable MakeDataTable(Dictionary<string,string> list)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("Key");
dt.Columns.Add("Value");
dt.Columns.Add("KeyValue");
foreach (var item in list)
{
dt.Rows.Add(new string[] { item.Key, item.Value, string.Format("[{0}] {1}", item.Key, item.Value) });
}
dt.AcceptChanges();
return dt;
}
public static Dictionary<string, string> getProjectList(string StateCode = "")
{
string where = "status = '{0}'";
if (StateCode != "") where = string.Format(where, StateCode);
else where = string.Empty;
return getTwoColumnList("Projects", "idx", "name", where, "status,name");
}
public static System.Data.DataTable getProjectData(string StateCode = "")
{
var list = getProjectList(StateCode);
return MakeDataTable(list);
}
public static Dictionary<string,string> getCodeList(string GroupCode="99")
{
string where = "Grp = '{0}'";
where = string.Format(where, GroupCode);
return getTwoColumnList("Common", "code", "memo", where, "code");
}
public static System.Data.DataTable getCodeTable(string GroupCode = "99")
{
var list = getCodeList(GroupCode);
return MakeDataTable(list);
}
private static Dictionary<string, string> getTwoColumnList(string table,string col1,string col2,string where="",string order="")
{
Dictionary<string, string> retval = new Dictionary<string, string>();
var cn = getCn();
cn.Open();
var sql = "select isnull({1},''),isnull({2},'')" +
" from {0}";
if (where != "") sql += " where " + where;
if (order != "") sql += " order by " + order;
sql = string.Format(sql,table, col1, col2);
var cmd = new System.Data.SqlClient.SqlCommand(sql, cn);
var rdr = cmd.ExecuteReader();
while (rdr.Read())
{
retval.Add(rdr[0].ToString(), rdr[1].ToString());
}
cmd.Dispose();
cn.Close();
cn.Dispose();
return retval;
}
public static Dictionary<string,string> getGroupTable(string GroupColumn, string table, string where = "", Boolean desc = false)
{
var list = getGroupList(GroupColumn, table, where, desc);
return MakeDataTable(list);
}
public static List<String> getGroupList(string GroupColumn, string table, string where = "",Boolean desc=false)
{
List<string> retval = new List<string>();
var cn = getCn();
cn.Open();
var sql = "select {0} " +
" from {1} " +
" where isnull({0},'') != '' ";
if (where != "") sql += " and " + where;
sql += " group by {0} " +
" order by {0} ";
if (desc) sql += " desc";
sql = string.Format(sql, "[" + GroupColumn + "]", table);
var cmd = new System.Data.SqlClient.SqlCommand(sql, cn);
var rdr = cmd.ExecuteReader();
while (rdr.Read())
{
retval.Add(rdr[0].ToString());
}
cmd.Dispose();
cn.Close();
cn.Dispose();
return retval;
}
public static List<String> getDateList(string table, string where = "",Boolean desc =false)
{
return getGroupList("pdate", table, where,desc);
}
}
}