返回由
组成的数据库中的表不知怎的,我想将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
答案 0 :(得分:0)
此错误表示您的其中一个字段DATATYPE length
为small
&您尝试插入more then the capacity of that DATATYPE length.
例如:
project_id声明为INT
,您传递的假设为12312312313,该值大于INT
数据类型的限制。
所以,请调试你的代码n检查所有coulmns&的数据类型是什么?根据该数据类型,您是否正确传递数据。
示例:
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
我们已经给了VARCHAR(10)
这么长的时间。我们正试图插入VARCHAR(25)
,但Designation
库存的限制为VARCHAR(10)
,那么它会给你错误
string or binary data would be truncated on gridview