条件表达式中的数据类型不匹配

Posted

技术标签:

【中文标题】条件表达式中的数据类型不匹配【英文标题】:Data type mismatch in criteria expression 【发布时间】:2010-07-29 22:54:06 【问题描述】:

我正在尝试将数据集中的数据添加到访问表 (.mdb)。当我尝试在表中插入值时出现此错误。这是代码

 DataSet ds = new DataSet();

 Console.Write("The NAME IS: " + FileName.Text.ToString());

 ds.ReadXml("C:\\Dallas_Test\\Offense_7_1_2010\\" + FileName.Text.ToString());

 ADOX.Catalog cat = new Catalog();

 cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Dallas_Test\\Offense_7_Test75_2010\\Offense.mdb;Jet OLEDB:Engine Type=5");

 //Create table in New Created Database

 OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\\Dallas_Test\\Offense_7_Test75_2010\\Offense.mdb");
 connection.Open();
 OleDbCommand olecom = new OleDbCommand("create table Offense([AgencyORI] Text,[AgencyName] Text,[offenseservicenumber] Text, [offensedate] Text, [offensereporteddate] Text,[offensestarttime] Text, [offensestoptime] Text, [offensetimedispatched] Text, [offensereportingarea] Number, " +

  " [offensebeat] Number,[offensewatch] Number, [offensesignal1] Number, [offensesignal2] Number, [offensename] Text, [offenserace] Text,[offensegender] Text, [offenseage] Number, [offenseblock] Number,[offensedirection] Number, [offensestreet] Text, [offenseapartment] Number, [offensecity] Text,[offensestate] Text, [offensezip] Number, " +

  " [offensebusinessblock] Text, [offensebusinessdirection] Text , [offensebusinessstreet] Text, [offensebusinesscity] Text, [offensepropertyattackcode] Text, [offensepremises] Text, [offensedateofoccurence1] Text, [offensetimeofoccurence1] Text, [offensedateofoccurence2] Text, [offensetimeofoccurence2] Text, [offenseucr1] Number, " +

  " [offenseucr2] Number, [offensemethodofoffense] Text, [offenseweather] Text, [offensefamilyviolence] Text, [offensegangacitivty] Text, [offensereportofficerbadge1] Number, " +

  " [offensereportingofficerbadge2] Number,[offenserecorddate] Text, [offensestatus] Text )", connection);
 olecom.ExecuteNonQuery();  

 // Iterate through each row and Write it in table
 foreach (DataRow r in ds.Tables[0].Rows)
 
  String AgencyORI = r["AgencyORI"].ToString();
  String AgencyName = r["AgencyName"].ToString();
  String offenseservicenumber = r["offenseservicenumber"].ToString();
  String offensedate = Convert.ToDateTime(r["offensedate"]).ToShortTimeString();
  String offensereporteddate = Convert.ToDateTime(r["offensereporteddate"]).ToShortTimeString();
  String offensestarttime = Convert.ToDateTime(r["offensestarttime"]).ToShortTimeString();
  String offensestoptime = Convert.ToDateTime(r["offensestoptime"]).ToShortTimeString();
  String offensetimedispatched = Convert.ToDateTime(r["offensetimedispatched"]).ToShortTimeString();
  int offensereportingarea = Convert.ToInt32(r["offensereportingarea"]);
  int offensebeat = Convert.ToInt32(r["offensebeat"]);
  int offensewatch = Convert.ToInt32(r["offensewatch"]);
  String offensesignal1 = r["offensesignal1"].ToString();
  String offensesignal2 = r["offensesignal2"].ToString();
  String offenserace = r["offenserace"].ToString();
  String offensename = r["offensename"].ToString();
  String offensegender = r["offensegender"].ToString();
  int offenseage = 0;
  int offenseblock = 0;
  int offensedirection = 0;
  if (r["offenseage"].GetType()==typeof(Int32))
  
    offenseage = Convert.ToInt32(r["offenseage"]);
  
  else
  
    r["offenseage"] = 0;
  
  if (r["offenseblock"].GetType() == typeof(Int32))
  
    offenseblock = Convert.ToInt32(r["offenseblock"]);
  
  else
  
    r["offenseblock"] = 0;
  
  if (r["offensedirection"].GetType() == typeof(Int32))
  
    offensedirection = Convert.ToInt32(r["offensedirection"]);
  
  else
  
    r["offensedirection"] = 0;
  
  String offensestreet = r["offensestreet"].ToString();
  int offenseapartment = 0;
  if (r["offenseapartment"].GetType() == typeof(Int32))
  
    offenseapartment = Convert.ToInt32(r["offenseatartment"]);
  
  else
  
    r["offenseapartment"] = 0;
   
  String offensecity = r["offensecity"].ToString();
  String offensestate = r["offensestate"].ToString();
  int offensezip = Convert.ToInt32(r["offensezip"]);
  String offensebusinessblock = r["offensebusinessblock"].ToString();
  String offensebusinessstreet = r["offensebusinessstreet"].ToString();
  String offensebusinessdirection =r["offensebusinessdirection"].ToString();
  String offensebusinesscity = r["offensebusinesscity"].ToString();
  String offensepropertyattackcode = r["offensepropertyattackcode"].ToString();
  int offensepremises = 0;
  if (r["offensepremises"].GetType() == typeof(Int32))
     
        offensepremises = Convert.ToInt32(r["offenspremises"]);
      
  else
      
        r["offensepremises"] = 0;
      
  String offensedateofoccurence1 = Convert.ToDateTime(r["offensedateofoccurence1"]).ToString();
  String offensetimeofoccurence1 = Convert.ToDateTime(r["offensetimeofoccurence1"]).ToShortTimeString();
  String offensedateofoccurence2 = Convert.ToDateTime(r["offensedateofoccurence2"]).ToString();
  String offensetimeofoccurence2 = Convert.ToDateTime(r["offensetimeofoccurence2"]).ToShortTimeString();
  int offenseucr1 = 0;
  if (r["offenseucr1"].GetType() == typeof(Int32))
  
   offenseucr1 = Convert.ToInt32(r["offenseucr1"]);
   
  else
  
   r["offenseucr1"] = 0;
  
  int offenseucr2 = 0;
  if (r["offenseucr2"].GetType() == typeof(Int32))
   
   offenseucr2 = Convert.ToInt32(r["offenseucr2"]);
    
  else
   
   r["offenseucr2"] = 0;
  
  String offensemethodofoffense = r["offensepropertyattackcode"].ToString();
  String offenseweather = r["offenseweather"].ToString();
  String offensefamilyviolence = r["offensefamilyviolence"].ToString();
  String offensegangacitivty =r["offensegangacitivty"].ToString();
  int offensereportofficerbadge1 = 0;
  if (r["offensereportofficerbadge1"].GetType() == typeof(Int32))
  
   offensereportofficerbadge1 = Convert.ToInt32(r["offensereportofficerbadge1"]);
   
  else
  
   r["offensereportofficerbadge1"] = 0;
  
  int offensereportingofficerbadge2 = 0;
  if (r["offensereportingofficerbadge2"].GetType() == typeof(Int32))
  
   offensereportingofficerbadge2 = Convert.ToInt32(r["offensereportingofficerbadge2"]);
  
  else
  
   r["offensereportingofficerbadge2"] = 0;
  
  String offenserecorddate = r["offenserecorddate"].ToString();
  String offensestatus = r["offensestatus"].ToString();
  olecom = new OleDbCommand("insert into Offense([AgencyORI],[AgencyName],[offenseservicenumber],[offensedate], [offensereporteddate], [offensestarttime], [offensestoptime],[offensetimedispatched],[offensereportingarea],[offensebeat],[offensewatch], [offensesignal1], [offensesignal2], [offensename], [offenserace], [offensegender]," +

   " [offenseage], [offenseblock], [offensedirection], [offensestreet], [offenseapartment], [offensecity], [offensestate],[offensezip], [offensebusinessblock], [offensebusinessdirection], [offensebusinessstreet], [offensebusinesscity], [offensepropertyattackcode],[offensepremises],[offensedateofoccurence1],[offensetimeofoccurence1], [offensedateofoccurence2],[offensetimeofoccurence2],[offenseucr1],[offenseucr2],[offensemethodofoffense],[offenseweather],[offensefamilyviolence], [offensegangacitivty], [offensereportofficerbadge1], [offensereportingofficerbadge2],[offenserecorddate],[offensestatus] ) values(@AgencyORI,@AgencyName,@offenseservicenumber,@offensedate,@offensereporteddate," + 

   " @offensestarttime, @offensestoptime,@offensetimedispatched, @offensereportingarea, @offesebeat, @offensewatch, @offensesignal1, @offensesignal2, @offensename, @offenserace, @offensegender, @offenseage, @offenseblock, @offensedirection, @offensestreet, @offenseapartment, @offensecity, offensestate, @offensezip," +

   " @offensebusinessblock, @offensebusinessdirection, @offensebusinessstreet, @offensebusinesscity,@offensepropertyattackcode,@offensepremises,@offensedateofoccurence1,@offensetimeofoccurence1, @offensedateofoccurence2, @offensetimeofoccurence2 ,@offenseucr1 ,@offenseucr2, @offensemethodofoffense, @offenseweather, @offensefamilyviolence, @offensegangacitivty, @offensereportofficerbadge1, @offensereportingofficerbadge2,@offenserecorddate, @offensestatus )", connection);
  olecom.Parameters.AddWithValue("@AgencyORI", AgencyORI);
  olecom.Parameters.AddWithValue("@AgencyName", AgencyName);
  olecom.Parameters.AddWithValue("@offenseservicenumber", offenseservicenumber);
  olecom.Parameters.AddWithValue("@offensedate", offensedate);
  olecom.Parameters.AddWithValue("@offensereporteddate", offensereporteddate);
  olecom.Parameters.AddWithValue("@offensestarttime", offensestarttime);
  olecom.Parameters.AddWithValue("@offensestoptime", offensestoptime);
  olecom.Parameters.AddWithValue("@offensetimedispatched", offensetimedispatched);
  olecom.Parameters.AddWithValue("@offensereportingarea", offensereportingarea);
  olecom.Parameters.AddWithValue("@offensebeat", offensebeat);
  olecom.Parameters.AddWithValue("@offensewatch", offensewatch);
  olecom.Parameters.AddWithValue("@offensesignal1", offensesignal1);
  olecom.Parameters.AddWithValue("@offensesignal2", offensesignal2);
  olecom.Parameters.AddWithValue("@offensename", offensename);
  olecom.Parameters.AddWithValue("@offenserace", offenserace);
  olecom.Parameters.AddWithValue("@offensegender", offensegender);
  olecom.Parameters.AddWithValue("@offenseblock", offenseblock);
  olecom.Parameters.AddWithValue("@offensedirection", offensedirection);
  olecom.Parameters.AddWithValue("@offensestreet", offensestreet);
  olecom.Parameters.AddWithValue("@offenseapartment", offenseapartment);
  olecom.Parameters.AddWithValue("@offensecity", offensecity);
  olecom.Parameters.AddWithValue("@offensestate", offensestate);
  olecom.Parameters.AddWithValue("@offenseage", offenseage);
  olecom.Parameters.AddWithValue("@offenesezip", offensezip);
  olecom.Parameters.AddWithValue("@offensebusinessblock", offensebusinessblock);
  olecom.Parameters.AddWithValue("@offensebusinessdirection", offensebusinessdirection);
  olecom.Parameters.AddWithValue("@offensebusinessstreet", offensebusinessstreet);
  olecom.Parameters.AddWithValue("@offensebusinesscity", offensebusinesscity);
  olecom.Parameters.AddWithValue("@offensepropertyattackcode", offensepropertyattackcode);
  olecom.Parameters.AddWithValue("@offensepremises", offensepremises);
  olecom.Parameters.AddWithValue("@offensedateofoccurence1", offensedateofoccurence1);
  olecom.Parameters.AddWithValue("@offensetimeofoccurence1", offensetimeofoccurence1);
  olecom.Parameters.AddWithValue("@offensedateofoccurence2", offensedateofoccurence2);
  olecom.Parameters.AddWithValue("@offensetimeofoccurence2", offensetimeofoccurence2);
  olecom.Parameters.AddWithValue("@offenseucr1", offenseucr1);
  olecom.Parameters.AddWithValue("@offenseucr2", offenseucr2);
  olecom.Parameters.AddWithValue("@offensemethodofoffense", offensemethodofoffense);
  olecom.Parameters.AddWithValue("@offenseweather", offenseweather);
  olecom.Parameters.AddWithValue("@offensefamilyviolence", offensefamilyviolence);
  olecom.Parameters.AddWithValue("@offensegangacitivty", offensegangacitivty);
  olecom.Parameters.AddWithValue("@offensereportofficerbadge1", offensereportofficerbadge1);
  olecom.Parameters.AddWithValue("@offensereportingofficerbadge2", offensereportingofficerbadge2);
  olecom.Parameters.AddWithValue("@offenserecorddate", offenserecorddate);
  olecom.Parameters.AddWithValue("@offensestatus", offensestatus);
  olecom.ExecuteNonQuery();
 
 connection.Close();  

我在 olecom.ExecuteNon Query() 方法上遇到错误。我也尝试将日期字段转换为字符串,但它仍然没有帮助。我也没有被告知导致此问题的字段。 有人可以帮我弄这个吗? 谢谢。

【问题讨论】:

如果您格式化您的代码,并且只发布相关部分会很有帮助... 抱歉这么长的代码,我发布长代码的原因是我可能在代码中犯了错误(错字或字符串/int互换)。我认为如果我修剪这段代码,我可能会隐藏一个可能的问题区域。不过感谢您的评论。如果您仍然希望我修剪代码,请告诉我。谢谢,希德 @sidhanshu:至少去掉行间多余的空格,以便我们阅读。 我只是把SQL剥离出来,放到access 2007中,运行成功。您使用的是哪个版本的 Access? @SteveCav:我正在运行 Access 2007。谢谢 【参考方案1】:

offensestate 缺少“@”。在您的插入值中应该是 @offensestate

【讨论】:

感谢您的建议,但不幸的是,它并没有消除错误。我会去掉代码中多余的空格。 我已尝试删除代码和文本中的所有空格。我希望这有帮助。谢谢您的帮助。 -Sid

以上是关于条件表达式中的数据类型不匹配的主要内容,如果未能解决你的问题,请参考以下文章

错误“条件表达式中的数据类型不匹配”

条件表达式中的数据类型不匹配

MS Access - 条件表达式中的数据类型不匹配

条件表达式中的数据类型不匹配

条件表达式中的 C# 数据类型不匹配

条件表达式访问中的查询数据类型不匹配