我想使sql与datagridview所选行中的所有值一致。在第8列中找到的值我尝试了这段代码,使其仅给出一行
Dim dgvRow As DataGridViewRow
For Each dgvRow In DataGridView1.SelectedRows
dt = New DAL().selectdatatable(String.Format("SELECT PT.PT_Name, PT.PT_Agey, PT.PT_Agem, PT.PT_Aged, PT.PT_Date, Result.PT_code, Result.Test_No, Tests.Test_Name, Result.Result, Result.test_ref,Result.test_unit, Result.test_comm, Result.test_date,Result.HorL, PT.PT_Gender, PT.fullname2, PT.age_sex2,PT.PT_Nikname, DR.Dr_Name,Tests.Test_Name,Tests.upnormalshow, Subcategory.Subcat_Name, Maincategory.Maincatname, labdetails.labnme, labdetails.labspecial, labdetails.labadress, labdetails.labphone, labdetails.labtime, labdetails.lablogo, labdetails.labprint, labdetails.labnameenglish, labdetails.labspecialenglish FROM labdetails, ((((PT INNER JOIN (Result INNER JOIN Tests ON Result.Test_No = Tests.Test_No) ON PT.PT_Code = Result.PT_code) INNER JOIN DR ON PT.DR_Code = DR.Dr_Code) INNER JOIN Gender ON (Tests.Test_No = Gender.Test_ID) AND (PT.ageperiod2 = Gender.PT_Gender)) INNER JOIN Subcategory ON Tests.Subcat_ID = Subcategory.Subcat_ID) INNER JOIN Maincategory ON Tests.Maincat_ID = Maincategory.Maincat_ID WHERE (((Result.PT_code)={0}) AND ((Tests.Test_No)<>10 and (Tests.Test_No)<> 11 AND (Tests.Test_No)<> 12 AND (Tests.Test_No)<> 13) and (Tests.Test_No<> 14) and (Tests.Test_No<> 15) and (Tests.Test_No<> 18) and (Tests.Test_No<> 20) and (Tests.Test_No <> 21) and Tests.Test_No <> 42 and Tests.Test_No <> 76 and Tests.Test_No <> 77 and Tests.Test_No <> 78 and Tests.Test_No <> 79 and Tests.Test_No <> 80 and Tests.Test_No <> 81 and Tests.Test_No <> 92 and Tests.Test_No = {4} AND ((Result.recorded))=Yes) and Result.test_date=#{2}# and PT.PT_Date=#{1}# and Result.PT_ID={3} ORDER BY Result.Test_No;", Txtcode.Text, DateTimePicker1.Value.ToString("MM/dd/yyyy"), DateTimePicker1.Value.ToString("dd/MM/yyyy"), Texid.Text, dgvRow.Cells(8).Value))
Next
答案 0 :(得分:0)
基于我对您要说的话的理解:
我认为您正在尝试基于datagridview的所选行制作一个sql
只需使用:DataGridView1.CurrentRow.Cells("GridviewColumnNameHere").Value
希望这会有所帮助。
无需循环。
Dim sql As String = "Select * from TableName where ColumnName = @columnName"
Dim conn As New MySqlConnection()
Dim command As MySqlCommand = New MySqlCommand(sql, conn)
command.Parameters.AddWithValue("@columnName", DataGridView1.CurrentRow.Cells("ColumnNameInGridview").Value)
答案 1 :(得分:0)
似乎您的selectdatatable函数返回一个DataTable。问题是您的变量dt在每次迭代中都会被覆盖,因此将以单行结尾。我已经调整了Where子句以使用参数。 Load方法将合并每次迭代返回的记录。我认为这仅在返回主键时才有效。因为我不得不猜测,您将不得不检查字段的数据类型。确保将参数值转换为正确的类型。对我来说,Tests.Test_No不等于一堆数字然后等于另一个数字对我来说没有多大意义。
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim dt As New DataTable
Using cn As New SqlConnection("Your connection string")
Using cmd As New SqlCommand("Select...WHERE
Result.PT_code=@PTCode
AND Tests.Text_No Not In (10,11,12,13,14,15,18,20,21,42,76,77,78,79,80,81,92)
AND Tests.Test_No = @TextNo
AND Result.recorded=Yes
AND Result.test_date=@TestDate
AND PT.PT_Date=@PTDate
AND Result.PT_ID=@TexID
ORDER BY Result.Test_No;", cn)
cmd.Parameters.Add("@PTCode", SqlDbType.VarChar).Value = Txtcode.Text
cmd.Parameters.Add("@TextNo", SqlDbType.Int)
cmd.Parameters.Add("@TestDate", SqlDbType.DateTime).Value = DateTimePicker1.Value
cmd.Parameters.Add("@PTDate", SqlDbType.DateTime).Value = DateTimePicker1.Value
cmd.Parameters.Add("@TextID", SqlDbType.VarChar).Value = Texid.Text
cn.Open()
For Each dgvRow As DataGridViewRow In DataGridView1.SelectedRows
cmd.Parameters("@TextNo").Value = CInt(dgvRow.Cells(8).Value)
dt.Load(cmd.ExecuteReader)
Next
End Using
End Using
End Sub