Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将表的列条目传输到另一个表的行的最佳方法_C#_Sql Server_Visual Studio 2010 - Fatal编程技术网

C# 将表的列条目传输到另一个表的行的最佳方法

C# 将表的列条目传输到另一个表的行的最佳方法,c#,sql-server,visual-studio-2010,C#,Sql Server,Visual Studio 2010,我必须编写一个程序,在VisualStudio2010C和SQLServer2012中,在上述限制下,将数据从一个表传输到另一个表。 问题是要找到一个高效的算法来实现这一点,因为表非常长 private static void fillRows() { //i have List<String> rows; // a list with table1 names (a,b,c,d,e...) List<String

我必须编写一个程序,在VisualStudio2010C和SQLServer2012中,在上述限制下,将数据从一个表传输到另一个表。 问题是要找到一个高效的算法来实现这一点,因为表非常长

    private static void fillRows()
    {
        //i have
        List<String> rows; // a list with table1 names (a,b,c,d,e...)
        List<String> columnList; //list with table2 columnNames (a#mm,b#cm,c#m...)

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (var command = connection.CreateCommand())
            {

            }
        }
    }

可以透视表或结果集


我不知道如何用参数化查询来处理它,但它是有效的

    private static void fillRows(List<String> columnList, String SOURCE_TABLE, String TARGET_TABLE, String COLUMN_NAME, String COLUMN_VALUE)
    {
        Console.WriteLine("Start filling");
        try
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string selectSql = "SELECT " + COLUMN_NAME + "," + COLUMN_VALUE + " FROM " + SOURCE_TABLE;

                SqlDataAdapter dataAdapter = new SqlDataAdapter(selectSql, connection);
                DataTable dataTable = new DataTable("cache");
                dataAdapter.Fill(dataTable);

                //Set ID
                int ID = 1;
                string insertSql = "INSERT INTO " + TARGET_TABLE + " VALUES ('" + ID + "',";
                StringBuilder sbInsertSQL = new StringBuilder();
                sbInsertSQL.Append(insertSql);


                int count = 0;
                foreach (DataRow row in dataTable.Rows)
                {
                    if (count == columnList.Count)
                    {
                        count = 0;
                        sbInsertSQL.Length--; //remove ','
                        sbInsertSQL.Append(");"); //complete command

                        SqlCommand sqlCmd = new SqlCommand(sbInsertSQL.ToString(), connection);

                        sqlCmd.ExecuteNonQuery(); //Insert row

                        //reset 
                        sbInsertSQL.Clear();
                        ID++;
                        sbInsertSQL.Append("INSERT INTO " + TARGET_TABLE + " VALUES ('" + ID + "',");
                    }

                    //make sure, connecting correct
                    if (row[0].ToString().Equals(columnList[count].Split('#')[0])) //split off the unit
                    {
                        sbInsertSQL.Append("'" + tools.withoutUnit(row[1].ToString()) + "',");
                        count++;
                    }      
                }
                connection.Close();
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex);
            Console.ReadLine();
        }
    }

我对列名是如何生成的有点困惑。。在表2中,它们根据表1中的计量单位动态生成名称,这样说对吗?@jonny是的,它的名称+单位给我五分钟:\n好主意,但我不能使用它,因为我需要一个c程序,我给定的值只是一个例子。正如前面所说,我有一个非常大的表,其中的列要多得多。为什么不能在c程序中使用Command.ExecuteOnQuery的代码呢?如果有更多列,会出现什么问题?这不像复制/粘贴/编辑最大行需要很多时间。是的,这将需要很多时间,尤其是对于所有表。它必须是动态的
    private static void fillRows(List<String> columnList, String SOURCE_TABLE, String TARGET_TABLE, String COLUMN_NAME, String COLUMN_VALUE)
    {
        Console.WriteLine("Start filling");
        try
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string selectSql = "SELECT " + COLUMN_NAME + "," + COLUMN_VALUE + " FROM " + SOURCE_TABLE;

                SqlDataAdapter dataAdapter = new SqlDataAdapter(selectSql, connection);
                DataTable dataTable = new DataTable("cache");
                dataAdapter.Fill(dataTable);

                //Set ID
                int ID = 1;
                string insertSql = "INSERT INTO " + TARGET_TABLE + " VALUES ('" + ID + "',";
                StringBuilder sbInsertSQL = new StringBuilder();
                sbInsertSQL.Append(insertSql);


                int count = 0;
                foreach (DataRow row in dataTable.Rows)
                {
                    if (count == columnList.Count)
                    {
                        count = 0;
                        sbInsertSQL.Length--; //remove ','
                        sbInsertSQL.Append(");"); //complete command

                        SqlCommand sqlCmd = new SqlCommand(sbInsertSQL.ToString(), connection);

                        sqlCmd.ExecuteNonQuery(); //Insert row

                        //reset 
                        sbInsertSQL.Clear();
                        ID++;
                        sbInsertSQL.Append("INSERT INTO " + TARGET_TABLE + " VALUES ('" + ID + "',");
                    }

                    //make sure, connecting correct
                    if (row[0].ToString().Equals(columnList[count].Split('#')[0])) //split off the unit
                    {
                        sbInsertSQL.Append("'" + tools.withoutUnit(row[1].ToString()) + "',");
                        count++;
                    }      
                }
                connection.Close();
            }
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex);
            Console.ReadLine();
        }
    }