简体   繁体   English

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

[英]copy data from datatable to mysql table in c# throwing fatal exception

I am trying to copy data from sql server to mysql table i have created a mysql table same as sql server. 我试图将数据从sql server复制到mysql表,我创建了一个与sql server相同的mysql表。 Here is my column name of my both table 这是我两个表的列名

EmpCode, Employee name, In Date & Time, Out Date & Time EmpCode,员工姓名,入职日期和时间,出职日期和时间

Here is my copytable method. 这是我的可复制方法。 I am getting fatal error exception. 我收到致命错误异常。 Here with i am copy pasting my insert statement i am getting before insertCmd.ExecuteNonQuery(); 我在这里复制粘贴我的插入语句,我在insertCmd.ExecuteNonQuery();之前获得;

insert into sms.empswipe ( EmpCode, Employee name, In Date & Time, Out Date & Time ) values ( @EmpCode, @Employee name, @In Date & Time, @Out Date & Time ) 插入sms.empswipe(EmpCode,雇员姓名,In日期和时间,Out日期和时间)值(@EmpCode,@Employee名称,@In日期和时间,@Out日期和时间)

Its giving exception {"Parameter '@Employee' must be defined."}. 它的给定异常{“必须定义参数'@Employee'。”}。 Its not taking employee name fully, only first word its considering. 它并没有充分体现员工的名字,只是首先考虑一下。 Anybody help me 有人帮我

 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();
            }
        }

You need to escape your column names with "[" eg [column name with spaces]. 您需要使用“ [”对列名进行转义,例如[带有空格的列名]。

See this answer: How do you deal with blank spaces in column names in SQL Server? 看到这个答案: 如何处理SQL Server中列名中的空格?

I haven't tried this but I think something like this would work: 我还没有尝试过,但是我认为这样会起作用:

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

Try with ? 试试看? instead of @ Like 而不是@ Like

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM