将数据传输到datagrid

时间:2014-08-14 10:31:43

标签: c# sql visual-studio-2010 datagridview

我在数据库中定义了一个列,比如yyyy / mm / dd hh:hh:hh。现在,我想通过datetimepicker将我从visiual studio获得的数据传输到datagridview。有没有办法做到这一点?

这是我写的代码但我从这一行得到错误“(”select * from tablo where tablo.tarih> ='“+ dtpBaslangic.Value.ToShortDateString()”

SqlDataAdapter adp = new SqlDataAdapter();
if (radioButton1.Checked == true)
{

);

adp = new SqlDataAdapter("select * from tablo where tablo.tarih >= '" + dtpBaslangic.Value.ToShortDateString() + "' AND tablo.tarih <= '" + dtpBaslangic.Value.ToString("dd-mm-yyyy") + "'" + " 23:59:59'", baglanti);

DataSet gonder = new DataSet();
try
{
    baglanti.Open();
    adp.Fill(gonder);
    dataGridView1.DataSource = gonder.Tables[0];
    baglanti.Close();
    adp.Dispose();
}
catch
{
    MessageBox.Show("Kayıtlar Listelenemedi!", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

1 个答案:

答案 0 :(得分:0)

查看您遇到问题的行:

adp = new SqlDataAdapter("select * from tablo where tablo.tarih >= '" + dtpBaslangic.Value.ToShortDateString() + "' AND tablo.tarih <= '" + dtpBaslangic.Value.ToString("dd-mm-yyyy") + "'" + " 23:59:59'", baglanti);

我会更新它以使用参数:

SqlDataAdapter adp = new SqlDataAdapter("select * from tablo where tablo.tarih >= @StartDateTime AND tablo.tarih <= @EndDateTime", baglanti);

adp.SelectCommand.Parameters.AddWithValue("@StartDateTime", Convert.ToDateTime(dtpBaslangic.Value));

DateTime EndDate = Convert.ToDateTime(dtpBaslangic.Value);
TimeSpan ts = new TimeSpan(23, 59, 0);
EndDate = EndDate.Date + ts;

adp.SelectCommand.Parameters.AddWithValue("@EndDateTime", EndDate);