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 "迁移失败:表头不匹配";
            }
  • 相关阅读:
    第二十二节:类与对象后期静态绑定对象和引用
    WePHP的表单令牌验证
    第二十一节:类与对象对象的比较类型约束
    Windows下 C++ 实现匿名管道的读写操作
    Mongoose 利用实现HTTP服务
    C++ Qt 框架静态编译 操作记录
    使用Qt框架开发http服务器问题的记录
    把CMD下的color 方案遍历一遍
    C++学习笔记
    在1~10的整数范围随机5个不重复的整数
  • 原文地址:https://www.cnblogs.com/ilookbo/p/4155661.html
Copyright © 2011-2022 走看看