0

返回数据库中的表包括

  • 项目编号
  • 任务名称
  • 开始
  • 完全的
  • 地点

不知何故,我想将gridview中的数据插入到sql中。请检查代码

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim SQLCmd As New SqlCommand
        SQLCmd.Connection = SQLCon

        SQLCon.open()

        ''insert data to sql database row by row
        Dim taskname, location, start, complete As String

        For i As Integer = 0 To Me.DataGridView1.Rows.Count
            taskname = Me.DataGridView1.Rows(i).Cells(0).ToString()
            start = Me.DataGridView1.Rows(i).Cells(1).ToString()
            complete = Me.DataGridView1.Rows(i).Cells(2).ToString()
            location = Me.DataGridView1.Rows(i).Cells(3).ToString()

            SQLCmd.CommandText = "insert into timeline ( project_id , taskname , start , complete , location) values (@pid,@task,@start,@complete,@location)"
            SQLCmd.Parameters.AddWithValue("@pid", TextBox1.Text)
            SQLCmd.Parameters.AddWithValue("@task", taskname)
            SQLCmd.Parameters.AddWithValue("@start", start)
            SQLCmd.Parameters.AddWithValue("@complete", complete)
            SQLCmd.Parameters.AddWithValue("@location", location)
            SQLCmd.ExecuteNonQuery()
        Next

        SQLCon.Close()
    End Sub
4

1 回答 1

0

此错误表示您的字段之一DATATYPE lengthsmall&您正在尝试插入more then the capacity of that DATATYPE length.

例如:
project_id 被声明为INT并且您传递假设 12312312313 值,该值大于INT数据类型的限制。

因此,请调试您的代码 n 检查所有列的数据类型是什么,并根据该数据类型正确传递数据。

示例

DECLARE @UserDetails TABLE(UserId INT, UserName VARCHAR(25),Designation VARCHAR(10))
INSERT INTO @UserDetails(UserId,UserName,Designation)
VALUES(1,'Hardik Parmar','Senior Software Engineer')
SELECT * FROM @UserDetails

这个例子会抛出和你一样的错误 Coz

为了

Designation 

coulmn 我们已经给出了VARCHAR(10)这么多的长度,我们正在尝试插入,VARCHAR(25)但是Designation coulmn 的限制是 VARCHAR(10)它会给你错误,因为

string or binary data would be truncated on gridview
于 2014-11-13T05:00:20.563 回答