忽略从datagrid视图插入sql的null数据

时间:2016-07-27 06:20:16

标签: sql-server vb.net datagrid

我从数据gridview向sql server插入数据我有问题一些空数据输入,如何使空数据无法进入我的tabble我有一些问题,一些数据没有进入我的表。{{3 }}

Using con As New SqlConnection(constring)
    Using cmd As New SqlCommand("INSERT INTO kkpsurabaya VALUES(@Emp_No, @AC_No, @No, @Name, @Auto_Assign, @Date, @Timetable, @on_duty, @off_duty, @clock_in, @clock_out, @Normal, @Realtime, @Late, @Early, @Absent, @OT_Time, @Work_time, @Departmen)", con)
        cmd.Parameters.AddWithValue("@Emp_No", row.Cells("Emp No#").Value)
        cmd.Parameters.AddWithValue("@AC_No", row.Cells("AC-No#").Value)
        cmd.Parameters.AddWithValue("@No", row.Cells("No#").Value)
        cmd.Parameters.AddWithValue("@Name", row.Cells("Name").Value)
        cmd.Parameters.AddWithValue("@Auto_Assign", row.Cells("Auto-Assign").Value)
        cmd.Parameters.AddWithValue("@Date", row.Cells("Date").Value)
        cmd.Parameters.AddWithValue("@Timetable", row.Cells("Timetable").Value)
        cmd.Parameters.AddWithValue("@on_duty", row.Cells("on duty").Value)
        cmd.Parameters.AddWithValue("@off_duty", row.Cells("off duty").Value)
        cmd.Parameters.AddWithValue("@clock_in", row.Cells("clock in").Value)
        cmd.Parameters.AddWithValue("@clock_out", row.Cells("clock out").Value)
        cmd.Parameters.AddWithValue("@Normal", row.Cells("Normal").Value)
        cmd.Parameters.AddWithValue("@Realtime", row.Cells("Real time").Value)
        cmd.Parameters.AddWithValue("@Late", row.Cells("Late").Value)
        cmd.Parameters.AddWithValue("@Early", row.Cells("Early").Value)
        cmd.Parameters.AddWithValue("@Absent", row.Cells("Absent").Value)
        cmd.Parameters.AddWithValue("@OT_Time", row.Cells("OT Time").Value)
        cmd.Parameters.AddWithValue("@Work_time", row.Cells("Work time").Value)
        cmd.Parameters.AddWithValue("@Departmen", row.Cells("Department").Value)
        con.Open()
        Try
            cmd.ExecuteNonQuery()

2 个答案:

答案 0 :(得分:1)

我强烈建议以不同的方式做这件事。不是循环遍历网格并手动插入每一行,而是创建DataTable并将其绑定到网格。然后,您可以对数据适配器的Update方法进行一次调用以保存该批次。您可以使用命令构建器生成数据适配器的InsertCommand,也可以自己执行。无论哪种方式,您都不必担心NULL,因为它将为您处理。以下是使用命令构建器的示例:

Private adapter As SqlDataAdapter
Private builder As SqlCommandBuilder
Private table As DataTable

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    adapter = New SqlDataAdapter("SELECT * FROM MyTable", "connection string here")
    builder = New SqlCommandBuilder(adapter)
    table = New DataTable

    adapter.FillSchema(table, SchemaType.Source)

    Me.BindingSource1.DataSource = table
    Me.DataGridView1.DataSource = Me.BindingSource1
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Me.BindingSource1.EndEdit()

    adapter.Update(table)
End Sub

答案 1 :(得分:0)

如果row.Cells(" Emp No#")。Value为null,则AddWithValue将省略此参数而不是放入DBNull.Value。
我认为这是一个巨大的错误,但我似乎是唯一的一个 无论如何你可以像这样解决它:

  cmd.Parameters.AddWithValue("@Emp_No", row.Cells("Emp No#").Value ?? (object)DBNull.Value)