使用Vb.net将行插入SQL Server表

时间:2019-05-28 17:52:10

标签: sql vb.net

我无法在SQL Server表中插入行。

我尝试使用SQL Server表中具有数据类型的参数的SQL命令。

Using conn As New SqlConnection(SQLconnectionString)
 Using comm As New SqlCommand()
  With comm
  .Connection = conn
  .CommandType = CommandType.Text

   Dim query As String = String.Empty
   query &= "INSERT INTO " + STTL_Cost_DS.Replace(" ", "_") + " ([Week 
   Ending], [Year], [Portfolio View], [Project], [Project Phase], [Project 
   Code], [HVEC %], [CPI/Constr.], [% Constr Rework], [Piping Design Hrs 
   FAC], [Electrical Engr Hrs FAC], [Inst Engr Hrs FAC], " + _
   "[Process Engr Hrs FAC], [Engineering Hrs FAC], [No. of Isometrics], 
   [No. of kW Installed], [No. of I-Os], [No. of P&IDs], 
   [No. of Equipment], 
   [Revisions per Iso])"

 query &= " VALUES ([@Week Ending], [@Year], [@Portfolio View], 
 [@Project], [@Project Phase], [@Project Code], [@HVEC %], [@CPI/Constr.], 
 [@% Constr Rework], [@Piping Design Hrs FAC], [@Electrical Engr Hrs FAC], 
 [@Inst Engr Hrs FAC], " + _
 "[@Process Engr Hrs FAC], [@Engineering Hrs FAC], [@No. of Isometrics], 
 [@No. of kW Installed], [@No. of I-Os], [@No. of P&IDs], [@No. of 
 Equipment], [@Revisions per Iso])"

 .CommandText = query

  .Parameters.Add("@[Week Ending]", SqlDbType.DateTime).Value = 
  dt_ExcelTable.Rows(i).Item(weekEnding)
  .Parameters.Add("@[Year]", SqlDbType.Float).Value = 
  dt_ExcelTable.Rows(i).Item(year)
  .Parameters.Add("@[Portfolio View]", SqlDbType.NChar).Value = 
  dt_ExcelTable.Rows(i).Item(PortfolioView)
  .Parameters.Add("@[Project]", SqlDbType.NVarChar).Value = 
  dt_ExcelTable.Rows(i).Item(project)
  .Parameters.Add("@[Project Phase]", SqlDbType.NVarChar).Value = 
  dt_ExcelTable.Rows(i).Item("Project Phase")
  .Parameters.Add("@[Project Code]", SqlDbType.NVarChar).Value = 
  dt_ExcelTable.Rows(i).Item("Project Code")
  .Parameters.Add("@[HVEC %]", SqlDbType.Float).Value = 
  dt_ExcelTable.Rows(i).Item("HVEC %")
 .Parameters.Add("@[CPI/Constr.]", SqlDbType.Float).Value = 
  dt_ExcelTable.Rows(i).Item("CPI/Constr.")
  .Parameters.Add("@[% Constr Rework]", SqlDbType.Float).Value = 
  dt_ExcelTable.Rows(i).Item("% Constr Rework")
 .Parameters.Add("@[Piping Design Hrs FAC]", SqlDbType.NVarChar).Value = 
 dt_ExcelTable.Rows(i).Item("Piping Design Hrs FAC")
 .Parameters.Add("@[Electrical Engr Hrs FAC]", SqlDbType.NVarChar).Value = 
 dt_ExcelTable.Rows(i).Item("Electrical Engr Hrs FAC")
 .Parameters.Add("@[Inst Engr Hrs FAC]", SqlDbType.NVarChar).Value = 
 dt_ExcelTable.Rows(i).Item("Inst Engr Hrs FAC")
 .Parameters.Add("@[Process Engr Hrs FAC]", SqlDbType.NVarChar).Value = 
 dt_ExcelTable.Rows(i).Item("Process Engr Hrs FAC")
 .Parameters.Add("@[Engineering Hrs FAC]", SqlDbType.NVarChar).Value = 
 dt_ExcelTable.Rows(i).Item("Engineering Hrs FAC")
 .Parameters.Add("@[No. of Isometrics]", SqlDbType.NVarChar).Value = 
 dt_ExcelTable.Rows(i).Item("No. of Isometrics")
 .Parameters.Add("@[No. of kW Installed]", SqlDbType.NVarChar).Value = 
 dt_ExcelTable.Rows(i).Item("No. of kW Installed")
.Parameters.Add("@[No. of I-Os]", SqlDbType.NVarChar).Value = 
 dt_ExcelTable.Rows(i).Item("No. of I-Os")
 .Parameters.Add("@[No. of P&IDs]", SqlDbType.NVarChar).Value = 
 dt_ExcelTable.Rows(i).Item("No. of P&IDs")
 .Parameters.Add("@[No. of Equipment]", SqlDbType.NVarChar).Value = 
 dt_ExcelTable.Rows(i).Item("No. of Equipment")
.Parameters.Add("@[Revisions per Iso]", SqlDbType.NVarChar).Value = 
 dt_ExcelTable.Rows(i).Item("Revisions per Iso")

  Try
    conn.Open()
    comm.ExecuteNonQuery()
  Catch ex As Exception
    MsgBox(ex.Message, "Error Message")
End Try

   End With
  End Using
 End Using

运行上述命令时出现错误

  

“日期时间”附近的语法不正确

即使我删除第一个“ datetime”参数只是为了进行测试,也会出现错误

  

“ float”附近的语法不正确

下一个参数。

0 个答案:

没有答案