如何在mysql数据库的日期之间选择一个名称

c8ib6hqw  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(360)

我会尽可能多的解释。
我有一个项目,需要显示的名称从数据库到 textboxes . 我有两个 datetimepicker 用于在日期和5之间搜索 textboxes 在mysql数据库中显示名称。例如,我在中选择起始日期2018/12/07和结束日期2019/01/07 datagridview 它将显示日期之间的所有行和列。但是,我需要的名字显示在我的5个文本框。我没有代码,因为我不知道从哪里开始。
mysqldatabase 我只有身份证,姓名,入境日期。
在我的 form : datetimepicker1 =
startdate datetimepicker2 =
enddate button1 =
generatebutton textbox1 =
nametxt1 textbox2 =
nametxt2 textbox3 =
nametxt3 textbox4 =
nametxt4 textbox5 = nametxt5 使用时更新:

mysqlconn.Open()
COMMAND.Connection = mysqlconn 
COMMAND.CommandText = "Select name from table1 where dateofentry between '" & Format(Me.startdate.Value, "yyyy-MM-dd") & "' AND '" & Format(Me.endtime.Value, "yyyy-MM-dd") & "'" 
Dim sqlresult1 As Object 
sqlresult1 = COMMAND.ExecuteScalar 
Dim str1 As String 
str1 = sqlresult1 
nametxt1.Text = str1  
mysqlconn.Close()

我的5个文本框中都显示了相同的名称
谢谢你回答这个问题。

zfycwa2u

zfycwa2u1#

在线解释和评论。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'The Using block ensures that your database objects are closed and disposed
    'even if there is an error
    Dim dt As New DataTable
    Using cn As New MySqlConnection("Your connection string")
        'Pass the Select statement and the connection to the constructor of the command
        Using cmd As New MySqlCommand("Select name from table1 where dateofentry between @Date1 AND @Date2;", cn)
            'Use parameters
            cmd.Parameters.Add("@Date1", MySqlDbType.Date).Value = DateTimePicker1.Value
            cmd.Parameters.Add("@Date2", MySqlDbType.Date).Value = DateTimePicker2.Value
            'open the connection at the last possible moment
            cn.Open()
            'Execute scalar only returns the first row, first column of the result set
            dt.Load(cmd.ExecuteReader)
        End Using
    End Using
    'Then a little link magic to dump the first column of the table
    'to an array
    Dim names() = (From row In dt.AsEnumerable()
                   Select row(0)).ToArray
    'If there are less than 5 records returned this
    'code will fail, Add a check for this and
    'adjest your code accordingly
    TextBox1.Text = names(0).ToString
    TextBox2.Text = names(1).ToString
    TextBox3.Text = names(2).ToString
    TextBox4.Text = names(3).ToString
    TextBox5.Text = names(4).ToString
End Sub
fcipmucu

fcipmucu2#

SELECT *
FROM `tblPerson`
WHERE (date_field BETWEEN '2018-12-30 14:15:55' AND '2019-01-06 10:15:55')
LIMIT 5;

text1.text = dt.Rows[0]["name"].ToString();
text2.text = dt.Rows[1]["name"].ToString();
text3.text = dt.Rows[2]["name"].ToString();
text4.text = dt.Rows[3]["name"].ToString();
text5.text = dt.Rows[4]["name"].ToString();

相关问题