Try
For i As Integer = 0 To dgvImport.RowCount - 1
strsql = "INSERT INTO Student(nisit_id,nisit_name,nisit_major)"
strsql &= "VALUES('" & dgvImport.Rows(i).Cells(1).Value & "' , '" & dgvImport.Rows(i).Cells(2).Value & "' , '" & dgvImport.Rows(i).Cells(3).Value & "')"
With cmd
.Connection = con
.CommandText = strsql
.CommandType = CommandType.Text
.ExecuteNonQuery()
End With
Next
Catch
MsgBox("มีข้อมูลนิสิตอยู่แล้ว")
MessageBox.Show("เพิ่มนิสิตเรียบร้อยแล้ว")
End Try
primary key คือ nisit_id
Tag : .NET, Ms SQL Server 2008, VB.NET, VS 2010 (.NET 4.x)