2011-09-28 48 views
0

我正在处理需要将数据从excel表导入到mysql数据库表的应用程序的一部分。代码工作正常,直到它到达Excel表格中的某个字符串值被赋予“ABCDE All'John D Doe 999 West Lame Blvd Cullman,AL 35055”的记录。我不确定,但我相信它必须完全符合那里出现的“'”。哪一个不能改变,excelsheet中的其他记录也可以包含“'”......当它到达这个记录时,它会抛出这个错误: 你的SQL语法有错误;检查对应于你的MySQL服务器版本的手册,在'John D Doe','ABCDE','All'John','D','Doe','256-555-5555',' ”,‘256-555-5555’在行1从excel表导入某些字符串到mysql的问题

代码,我有解决此问题如下:

 Private Function PerFormUpdate(ByVal customer As String, ByVal bill_to As String, ByVal Contact As String, ByVal Company As String, ByVal firstName As String, ByVal mi As String, ByVal lastname As String, ByVal phone As String, ByVal altPhone As String, ByVal fax As String) 
     Dim _db As New schoolEntities 

     Dim command As MySqlCommand = _dbconn.CreateCommand() 
     command.CommandText = "SELECT * FROM quickbooks_imports WHERE Customer= "" & _customer& "" & Bill_to= "" & _bill_to& "" & Contact= "" & _Company& ""& First_Name= "" & _firstName& "" & M_I= "" & _mi& "" & Last_Name= "" & _lastname& "" & Phone= "" & _phone& "" & Alt_Phone= "" & _altPhone& "" & Fax= "" & _Fax& """ 
     _dbconn.Open() 

     Dim _mysqlReader As MySqlDataReader = command.ExecuteReader() 
     _dbconn.Close() 

     If Not _mysqlReader.HasRows Then 
      Dim _UpdateItem As New quickbooks_imports 
      Dim updateCommand As MySqlCommand = _dbconn.CreateCommand() 

      _UpdateItem.Customer = customer 
      _UpdateItem.Bill_to = bill_to 
      _UpdateItem.Contact = Contact 
      _UpdateItem.Company = Company 
      _UpdateItem.First_Name = firstName 
      _UpdateItem.M_I = mi 
      _UpdateItem.Last_Name = lastname 
      _UpdateItem.Phone = phone 
      _UpdateItem.Alt_Phone = altPhone 
      _UpdateItem.Fax = fax 

      updateCommand.CommandText = "INSERT INTO quickbooks_imports(Customer,Bill_to,Contact,Company,First_Name,M_I,Last_Name,Phone,Alt_Phone,Fax) VALUES ('" & _UpdateItem.Customer & "','" & _UpdateItem.Bill_to & "','" & _UpdateItem.Contact & "','" & _UpdateItem.Company & "','" & _UpdateItem.First_Name & "','" & _UpdateItem.M_I & "','" & _UpdateItem.Last_Name & "','" & _UpdateItem.Phone & "','" & _UpdateItem.Alt_Phone & "','" & _UpdateItem.Fax & "') " 
      _dbconn.Open() 
      updateCommand.ExecuteNonQuery() 

      _db.SaveChanges() 

的误差上显示出为ExecuteNonQuery执行更新..

任何帮助将不胜感激...

根据您的回应,我切换到PARAMS,这是新的代码:

  updateCommand.CommandText = "INSERT INTO quickbooks_imports (Customer,Bill_to,Contact,Company,First_Name,M_I,Last_Name,Phone,Alt_Phone,Fax) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" 
      updateCommand.Parameters.AddWithValue("Customer", _UpdateItem.Customer) 
      updateCommand.Parameters.AddWithValue("Bill_to", _UpdateItem.Bill_to) 
      updateCommand.Parameters.AddWithValue("Contact", _UpdateItem.Contact) 
      updateCommand.Parameters.AddWithValue("Company", _UpdateItem.Company) 
      updateCommand.Parameters.AddWithValue("First_Name", _UpdateItem.First_Name) 
      updateCommand.Parameters.AddWithValue("M_I", _UpdateItem.M_I) 
      updateCommand.Parameters.AddWithValue("Last_Name", _UpdateItem.Last_Name) 
      updateCommand.Parameters.AddWithValue("Phone", _UpdateItem.Phone) 
      updateCommand.Parameters.AddWithValue("Alt_Phone", _UpdateItem.Alt_Phone) 
      updateCommand.Parameters.AddWithValue("Fax", _UpdateItem.Fax) 

怎么过的,现在抛出一个致命异常...

我就像你在你的答复中提到,代码如下所示使用名称参数尝试:

  Private Function PerFormUpdate(ByVal customer As String, ByVal bill_to As String, ByVal Contact As String, ByVal Company As String, ByVal firstName As String, ByVal mi As String, ByVal lastname As String, ByVal phone As String, ByVal altPhone As String, ByVal fax As String) 
     Dim _db As New schoolEntities 

     Dim command As MySqlCommand = _dbconn.CreateCommand() 
     command.CommandText = "SELECT * FROM quickbooks_imports WHERE Customer= "" & _customer& "" & Bill_to= "" & _bill_to& "" & Contact= "" & _Company& ""& First_Name= "" & _firstName& "" & M_I= "" & _mi& "" & Last_Name= "" & _lastname& "" & Phone= "" & _phone& "" & Alt_Phone= "" & _altPhone& "" & Fax= "" & _Fax& """ 
     _dbconn.Open() 

     Dim _mysqlReader As MySqlDataReader = command.ExecuteReader() 
     _dbconn.Close() 

     If Not _mysqlReader.HasRows Then 
      Dim _UpdateItem As New quickbooks_imports 
      Dim updateCommand As MySqlCommand = _dbconn.CreateCommand() 

      _UpdateItem.Customer = customer 
      _UpdateItem.Bill_to = bill_to 
      _UpdateItem.Contact = Contact 
      _UpdateItem.Company = Company 
      _UpdateItem.First_Name = firstName 
      _UpdateItem.M_I = mi 
      _UpdateItem.Last_Name = lastname 
      _UpdateItem.Phone = phone 
      _UpdateItem.Alt_Phone = altPhone 
      _UpdateItem.Fax = fax 

      updateCommand.CommandText = "INSERT INTO quickbooks_imports (Customer,Bill_to,Contact,Company,First_Name,M_I,Last_Name,Phone,Alt_Phone,Fax) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" 
      updateCommand.Parameters.AddWithValue("@Customer", _UpdateItem.Customer) 
      updateCommand.Parameters.AddWithValue("@Bill_to", _UpdateItem.Bill_to) 
      updateCommand.Parameters.AddWithValue("@Contact", _UpdateItem.Contact) 
      updateCommand.Parameters.AddWithValue("@Company", _UpdateItem.Company) 
      updateCommand.Parameters.AddWithValue("@First_Name", _UpdateItem.First_Name) 
      updateCommand.Parameters.AddWithValue("@M_I", _UpdateItem.M_I) 
      updateCommand.Parameters.AddWithValue("@Last_Name", _UpdateItem.Last_Name) 
      updateCommand.Parameters.AddWithValue("@Phone", _UpdateItem.Phone) 
      updateCommand.Parameters.AddWithValue("@Alt_Phone", _UpdateItem.Alt_Phone) 
      updateCommand.Parameters.AddWithValue("@Fax", _UpdateItem.Fax) 



      'updateCommand.CommandText = "INSERT INTO EXCEL (id,Customer,Bill_to,Contact,Company,First_Name,M_I,Last_Name,Phone,Alt_Phone,Fax) VALUES ('" & _UpdateItem.id & "','" & _UpdateItem.Customer & "','" & _UpdateItem.Bill_to & "','" & _UpdateItem.Contact & "','" & _UpdateItem.Company & "','" & _UpdateItem.First_Name & "','" & _UpdateItem.M_I & "','" & _UpdateItem.Last_Name & "','" & _UpdateItem.Phone & "','" & _UpdateItem.Alt_Phone & "','" & _UpdateItem.Fax & "') ON DUPLICATE KEY UPDATE Customer= '" & _UpdateItem.Customer & "' Bill_to= '" & _UpdateItem.Bill_to & "' Contact= '" & _UpdateItem.Contact & "' Company= '" & _UpdateItem.Company & "' First_Name= '" & _UpdateItem.First_Name & "' M_I= '" & _UpdateItem.M_I & "' Last_Name= '" & _UpdateItem.Last_Name & "' Phone= '" & _UpdateItem.Phone & "' Alt_Phone= '" & _UpdateItem.Alt_Phone & "' Fax= '" & _UpdateItem.Fax & "'" 
      'updateCommand.CommandText = "INSERT INTO quickbooks_imports (Customer,Bill_to,Contact,Company,First_Name,M_I,Last_Name,Phone,Alt_Phone,Fax) VALUES ('" & _UpdateItem.Customer & "','" & _UpdateItem.Bill_to & "','" & _UpdateItem.Contact & "','" & _UpdateItem.Company & "','" & _UpdateItem.First_Name & "','" & _UpdateItem.M_I & "','" & _UpdateItem.Last_Name & "','" & _UpdateItem.Phone & "','" & _UpdateItem.Alt_Phone & "','" & _UpdateItem.Fax & "') " 
      _dbconn.Open() 
      updateCommand.ExecuteNonQuery() 

      _db.SaveChanges() 

和我仍然在updateCommand.ExecuteNonQuery()上遇到致命异常()

命令执行期间遇到致命错误。

InnerException消息:“Parameter'?'必须定义。“

+0

这是新代码: – Skindeep2366

回答

0

您需要使用parameters,这将正确地转义您的字符串以执行数据库。

请参阅此链接。 http://www.devart.com/dotconnect/mysql/docs/Parameters.html

编辑:尝试使用命名参数来代替:

updateCommand.CommandText = "INSERT INTO quickbooks_imports (Customer,Bill_to,Contact,Company,First_Name,M_I,Last_Name,Phone,Alt_Phone,Fax) VALUES ("@Customer", "@Bill_to", "@Contact", "@Company", "@First_Name", "@M_I", "@Last_Name", "@Phone", "@Alt_Phone", "@Fax")" 
updateCommand.Parameters.AddWithValue("@Customer", _UpdateItem.Customer) 
updateCommand.Parameters.AddWithValue("@Bill_to", _UpdateItem.Bill_to) 
updateCommand.Parameters.AddWithValue("@Contact", _UpdateItem.Contact) 
updateCommand.Parameters.AddWithValue("@Company", _UpdateItem.Company) 
updateCommand.Parameters.AddWithValue("@First_Name", _UpdateItem.First_Name) 
updateCommand.Parameters.AddWithValue("@M_I", _UpdateItem.M_I) 
updateCommand.Parameters.AddWithValue("@Last_Name", _UpdateItem.Last_Name) 
updateCommand.Parameters.AddWithValue("@Phone", _UpdateItem.Phone) 
updateCommand.Parameters.AddWithValue("@Alt_Phone", _UpdateItem.Alt_Phone) 
updateCommand.Parameters.AddWithValue("@Fax", _UpdateItem.Fax) 
+0

我去那里,通过材料看,以为我有正确的,但它现在抛出一个致命异常错误说:“参数'?'必须定义。“ – Skindeep2366

+0

尝试仅使用'Parameters.Add'而不是'Parameters.AddWithValue'。 – Jack

+0

与Parameters.Add它仍然会引发致命的异常,并且VS 2010下划线Parameters.Add绿色,并说它outdatted使用parameters.addwithvalue ....我已经改变它,虽然参数。添加,仍然得到相同的异常 – Skindeep2366