C# ACCESS的SQL添加数据操作封装类AccessSqlIn

C# ACCESS的SQL添加数据操作封装类AccessSqlIn

找了很久,都没有找到C# 的Access数据添加操作封装的类,又不想用添加别人的lib库,只好自己封装了一个。
该类采用简化设计,没有异常处理,只是生成SQL时会判断下条件,如果条件不符就会返回空字符,要自己写判断条件就行了。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace kqgz.Model
{
    class AccessSqlIn
    {
        private string _tbname = "";
        public string TbName { get => _tbname; set => _tbname = value; }

        private Hashtable slist = new Hashtable();        
        public void AddString(string key,string value)
        {
            if(slist.ContainsKey(key))
            {
                slist.Remove(key);
            }

            slist.Add(key, value);
        }

        private Hashtable dlist = new Hashtable();
        public void AddDate(string key,string value)
        {
            if (dlist.ContainsKey(key))
            {
                dlist.Remove(key);
            }

            dlist.Add(key, value);
        }

        private Hashtable nlist = new Hashtable();
        public void AddNull(string key)
        {
            if (nlist.ContainsKey(key))
            {
                nlist.Remove(key);
            }

            nlist.Add(key, null);
        }

        private Hashtable ilist = new Hashtable();
        public void AddInt(string key, int value)
        {
            if (ilist.ContainsKey(key))
            {
                ilist.Remove(key);
            }

            ilist.Add(key, value);
        }

        public string GetInsertSql()
        {
            string sql = "";

            if(_tbname == "")
            {
                return "";
            }

            int slen = slist.Count;
            int dlen = dlist.Count;
            int nlen = nlist.Count;

            if(slen == 0 && dlen ==0 && nlen == 0)
            {
                return "";
            }

            sql = "insert into {0}({1}) values({2})";

            sql = sql.Replace("{0}", _tbname);

            string liestr = "";
            string vstr = "";

            //string
            foreach (DictionaryEntry de in slist) 
            {
                if(liestr == "")
                {
                    liestr = de.Key.ToString();
                }
                else
                {
                    liestr += "," + de.Key.ToString();
                }

                if (vstr == "")
                {
                    vstr = "'" + de.Value.ToString() + "'";
                }
                else
                {
                    vstr += ",'" + de.Value.ToString() + "'";
                }

            }

            //date
            foreach (DictionaryEntry de in dlist)
            {
                if (liestr == "")
                {
                    liestr = de.Key.ToString();
                }
                else
                {
                    liestr += "," + de.Key.ToString();
                }

                if (vstr == "")
                {
                    vstr = "#" + de.Value.ToString() + "#";
                }
                else
                {
                    vstr += ",#" + de.Value.ToString() + "#";
                }
            }

            //null
            foreach (DictionaryEntry de in nlist)
            {
                if (liestr == "")
                {
                    liestr = de.Key.ToString();
                }
                else
                {
                    liestr += "," + de.Key.ToString();
                }

                if (vstr == "")
                {
                    vstr = "null";
                }
                else
                {
                    vstr += ",null";
                }
            }

            //int
            foreach (DictionaryEntry de in ilist)
            {
                if (liestr == "")
                {
                    liestr = de.Key.ToString();
                }
                else
                {
                    liestr += "," + de.Key.ToString();
                }

                if (vstr == "")
                {
                    vstr = Convert.ToString(de.Value);
                }
                else
                {
                    vstr += "," + Convert.ToString(de.Value);
                }
            }

            sql = sql.Replace("{1}",liestr);
            sql = sql.Replace("{2}",vstr);

            return sql;
        }

        public void ResetData()
        {
            slist.Clear();
            dlist.Clear();
            nlist.Clear();
            ilist.Clear();
        }

        public void ResetTable()
        {
            TbName = "";
        }

    }
}

一边判断条件一边添加字段就行了,多方便,从此过上幸福的日子,不需要对着一长串insert语句发傻,不担心字段和数值错位。
我只封装了自己用到的数据类型,float、double那些没有封装,需要其他类型的话可以自己照样子添加就行了。

使用方法如下:

AccessSqlIn sqlin = new AccessSqlIn();
sqlin.TbName = "userinfo";
//字符串
sqlin.AddString("uno",ghstr);
//日期
string rzstr = dateTimePicker1.Value.ToString("yyyy-M-d");
sqlin.AddDate("uinday", rzstr);
//整型
string jtstr = tbjintie.Text.Trim();
int jt = Convert.ToInt32(jtstr);
sqlin.AddInt("ujt", jt);

//null
sqlin.AddNull("ulfday");

string sql = sqlin.GetInsertSql();
if(sql == "")
{
    MessageBox.Show("SQL出现异常", "提示");
    return;
}

添加成功后,调用ResetData(),又可以继续添加下一条记录,真过瘾。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XINGTECODE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值