WPF使用for循环将数据从datagrid插入sql数据库

WPF使用for循环将数据从datagrid插入sql数据库,wpf,vb.net,Wpf,Vb.net,我试图使用for循环将WPF数据网格中的行数据插入SQL数据库。不幸的是,我收到一个错误,我不知道我的代码出了什么问题。请你查一下我的密码好吗 For Z As Integer = 0 To Dawam_Grid.Items.Count - 1 Dim rd As SqlDataReader Dim conn As New SqlConnection Dim cmd As New SqlCommand If Not conn Is Nothing Then conn.Close() conn

我试图使用
for
循环将WPF数据网格中的行数据插入SQL数据库。不幸的是,我收到一个错误,我不知道我的代码出了什么问题。请你查一下我的密码好吗

For Z As Integer = 0 To Dawam_Grid.Items.Count - 1
Dim rd As SqlDataReader
Dim conn As New SqlConnection
Dim cmd As New SqlCommand

If Not conn Is Nothing Then conn.Close()

conn.ConnectionString = "User Id='" & sb2.UserID & "';Pwd='" & sb2.Password & "';DATA SOURCE='" & sb2.DataSource & "';Initial Catalog='" & sb2.InitialCatalog & "';MultipleActiveResultSets=true"
conn.Open()
Dim ds As DataSet = New DataSet
ds.Clear() `?
cmd.Connection = conn

' dt.Columns.Add("ShowRoom_col")
' dt.Columns.Add("Job")
Dim mycommand As New SqlCommand("insert into Scedual (emp_code,Name_col,Date_Col,Shift_Col,Mor_from,Mor_to,eve_from, " _
                                                  & "eve_to,BasicSalary_col,Hour_Value_col2,Over_Hours_col2,Trans_Value_col2, " _
                                                  & "Total_OverTime_col2,Month_No,create_user,create_date,create_time,Ikama_No,Official_Hours,Day_Shift_Hours, " _
                                                  & "Day_Shift_Extra_Hours,Month_Shift_Hours,Month_Shift_Extra_Hours,Month_Official_Hours,from_date,to_date,ShowRoom_col,Job,Year_No) " _
                                                      & "values (@emp_code,@Name_col,@Date_Col,@Shift_Col,@Mor_from,@Mor_to,@eve_from, " _
                                                  & "@eve_to,@BasicSalary_col,@Hour_Value_col2,@Over_Hours_col2, " _
                                                  & "@Trans_Value_col2,@Total_OverTime_col2,@Month_No,@create_user,@create_date,@create_time,@Ikama_No,@Official_Hours,@Day_Shift_Hours, " _
                                                  & "@Day_Shift_Extra_Hours,@Month_Shift_Hours,@Month_Shift_Extra_Hours,@Month_Official_Hours,@from_date,@to_date,@ShowRoom_col,@Job,@Year_No)", conn)




mycommand.Parameters.AddWithValue("@ShowRoom_col", Dawam_Grid.Items(Z).Cells("ShowRoom_col1").Value.ToString)
mycommand.Parameters.AddWithValue("@Job", Dawam_Grid.Items(Z).Cells("Job1").Value.ToString)
mycommand.Parameters.AddWithValue("@emp_code", Dawam_Grid.Items(Z).Cells("Emp_Code1").Value.ToString)
mycommand.Parameters.AddWithValue("@Name_col", Dawam_Grid.Items(Z).Cells("Emp_Name1").Value.ToString)
mycommand.Parameters.AddWithValue("@Date_Col", Dawam_Grid.Items(Z).Cells("Date_Col1").Value)
mycommand.Parameters.AddWithValue("@Shift_Col", Dawam_Grid.Items(Z).Cells("Shift_Col1").Value.ToString)
mycommand.Parameters.AddWithValue("@Mor_from", Dawam_Grid.Items(Z).Cells("Time_Mor_from1").Value)
mycommand.Parameters.AddWithValue("@Mor_to", Dawam_Grid.Items(Z).Cells("Time_Mor_to1").Value)
mycommand.Parameters.AddWithValue("@eve_from", Dawam_Grid.Items(Z).Cells("Time_eve_from1").Value)
mycommand.Parameters.AddWithValue("@eve_to", Dawam_Grid.Items(Z).Cells("Time_eve_to1").Value)
mycommand.Parameters.AddWithValue("@BasicSalary_col", Dawam_Grid.Items(Z).Cells("Salary_col1").Value)
mycommand.Parameters.AddWithValue("@Hour_Value_col2", Dawam_Grid.Items(Z).Cells("Hour_Value_col1").Value)
mycommand.Parameters.AddWithValue("@Over_Hours_col2", Dawam_Grid.Items(Z).Cells("Over_Hours_col1").Value)
mycommand.Parameters.AddWithValue("@Trans_Value_col2", Dawam_Grid.Items(Z).Cells("Trans_Value_col1").Value)
mycommand.Parameters.AddWithValue("@Total_OverTime_col2", Dawam_Grid.Items(Z).Cells("Total_OverTime_col1").Value)
mycommand.Parameters.AddWithValue("@Month_No", Dawam_Grid.Items(Z).Cells("Month_No1").Value)
mycommand.Parameters.AddWithValue("@Ikama_No", Dawam_Grid.Items(Z).Cells("Ikama_No1").Value)
mycommand.Parameters.AddWithValue("@create_user", Main_UserName)
mycommand.Parameters.AddWithValue("@create_date", DateTime.Today.Date.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture))
mycommand.Parameters.AddWithValue("@create_time", DateTime.Now.ToString("HH:mm:ss"))
mycommand.Parameters.AddWithValue("@Official_Hours", Dawam_Grid.Items(Z).Cells("Official_Hours1").Value)
mycommand.Parameters.AddWithValue("@Day_Shift_Hours", Dawam_Grid.Items(Z).Cells("Day_Shift_Hours1").Value)
mycommand.Parameters.AddWithValue("@Day_Shift_Extra_Hours", Dawam_Grid.Items(Z).Cells("Day_Shift_Extra_Hours1").Value)
mycommand.Parameters.AddWithValue("@Month_Official_Hours", Dawam_Grid.Items(Z).Cells("Month_Official_Hours1").Value)
mycommand.Parameters.AddWithValue("@Month_Shift_Hours", Dawam_Grid.Items(Z).Cells("Month_Shift_Hours1").Value)
mycommand.Parameters.AddWithValue("@Month_Shift_Extra_Hours", Dawam_Grid.Items(Z).Cells("Month_Shift_Extra_Hours1").Value)
mycommand.Parameters.AddWithValue("@from_date", Dawam_Grid.Items(Z).Cells("from_date1").Value)
mycommand.Parameters.AddWithValue("@to_date", Dawam_Grid.Items(Z).Cells("to_date1").Value)
mycommand.Parameters.AddWithValue("@Year_No", Format(From_Date.EditValue, ("yyyy")))
mycommand.ExecuteNonQuery()

Next
错误消息:

System.MissingMemberException:在类型“Dawam\u网格\u项”中找不到“单元格”

Dawam\u Grid\u Items
是我创建的一个公共类,用于向数据网格添加行

Public Class Dawam_Grid_Items

    Public Property Emp_Code1 As String
    Public Property Emp_Name1 As String
    Public Property Salary_col1 As String
    Public Property Hour_Value_col1 As String
    Public Property Over_Hours_col1 As String
    Public Property Trans_Value_col1 As String
    Public Property Total_OverTime_col1 As String
    Public Property Ikama_No1 As String
    Public Property ShowRoom_col1 As String
    Public Property Time_Mor_from1 As String
    Public Property Time_Mor_to1 As String
    Public Property Time_eve_from1 As String
    Public Property Time_eve_to1 As String
    Public Property Date_Col1 As String
    Public Property Official_Hours1 As String
    Public Property Day_Shift_Hours1 As String
    Public Property Day_Shift_Extra_Hours1 As String
    Public Property Month_Official_Hours1 As String
    Public Property Month_Shift_Hours1 As String
    Public Property Month_Shift_Extra_Hours1 As String
    Public Property Shift_Col1 As String
    Public Property Month_No1 As String
    Public Property from_date1 As String
    Public Property to_date1 As String
    Public Property Job1 As String

End Class
  • 删除
    作为SqlDataReader的Dim rd
    它从未使用过
  • 将conn创建移到循环外部,并在构造函数中传递连接字符串。您不希望在循环的每次迭代中都有新的连接
  • 将cmd创建移到循环之外。与连接的原因相同
  • 删除
    如果不是conn就是Nothing,那么conn.Close()
    您刚刚创建了它,它怎么可能是Nothing或Open
  • 将“conn.Open()移动到循环外部。您不想在每次迭代中打开另一个连接。 将其直接放在循环之前,以便在最后可能的时刻打开循环
  • 删除
    Dim ds As DataSet=New DataSet
    ds.Clear()'?
    它从未使用过。顺便说一句,VB中的注释字符是单引号而不是反勾号
  • 为什么有两个命令?cmd和mycommand?我把它们合并起来
  • 将参数添加到循环外部的命令参数集合中。参数不会改变,只有它们的值会改变。使用Add方法。检查数据库中是否有正确的SqlDbType
  • 在循环中引用参数集合的成员并设置其值
  • 格式(从_Date.EditValue(“yyyy”)
    看起来不正确
  • Using…End Using块确保即使出现错误,也会关闭并释放数据库对象
  • Private子操作码2()
    将conn用作新的SqlConnection(“用户Id=”&sb2.UserID&“;Pwd=”&sb2.Password&“;数据源=”&sb2.DataSource&“;初始目录=”&sb2.InitialCatalog&“;MultipleActiveResultSets=true”)
    使用mycommand作为新的SqlCommand(“插入SCEDUL(emp_代码、名称、日期、移位、Mor_from、Mor_to、eve_from,”_
    &eve\u to,BasicSalary\u col,Hour\u Value\u col2,Over\u Hours\u col2,Trans\u Value\u col2_
    &“总加班时间2、月号、创建用户、创建日期、创建时间、Ikama时间、正式工作时间、日班工作时间”_
    &“日班加班、月班加班、月班加班、月正式加班、从年月日到年月日、展厅类别、工作、年份”_
    &“值(@emp_code、@Name_col、@Date_col、@Shift_col、@Mor_from、@Mor_to、@eve_from,”_
    &“@eve\u to、@BasicSalary\u col、@Hour\u Value\u col2、@Over\u Hours\u col2,”_
    &“@Trans_Value_col2、@Total_加班_col2、@Month_No、@create_user、@create_date、@create_time、@Ikama_No、@Official_Hours、@Day_Shift_Hours,”_
    &“@Day\u Shift\u Extra\u Hours、@Month\u Shift\u Extra\u Hours、@Month\u Shift\u Official\u Hours、@from\u date、@to\u date、@ShowRoom\u col、@Job、@Year\u No)”,康涅狄格州)
    mycommand.Parameters.Add(“@ShowRoom\u col”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Job”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@emp_code”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Name\u col”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Date\u Col”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Shift\u Col”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Mor_from”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Mor_to”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@eve\u from”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@eve\u to”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@BasicSalary\u col”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Hour\u Value\u col2”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Over\u Hours\u col2”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Trans\u Value\u col2”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Total\u加班费\u col2”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Month\u No”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Ikama_No”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@create_user”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@create_date”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@create_time”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Official_Hours”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Day\u Shift\u Hours”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Day\u Shift\u Extra\u Hours”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Month\u Official\u Hours”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Month\u Shift\u Hours”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Month\u Shift\u Extra\u Hours”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@from_date”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@to_date”,SqlDbType.VarChar)
    mycommand.Parameters.Add(“@Year\u No”,SqlDbType.VarChar)
    康涅狄格州公开赛
    对于Z为整数=0的Dawam_Grid.Items.Count-1
    mycommand.Parameters(“@ShowRoom\u col”).Value=Dawam\u Grid.Items(Z).Cells(“ShowRoom\u col1”).Value.ToString
    mycommand.Parameters(“@Job”).Value=Dawam_Grid.Items(Z).Cells(“Job1”).Value.ToString
    mycommand.Parameters(“@emp_code”).Value=Dawam_Grid.Items(Z).Cells(“emp_Co