0

在这段代码中,我在读取我的数据库阅读器从查询中获得的列时出错。错误在行中:

DbCom.CommandText = System.String.Format("INSERT into Cortex(IdDA,Vlsd) values({0},'yes')",DbReader.GetInt16(0));

他们说在 ligne 或 colomn 中不存在数据,但它确实存在。

public static void Main(string[] args)
        {
            using (OdbcConnection DbConnection = new OdbcConnection("DSN=savior"))
            {
                DbConnection.Open();
                OdbcCommand DbCommand = DbConnection.CreateCommand();
                DbCommand.CommandText = "SELECT IDA FROM Dchas";
                try
                {
                    DbReader = DbCommand.ExecuteReader();
                    int fCount = DbReader.FieldCount;
                    if (fCount > 0)
                    {
                        do
                        {

                            using (OdbcConnection DbConnect = new OdbcConnection("DSN=savior"))
                            {
                                DbConnect.Open();
                                OdbcCommand DbCom = DbConnect.CreateCommand();
                                DbCom.CommandText = System.String.Format("INSERT into Cortex(IdDA,Vlsd) values({0},'yes')",DbReader.GetInt16(0));

                                try
                                {
                                    DbCom.ExecuteNonQuery();
                                }
                                catch (OdbcException ex)
                                {
                                    Console.WriteLine("Executing the query2 failed.");
                                    Console.WriteLine("The OdbcCommand returned the following message");
                                    Console.WriteLine(ex.ToString());
                                    return;
                                }
                            }

                        } while (DbReader.Read());
                    }
                    else
                    {
                        Console.WriteLine("Query affected row(s)");
                        return;
                    }
                }
                catch (OdbcException ex)
                {
                    Console.WriteLine("Executing the query1 failed.");
                    Console.WriteLine("The OdbcCommand returned the following message");
                    Console.WriteLine(ex.ToString());
                    return;
                }
            }
        }
4

2 回答 2

1

在尝试访问 DBReader 数据之前,您do..while loop无需调用即可输入您的。DBReader.Read

最好的解决方法是将循环更改为

while(DBReader.Read())
{
    .....

}

如果 Reader 查询不返回任何记录,这将避免进入循环

于 2013-05-19T10:29:01.773 回答
0

Please note that you can save a lot of code (and a lot of potential errors like that one) if you use the features the database already supplies. Copying records from one table to the next is something you should let the database worry about:

    public static void Main(string[] args)
    {
        using (var connection = new OdbcConnection("DSN=savior"))
        {
            const string Sql = "INSERT INTO Cortex( IdDA, Vlsd ) SELECT IDA, 'yes' FROM Dchas";

            connection.Open();

            using(var command = new OdbcCommand(Sql))
            {
                int numberOfRecords = command.ExecuteNonQuery();
                Console.WriteLine("Copied {0} rows.", numberOfRecords);
            }
        }
     }
于 2013-05-19T13:37:03.363 回答