データベースから 3 日後に有効期限が切れる車両認証情報を取得し、フォーム ロードのデータグリッドに表示したいと考えています。これは、VB.NET で実装するコードです。
Dim ConnectString As String
ConnectString = ""
Dim connection As New SqlConnection(ConnectString)
Dim cmd As SqlCommand = New SqlCommand()
cmd.Connection = connection
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "AuthorizationExp"
Try
connection.Open()
Dim dreader As SqlDataReader
dreader = cmd.ExecuteReader()
While (dreader.Read())
Dim n As Integer = DataGridView1.Rows.Add()
DataGridView1.Rows.Item(n).Cells(0).Value = (dreader("AuthorizationNo").ToString)
DataGridView1.Rows.Item(n).Cells(1).Value = (dreader("DriverID").ToString)
DataGridView1.Rows.Item(n).Cells(2).Value = (dreader("PlateNo").ToString)
DataGridView1.Rows.Item(n).Cells(3).Value = (dreader("AuthorizationStart").ToString)
DataGridView1.Rows.Item(n).Cells(4).Value = (dreader("AuthorizationEnd").ToString)
End While
dreader.Close()
Catch ex As Exception
MessageBox.Show("An error ocurred (" + ex.Message + ")")
Finally
connection.Close()
End Try
ストアド プロシージャからそれらを取得します。
create procedure AuthorizationExp
as
select CarDriver.AuthorizationNo, CarDriver.DriverID, CarDriver.PlateNo, CarDriver.AuthorizationStart, CarDriver.AuthorizationEnd
from CarDriver
where DATEDIFF(day, GETDATE(), AuthorizationEnd) <=3
私が得たエラーは
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound
助けてくれてありがとう