2016-03-16 24 views
0

因此,我正在编写一个数据导出,在SQL和Foxpro之间,我使用参数化的字符串以便通过必要的数据,我还使用字符串连接来获取某些请求的信息记录,这似乎工作正常,直到我到达排序代码记录,其中所述的错误变得普遍。为什么我得到这个错误?如何处理空值。消息=必须指定其他参数

作为一个快速破败。建立到两个数据库的连接。

static void Main(string[] args) 
    { 
     using (var exportConnection = new SqlConnection(connectionString: "Data Source=Localhost;" 
                      + "Trusted_Connection=yes;" 
                      + @"connection timeout=30;" 
                      + @"database=001-CARL_V4")) 
     using (var importConnection = new OleDbConnection(connectionString: @"Provider=vfpoledb.1;" 
                      + 
                      @"data source=C:\Users\Joshua.cameron\Desktop\PCHomesImportTestBlank\PCHomesServer\DATABASE\pchomes.dbc") 
      ) 

我设立的命令从SQL选择:

  using (SqlCommand exportCommand = new SqlCommand(@"select 
    [ID] 
    ,[OwTitle] 
    ,[OwForenames] 
    ,[OwSurname]  
    ,[OwNum] 
    ,[OwAddress1] 
    ,[OwAddress2] 
    ,[OwAddress3] 
    ,[OwAddress4] 
    ,[OwPostcode] 
    ,[OwPhHome] 
    ,[OwPhWork] 
    ,[OwPhFax] 
    ,[OwPhMobile] 
    ,[OwEMail] 
    ,[OwBankName] 
    ,[OwAccNo] 
    ,[OwAccName] 
    ,[OwSalutation] 
    ,[OwNotes] 
    ,[OwSort1] 
    ,[OwSort2] 
    ,[OwSort3] 

从dbo.CARL_Owners”,exportConnection))

和命令插入到VFP:

using (OleDbCommand importCommand = new OleDbCommand(@"INSERT INTO CLIENT 
    (CLCODE 
    ,clcodedesc 
    ,Title 
    ,Fname 
    ,Sname 
    ,Address1 
    ,Address3 
    ,Address5 
    ,Address4 
    ,Address6 
    ,Postcode 
    ,Phoneh 
    ,PhoneW 
    ,Facsimileh 
    ,Mobile 
    ,Emailaddr 
    ,Bankname 
    ,Bankacno 
    ,Bankacna 
    ,Salute 
    ,Notes 
    ,BANKSORT 
    ) 
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", 
      importConnection)) 

将参数化值添加到上面的INSERT语句中:

importCommand.Parameters.AddWithValue("clcode", ""); 
      importCommand.Parameters.AddWithValue("clcodedesc", ""); 
      importCommand.Parameters.AddWithValue("Title", ""); 
      importCommand.Parameters.AddWithValue("Fname", ""); 
      importCommand.Parameters.AddWithValue("Sname", ""); 
      importCommand.Parameters.AddWithValue("Address1", ""); 
      importCommand.Parameters.AddWithValue("Address3", ""); 
      importCommand.Parameters.AddWithValue("Address5", ""); 
      importCommand.Parameters.AddWithValue("Address4", ""); 
      importCommand.Parameters.AddWithValue("Address6", ""); 
      importCommand.Parameters.AddWithValue("Postcode", ""); 
      importCommand.Parameters.AddWithValue("Phoneh", ""); 
      importCommand.Parameters.AddWithValue("PhoneW", ""); 
      importCommand.Parameters.AddWithValue("Facsimileh", ""); 
      importCommand.Parameters.AddWithValue("Mobile", ""); 
      importCommand.Parameters.AddWithValue("Emailaddr", ""); 
      importCommand.Parameters.AddWithValue("Bankname", ""); 
      importCommand.Parameters.AddWithValue("Bankacno", ""); 
      importCommand.Parameters.AddWithValue("Bankacna", ""); 
      importCommand.Parameters.AddWithValue("Salute", ""); 
      importCommand.Parameters.AddWithValue("Notes", ""); 
      importCommand.Parameters.AddWithValue("Banksort", ""); 

打开数据库连接:

exportConnection.Open(); 
      importConnection.Open(); 

填充上述字符串与从SQL拉出值:

var exportReader = exportCommand.ExecuteReader(); 

      while (exportReader.Read()) 
      { 
       string part1 = exportReader.GetSqlValue(2).ToString(); 
       string part2 = exportReader.GetSqlValue(3).ToString(); 
       string space = " "; 
       string code = exportReader.GetSqlValue(0).ToString(); 
       string clcode = exportReader.GetSqlValue(2).ToString(); 
       string title = exportReader.GetSqlValue(1).ToString(); 
       string sort1 = exportReader.GetSqlValue(20).ToString(); 
       string sort2 = exportReader.GetSqlValue(21).ToString(); 
       string sort3 = exportReader.GetSqlValue(22).ToString(); 
       // ID 
       importCommand.Parameters["clcode"].Value = string.Concat(clcode,code); 
       // 
       importCommand.Parameters["clcodedesc"].Value = string.Concat(title,space,part1,space,part2); 
       //OwTitle 
       importCommand.Parameters["Title"].Value = exportReader.GetSqlValue(1).ToString(); 
       //OwForenames 
       importCommand.Parameters["Fname"].Value = exportReader.GetSqlValue(2).ToString(); 
       //OwSurname 
       importCommand.Parameters["Sname"].Value = exportReader.GetSqlValue(3).ToString(); 
       //OwNum 
       importCommand.Parameters["Address1"].Value = exportReader.GetSqlValue(4).ToString(); 
       //[OwAddress1] 
       //[OwAddress2] 
       //[OwAddress3] 
       //[OwAddress4] 
       importCommand.Parameters["Address3"].Value = exportReader.GetSqlValue(5).ToString(); 
       importCommand.Parameters["Address5"].Value = exportReader.GetSqlValue(6).ToString(); 
       importCommand.Parameters["Address4"].Value = exportReader.GetSqlValue(7).ToString(); 
       importCommand.Parameters["Address6"].Value = exportReader.GetSqlValue(8).ToString(); 
       //OwPostcode 
       importCommand.Parameters["Postcode"].Value = exportReader.GetSqlValue(9).ToString(); 
       //OwPhHome 
       importCommand.Parameters["Phoneh"].Value = exportReader.GetSqlValue(10).ToString(); 
       //OwPhWork 
       importCommand.Parameters["PhoneW"].Value = exportReader.GetSqlValue(11).ToString(); 
       //OwPhFax 
       importCommand.Parameters["Facsimileh"].Value = exportReader.GetSqlValue(12).ToString(); 
       //OwPhMobile 
       importCommand.Parameters["Mobile"].Value = exportReader.GetSqlValue(13).ToString(); 
       //OwEMail 
       importCommand.Parameters["Emailaddr"].Value = exportReader.GetSqlValue(14).ToString(); 
       //OwBankName 
       importCommand.Parameters["Bankname"].Value = exportReader.GetSqlValue(15).ToString(); 
       //OwAccNo 
       importCommand.Parameters["Bankacno"].Value = exportReader.GetSqlValue(16).ToString(); 
       //OwAccName 
       importCommand.Parameters["Bankacna"].Value = exportReader.GetSqlValue(17).ToString(); 
       //OwSalutation 
       importCommand.Parameters["Salute"].Value = exportReader.GetSqlValue(18).ToString(); 
       //OwNotes 
       importCommand.Parameters["Notes"].Value = exportReader.GetSqlValue(19).ToString(); 
       //OwSort1 
       //OwSort2 
       //OwSort3 
       importCommand.Parameters["Banksort"].Value = string.Concat(sort1, sort2, sort3); 

注释行是在SQL数据库中的相关联的记录。

最后,我执行查询,并关闭连接:

try 
       { 
        importCommand.ExecuteNonQuery(); 
       } 
       catch (Exception e) 
       { 
        Console.Write("Error Writing to database"); 
        Console.Write(e); 
        Console.ReadKey(); 
       } 
      } 

      // done 
      Console.WriteLine("Complete!"); 
      Console.ReadKey(); 
      exportConnection.Close(); 
      importConnection.Close(); 

我的理论是,这个字符串值:

    importCommand.Parameters["Banksort"].Value = string.Concat(sort1, sort2, sort3); 

是导致问题,如表中的第一个记录是“NULL “

+0

你真的不应该使用'这样的AddWithValue',只要使用'加入('和使用,需要在'DbType'超载 –

+0

哦不对请告诉我两个 –

+1

之间的区别?那么,'AddWithValue('最好猜测参数的数据类型应该基于你传入的值,'Add'(你可以准确地告诉它数据类型应该是什么,所以没有机会错误来自错误的最佳猜测 –

回答

1

你当然应该使用AddWithValue。错误与此无关。你应该按照消息找出错误的原因:)它说“必须指定附加参数”。这意味着你的参数比你真正应该提供的要少。 IOW错误在计数中?占位符。它们在位置上处理并且必须与期望值计数的数量相匹配。即:

insert into myTable (field1, field2) values (?,?) 

是正确的,而:

insert into myTable (field1, field2) values (?) 

会给你看到的错误。你说,你有2列,但只指定1个参数占位符。添加足够吗? (因为我可以看到22个值和21?存在那里)。当然,不要忘记以与期望参数相同的顺序添加参数(使用AddWithValue)(第一个AddWithValue()绑定到第一个?因此field1,第二个绑定到第二个?和第二个field2等等)。

PS:你可以写这样的SQL,所以你不会指望?错误:

string fields = @"CLCODE 
    ,clcodedesc 
    ,Title 
    ,Fname 
    ,Sname 
    ,Address1 
    ,Address3 
    ,Address5 
    ,Address4 
    ,Address6 
    ,Postcode 
    ,Phoneh 
    ,PhoneW 
    ,Facsimileh 
    ,Mobile 
    ,Emailaddr 
    ,Bankname 
    ,Bankacno 
    ,Bankacna 
    ,Salute 
    ,Notes 
    ,BANKSORT"; 

string sql = string.Format("INSERT INTO CLIENT ({0}) values ({1})", 
    fields, 
    string.Join(",",fields.Split(',').Select(f => "?"))); 
+0

非常好的方法来避免'?'计数不匹配。 +1 –

+0

你先生是英雄,非常感谢你,这是确切的问题。 我不能相信我错过了。 –

相关问题