zoukankan      html  css  js  c++  java
  • C# DataGridView插入DB

            public static bool ContrastColumns(DataColumnCollection co1, DataGridViewColumnCollection co2)
            {
                bool aa = false;
                if (co1.Count == co2.Count)
                {
                    for (int i = 0; i < co1.Count; i++)
                    {
                        if (co1[i].Caption != co2[i].Name)
                        {
                            aa = false;
                            break;
                        }
                        else
                        {
                            aa = true;
                        }
                    }
                    return aa;
                }
                else
                    return false;
            }
    
            public static string DataGridViewToDB(DataGridView DGV, string connstr, string Tablestr)
            {
                int aa = 0;
                string mycmd = "select * from " + Tablestr;
                DataTable dt = SqlHelper.SQLCommand<DataTable>(mycmd, connstr);
    
                if (ContrastColumns(dt.Columns, DGV.Columns))
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("insert into " + Tablestr + "(");
    
                    for (int i = 0; i < DGV.ColumnCount; i++)
                    {
                        if (i == DGV.ColumnCount - 1)
                        {
                            sb.Append(DGV.Columns[i].Name + ") values (");
                        }
                        else
                        {
                            sb.Append(DGV.Columns[i].Name + ",");
                        }
                    }
    
                    foreach (DataGridViewRow dgvRow in DGV.Rows)
                    {
                        StringBuilder mysb = new StringBuilder();
                        mysb.Append("select * from " + Tablestr + " where ");
    
                        StringBuilder sb2 = new StringBuilder();
                        for (int i = 0; i < dgvRow.Cells.Count; i++)
                        {
                            if (i == 0)
                            {
                                mysb.Append(DGV.Columns[i].Name + "='" + dgvRow.Cells[i].Value + "'");
                            }
                            else
                            {
                                mysb.Append(" and " + DGV.Columns[i].Name + "='" + dgvRow.Cells[i].Value + "'");
                            }
    
                            if (i == dgvRow.Cells.Count - 1)
                            {
                                sb2.Append("'" + dgvRow.Cells[i].Value + "')");
                            }
                            else
                            {
                                sb2.Append("'" + dgvRow.Cells[i].Value + "',");
                            }
                        }
                        string sqlcmd = sb.ToString() + sb2.ToString();
    
                        try
                        {
                            DataTable mydt = SqlHelper.SQLCommand<DataTable>(mysb.ToString(), connstr);
                            if (mydt.Rows.Count == 0)
                            {
                                SqlHelper.SQLCommand<int>(sqlcmd, connstr);
                                aa++;
                            }
                        }
                        catch (Exception ee)
                        {
                            ee.ToString();
                        }
                    }
                    return "迁移成功:共" + aa.ToString() + "条记录";
                }
                else
                    return "迁移失败:表头不匹配";
            }
  • 相关阅读:
    寒假练习集中贴
    7-49 打印学生选课清单 (25分)
    7-47 打印选课学生名单 (25分)
    进阶实验5-3.3 基于词频的文件相似度 (30分)-哈希
    进阶实验5-3.4 迷你搜索引擎 (35分)-哈希
    7-24 树种统计 (25分)-二叉排序树or快速排序
    7-25 朋友圈 (25分)-并查集
    进阶实验6-3.4 拯救007(升级版) (30分)-BFS
    基础实验6-2.3 拯救007 (25分)-DFS
    进阶实验4-3.5 哈夫曼编码 (30分)-最优二叉树
  • 原文地址:https://www.cnblogs.com/ilookbo/p/4155661.html
Copyright © 2011-2022 走看看