在c中将数据从datatable复制到mysql表,引发致命异常

本文关键字:异常 mysql 数据 复制 datatable | 更新日期: 2023-09-27 18:21:29

我正在尝试将数据从sql server复制到mysql表,我已经创建了一个与sql server相同的mysql表。这是我的两张表的列名

EmpCode,员工姓名,在日期&时间过期日期&时间

这是我的可复制方法。我得到致命错误异常。在这里,我正在复制粘贴我在insertCmd.ExecuteNonQuery()之前得到的插入语句;

插入sms.empswipe(EmpCode,Employee name,In-Date&Time,Out-Date&aamp;Time)值(@EmpCode,@Employee-name,@In-Date&Time,@Out-Date∓Time)

它的给定异常{"必须定义参数'@Employee'。"}。它没有完全采用员工名称,只考虑第一个词。有人帮我吗

 public static void CopyTable(IDbConnection source, IDbConnection destination, String sourceSQL, String destinationTableName)
        {
            var cmd = source.CreateCommand();
            cmd.CommandText = sourceSQL;
            System.Diagnostics.Debug.WriteLine("'tSource SQL: " + sourceSQL);
            try
            {
                source.Open();
                destination.Open();
                var rdr = cmd.ExecuteReader();
                var schemaTable = rdr.GetSchemaTable();

                string paramsSQL = String.Empty;
                string paramsQuoted = String.Empty;
                var insertCmd = destination.CreateCommand();
                //build the insert statement
                foreach (DataRow row in schemaTable.Rows)
                {
                    if (paramsSQL.Length > 0) paramsSQL += ", ";
                    if (paramsQuoted.Length > 0) paramsQuoted += ", ";
                    paramsSQL += "@" + row["ColumnName"];
                    paramsQuoted += "[" + row["ColumnName"] + "]";
                    IDbDataParameter param = insertCmd.CreateParameter();
                    param.ParameterName = "@" + row["ColumnName"];
                    param.SourceColumn = row["ColumnName"].ToString();
                    if (ReferenceEquals(row["DataType"], typeof(DateTime)))
                    {
                        param.DbType = DbType.DateTime;
                    }
                    else if (ReferenceEquals(row["DataType"], typeof(Int32)))
                    {
                        param.DbType = DbType.Int32;
                    }
                    insertCmd.Parameters.Add(param);
                }
                insertCmd.CommandText = String.Format("insert into {0} ( {1} ) values ( {2} )",
                    destinationTableName, paramsSQL.Replace("@", String.Empty),paramsSQL);
                const int counter = 0;
                var errors = 0;
                while (rdr.Read())
                {
                    try
                    {
                        foreach (IDbDataParameter param in insertCmd.Parameters)
                        {
                            object col = rdr[param.SourceColumn];
                            //special check for SQL Server and 
                            //datetimes less than 1753
                            if (param.DbType == DbType.DateTime)
                            {
                                if (col != DBNull.Value)
                                {
                                    //sql server can not have dates less than 1753
                                    if (((DateTime)col).Year < 1753)
                                    {
                                        param.Value = DBNull.Value;
                                        continue;
                                    }
                                }
                            }
                            param.Value = col;
                        }
                        insertCmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        if (errors == 0)
                            System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
                        errors++;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                System.Diagnostics.Debug.WriteLine(ex);
            }
            finally
            {
                destination.Close();
                source.Close();
            }
        }

在c中将数据从datatable复制到mysql表,引发致命异常

您需要用"["例如[带空格的列名]转义列名。

请参阅以下答案:在SQL Server中,如何处理列名中的空格?

我还没有尝试过,但我认为这样的东西会起作用:

param.ParameterName = "@[" + row["ColumnName"]+"]";

尝试使用?而不是@Like

insert into sms.empswipe ( EmpCode,Employeename, InDate&Time, OutDate&Time ) values ( ?EmpCode, ?Employeename, ?InDate&Time, ?OutDate&Time )