简体   繁体   English

使用DataGridView从数据库表中检索数据

[英]Retrieving Data from a database table using DataGridView

This is my code : 这是我的代码:

Properties.Settings.Default.In_OutConnectionString

c.Open();
// 2
// Create new DataAdapter
string textboxValue1 = textBox1.Text.Trim();
string textboxValue2 = textBox2.Text.Trim();
using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM People_Tracking WHERE Enter_Exit_Time >='textboxValue1' AND Enter_Exit_Time <='textboxValue2'", c))
{
    // 3
    // Use DataAdapter to fill DataTable
    DataTable t = new DataTable();
    a.Fill(t);
    // 4
    // Render data onto the screen
    dataGridView1.DataSource = t;
}

I have a windows form application where I will enter start-date-time and end-date-time to show result in table, but whenever I run , I have the error below: I am using visual studio 2015 . 我有一个Windows窗体应用程序,我将在其中输入开始日期时间和结束日期时间以在表中显示结果,但是每当我运行时,都会出现以下错误:我正在使用Visual Studio 2015。 It only works if i use the date time directly in the query instead of the text boxs 仅当我直接在查询中使用日期时间而不是文本框时才有效

error: "An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Additional information: Conversion failed when converting date and/or time from character string." 错误:“ System.Data.dll中发生了'System.Data.SqlClient.SqlException类型的未处理的异常。附加信息:从字符串转换日期和/或时间时转换失败。”

I think the problem is your definition of you variable in database it should be nvarchar() instead of char(). 我认为问题是您在数据库中对变量的定义应该是nvarchar()而不是char()。 Use a break point in you code to find out that your textbox values plus by some space or not 在代码中使用一个断点来找出您的文本框值是否加上一些空格

First, your syntax in query is incorrect : Enter_Exit_Time >='textboxValue1' AND Enter_Exit_Time <='textboxValue2' , you send on the query the name of textboxValue instead of its value. 首先,查询中的语法不正确: Enter_Exit_Time >='textboxValue1' AND Enter_Exit_Time <='textboxValue2' ,您在查询中发送了textboxValue的名称而不是其值。

It generates an error because you trying to send a text to a DateTime field that SQL doesn't understand (according to the error message). 由于您尝试将文本发送到SQL无法DateTime字段(根据错误消息),因此会产生错误。

I advice you to use the Parameter to use SqlDbType.DateTime and then pass the DateTime directly to the parameter, and also avoid SQL injections , like this : 我建议您使用Parameter来使用SqlDbType.DateTime ,然后将DateTime直接传递给该参数,并避免使用SQL注入,如下所示:

c.Open();
DateTime startDateTime = Convert.ToDateTime(textBox1.Text);
DateTime endDateTime = Convert.ToDateTime(textBox2.Text);
string query = "SELECT * FROM People_Tracking WHERE Enter_Exit_Time BETWEEN @startDateTime AND @endDateTime ;";
SqlCommand cmd = new SqlCommand(query, c);
cmd.Parameters.Add("@startDateTime", SqlDbType.DateTime).Value = startDateTime;
cmd.Parameters.Add("@endDateTime", SqlDbType.DateTime).Value = endDateTime;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable t = new DataTable();
adapter.Fill(t);
dataGridView1.DataSource = t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM