使用通用字典中插入值表 [英] Inserting values to a table using generic dictionary

查看:209
本文介绍了使用通用字典中插入值表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编码平台:ASP.NET 2.0的WebForms用C#与MySQL作为后端

背景

我目前的工作在固定网站的错误。结果
一表登记的有80列。

插入/更新是通过简单的SQL语句不带任何参数化查询完成。

问题

目前登记时,用户可以改变导致ATLEAST15种​​INSERT查询的许多参数。我的问题是如何确保所有字段被插入它们正确的值。

所以,我创建了一个

 词典<字符串,字符串>域=新词典<字符串,字符串>();fields.Add(注册邮箱,MySQL.SingleQuoteSQL(txtLoginEmail.Text));
fields.Add(密码,MySQL.SingleQuoteSQL(txtLoginPassword.Text));fields.Add(联系人姓名,MySQL.SingleQuoteSQL(txtContactName.Text));
fields.Add(城市,MySQL.SingleQuoteSQL(txtCity.Text));

我的想法是做一个简单的插入查询像这样

  INSERT INTO登记(全键作为逗号分隔字符串)VALUES(全键作为逗号分隔字符串)

我的问题是


  1. 是字典最佳的数据结构来实现这一点?

  2. 键是否由通用词典排序的查询修改键值指数?

  3. 最好的方法来获取所有键到一个数组和相应的值到另一个阵列匹配。

和也,有什么其他更好的方法?

P.S:我保持code和制作实体类映射列属性和存储的值不在此选项


解决方案

 字符串列表< T>(IEnumerable的< T>枚举)
 {
   清单< T>名单=新名单< T>(枚举);
   返回的string.join(,,list.ToArray());
 }// ...
字符串SQL =的String.Format(INSERT INTO注册({0})VALUES({1}),
                列表(fields.Keys)
                列表(fields.Values​​));

Coding Platform: ASP.NET 2.0 WebForms with C# with MySQL as backend

Background

I am currently working on a bug fixing a website.
One of the table "registrations" have 80 columns.

The Insert/ Update is done through simple sql statements without any parameterized queries.

Problem

At registration, the user can vary many parameters resulting in atleast 15 kinds of INSERT query. My problem is how to ensure all fields are inserted with their correct value.

So, I created a

Dictionary<string, string> fields = new Dictionary<string, string>();

fields.Add("LoginEmail", MySQL.SingleQuoteSQL(txtLoginEmail.Text));
fields.Add("Password", MySQL.SingleQuoteSQL(txtLoginPassword.Text));

fields.Add("ContactName", MySQL.SingleQuoteSQL(txtContactName.Text));
fields.Add("City", MySQL.SingleQuoteSQL(txtCity.Text));

My idea was to make a simple insert query like this

INSERT INTO registrations("all keys as comma separated string") VALUES ("all keys as comma separated string") 

My questions are

  1. Is Dictionary the best data structure to implement this?
  2. Does the sorting of keys by generic Dictionary changes key-value indices at the query?
  3. Best method to fetch all keys into an array and the corresponding values to another matching array.

And also, what are the other better approaches?

P.S: I am maintaining the code and Making an Entity Class mapping the columns to properties and storing the values is not an option in this.

解决方案

 string list<T>(IEnumerable<T> enumerable)
 {
   List<T> list = new List<T>(enumerable);
   return string.Join(",", list.ToArray());
 } 

//...
string sql= String.Format("INSERT INTO registrations({0}) VALUES({1})",
                list(fields.Keys),
                list(fields.Values));

这篇关于使用通用字典中插入值表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆