|  | CYQ.Data componentsCYQ.Data support multi-database application  [Txt,Xml,Access, MSSQL, Oracle,SQLite,MySql], help easily and quickly to develop your project | 
CYQ.Data V5 批量插入与批量更新示例
| Technology exchange |
										
										|
										
										| | #TopicOwner | 
| 最近有网友问了我 CYQ.Data 要怎么实现批量修改,于是我就给写了以下的示例: 示例为Winform应用,现实对DataGridView控件单元格的内容后,可以实现自动更新到数据库。 先上最终结果图:  接下上一张项目解决方案图:  由于是写Demo,所以我一般都选择用文本数据库做为示例。 我新建了一个Users.ts,内容为: ID,int; UserName,string; CreateTime,datetime; 意思就是表名为“Users”,字段为“ID,UserName,CreateTime”。 对应的标准型枚举文件TableNames.cs内容为:     /// <summary>     /// TableNames 的摘要说明     /// </summary>     public enum TableNames     {        Users,     }     public enum Users     {         ID, UserName, CreateTime     } 由于只是示例,可以用V5自带的工具生成,也可以自己新建手写了。 接下来先看一下部分代码: 首先定义一个全局的MDataTable字段:  public MDataTable table; 接下来写个Load函数,页面加载时显示下数据,然后绑定到列表控件:         private void LoadData()         {             using (MAction action = new MAction(TableNames.Users))             {                 table = action.Select();                 table.Bind(gvUsers);             }         } FormLoad的代码:         private void Form1_Load(object sender, EventArgs e)         {             LoadData();             if (table.Rows.Count < 100)             {                 //批量添加数据。                 //给table批量添加行。                 MDataRow row = null;                 for (int i = 0; i < 100; i++)                 {                     row = table.NewRow();                     row.Set(Users.UserName, "User:" + DateTime.Now.Millisecond);                     row.Set(Users.CreateTime, DateTime.Now);                     table.Rows.Add(row);                 }                 //调用表的批量添加。                 table.AcceptChanges(AcceptOp.Insert);                 //重新绑定到控件。                 table.Bind(gvUsers);             }         } 先绑定数据,这样table就不为Null了,如果一开始没数据,table也是有数据结构的。 判断如果数据少于100行,下面的几行代码就是批量插入了,然后重新绑定到控件。 如何进行量更新呢?  private void gvUsers_CellValueChanged(object sender, DataGridViewCellEventArgs e)         {             if (table.AcceptChanges(AcceptOp.Update))             {                 index++;                 labTip.Text = "数据已更新到数据库..." + index;             }             else             {                 labTip.Text = "数据更新失败";             }         } 方法很简单,只要处理单元格的列值改变状态就调用一下就可以了。 当然了了,由于是批量,你也可以把代码放到一个按钮里去,通过按钮点击再去确定指更新。 理论上,指量更新就到这里为止了,这里再补一点内容,就是列的中文显示: 通常我们查询后,列名都是英文的,但是系统多数是显示中文的,这里给出一种解决方案:         private void gvUsers_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)         {             FieldKeyValue.FormatHeaderText(gvUsers);         } 我们在控件的DataBindingComplete事件里,翻译一下列头就可以了。 这里我把它封装到一个FieldKeyValue类里,代码如下: public class FieldKeyValue     {         public static void FormatHeaderText(DataGridView gv)         {             for (int i = 0; i < gv.Columns.Count; i++)             {                 gv.Columns[i].HeaderText = GetName(gv.Columns[i].HeaderText);             }         }         public static string GetName(string key)         {             return GetName(key, null);         }         public static string GetName(string key, string tableName)         {             Dictionary<string, string> dictionary = GetFieldDescriptionDictionary();             if (dictionary != null)             {                 if (tableName != null && dictionary.ContainsKey(tableName + "_" + key))                 {                     return dictionary[tableName + "_" + key];                 }                 else if (dictionary.ContainsKey(key))                 {                     return dictionary[key];                 }             }             return key;         }         private static Dictionary<string, string> _Dic = null;         private static Dictionary<string, string> GetFieldDescriptionDictionary()         {             if (_Dic == null)             {                 _Dic = new Dictionary<string, string>();                 #region 字典添加                 _Dic.Add("ID", "编号");                 _Dic.Add("UserName", "用户名");                 _Dic.Add("CreateTime", "创建日期");                 #endregion             }             return _Dic;         } 只要添加字典对应关系就可以了,由于有时候不同的表名,相同字段可能有不同的翻译,所以字典是支持:  _Dic.Add("TableA_ID", "编号");  _Dic.Add("TableB_ID", "标识"); 该类会优先判断查询“表名_字段名”,如果找不到才找“字段名”。 最后提供Demo项目源码下载:  CYQ.Data_V5_Test_Win.rar 对于Winform的分页,可以下载我的Winform分页控件:C#Winform通用分页控件实战篇(提供源码下载)  | |
| 游客[注册][106.104.43.*]2013/9/18 1:22:27 | #1 | |
|  | 我想请问,关於app.config有什麽好的保密方法? reply: 不用app.config,可以把默认数据库链接写在代码里,对AppConfig.DB.DefaultConn赋值数据库链接也可以全局有效。 | |
Post Comment
Bulletin
Article Search
Categories
- Platform for dynamic (20)
- Feedback (9)
- Guide (33)
- Principles (19)
- Project-Case (8)
- Business & Buy (2)
- Technology exchange (45)
New Article
- CYQ.Data Components Getting Started Guide [Part 5]-[MProc Execute Stored Procedures or SQL]
- CYQ.Data Components Getting Started Guide [Part 4]-[MAction Insert Delete Update]
- CYQ.Data Components Getting Started Guide [Part 3]-[MAction Get And Set Value]
- CYQ.Data Components Getting Started Guide [Part 2]-[MAction Data Query- Fill And Select]
- CYQ.Data Components Getting Started Guide [Part 1]
New Comment
- When some one searches for his necessary thing, therefore he/she wishes to be available that in detail, so that thing is maintained over here.
- This is my first time pay a quick visit at here and i am in fact happy to read everthing at alone place.
- I truly appreciate this blog article.Really thank you! Cool.
- please pay a visit to the web sites we follow, like this one particular, as it represents our picks in the web
- Really enjoyed this post.Really thank you!
- Really enjoyed this article.Really looking forward to read more. Great.
- poker bonuses What are the norms of copyright of web content? How as it different from Patent?
- Wow! Thank you! I permanently needed to write on my blog something like that. Can I implement a fragment of your post to my site?
- This website was how do I say it? Relevant!! Finally I ave found something that helped me. Cheers!
- I was reading through some of your content on this internet site and I believe this web site is very informative ! Continue posting.

