如何从数据库中选择日期并将所选值设置为datetimepicker [英] How to get selected date from database and set the selected value to datetimepicker

查看:96
本文介绍了如何从数据库中选择日期并将所选值设置为datetimepicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从数据库中获取日期值,并在使用dataReader时将所选值设置为我的datetimpicker。



[edit]已添加代码块 - OriginalGriff [/编辑]



我尝试过:



  public   void  loadVoucher()
{
connection = new SqlConnection(DBHelper.ConnectionString());
connection.Open();
command = new SqlCommand();
command.Connection = connection;
command.CommandText = select * from lease where linovice =' + txtLeaseVoucherNo.Text + ';
da = new SqlDataAdapter();
da.SelectCommand = command;
ds = new DataSet();
da.Fill(ds);

尝试
{


reader2 = command.ExecuteReader();
{
while (reader2.Read())
{

txtName.Text =(reader2 [ client_name]。ToString());
txtcFname.Text =(reader2 [ f_name]。ToString());
txtCNIC.Text =(reader2 [ cnic]。ToString());
txtGName.Text =(reader2 [ g_name]。ToString());
txtGFname.Text =(reader2 [ g_fname]。ToString());
txtGCnic.Text =(reader2 [ g_cnic]。ToString());
txtGPhone.Text =(reader2 [ g_phone]。ToString());
txtGPhone2.Text =(reader2 [ g_phone2]。ToString());
txtGAdress.Text =(reader2 [ g_adress]。ToString());
txtGadress2.Text =(reader2 [ g_adress2]。ToString());
txtTotalBill.Text =(reader2 [ total_bill]。ToString());
txtAdvance.Text =(reader2 [ advance]。ToString());
txtPlan.Text =(reader2 [ lplan]。ToString());
txtLeaseAmount.Text =(reader2 [ lease_amu]。ToString());
txtInstalment.Text =(reader2 [ linstalment]。ToString());
txtSMName.Text =(reader2 [ sales_men]。ToString());
txtInspName.Text =(reader2 [ insp]。ToString());
comboBoxReceivedIn.Text =(reader2 [ GName]。ToString());
txtCurrentBal.Text =(reader2 [ balbefore]。ToString());
txtNewCurrentBal.Text =(reader2 [ balafter]。ToString());
DateTime datevalue;
if (DateTime.TryParse(reader2 [ startsfrom]。ToString(), out datevalue))
{
dateTimePickerStartsFrom.Value = datevalue;
}
其他
{
// < span class =code-comment> TODO - 转换失败...不是有效的日期时间
// 打印出来,看看它有什么问题......
}

txtChecque.Text =(reader2 [ checqueNo]。ToString());
txtPageNo.Text =(reader2 [ pageNo]。ToString());


}

}
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message);


}
最后
{
connection.Close();
}

}

解决方案

首先,永远不要那样做!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。

其次,为什么使用循环来检索数据?你总是将它加载到同一个地方,所以看起来很傻 - 最后一组值将始终是唯一显示的值。

第三,为什么要将数据库值转换为字符串,然后将其解析回DateTime值?如果它是数据库中的DateTime - 如果它不是它应该是 - 那么一个简单的演员将给你的价值,而不需要检查任何东西。



所以使用参数化查询,删除循环并检查行数(多个是某个地方的问题),确保您的数据库有teh ciorrect数据类型列,并用转换替换解析:

 dateTimePickerStartsFrom.Value =(DateTime)reader2 [ ]开始; 


I have to get the date value from database and set that selected value to my datetimpicker while using dataReader.

[edit]Code block added - OriginalGriff[/edit]

What I have tried:

public void loadVoucher()
        {
            connection = new SqlConnection(DBHelper.ConnectionString());
            connection.Open();
            command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "select * from lease where linovice='" + txtLeaseVoucherNo.Text + "'";
            da = new SqlDataAdapter();
            da.SelectCommand = command;
            ds = new DataSet();
            da.Fill(ds);

            try
            {


                reader2 = command.ExecuteReader();
                {
                    while (reader2.Read())
                    {

                        txtName.Text  = (reader2["client_name"].ToString());
                        txtcFname.Text= (reader2["f_name"].ToString());
                       txtCNIC.Text   = (reader2["cnic"].ToString());
                       txtGName.Text  = (reader2["g_name"].ToString());
                       txtGFname.Text = (reader2["g_fname"].ToString());
                       txtGCnic.Text  = (reader2["g_cnic"].ToString());
                       txtGPhone.Text = (reader2["g_phone"].ToString());
                       txtGPhone2.Text= (reader2["g_phone2"].ToString());
                       txtGAdress.Text= (reader2["g_adress"].ToString());
                       txtGadress2.Text= (reader2["g_adress2"].ToString());
                       txtTotalBill.Text = (reader2["total_bill"].ToString());
                       txtAdvance.Text = (reader2["advance"].ToString());
                       txtPlan.Text = (reader2["lplan"].ToString());
                       txtLeaseAmount.Text = (reader2["lease_amu"].ToString());
                       txtInstalment.Text = (reader2["linstalment"].ToString());
                       txtSMName.Text = (reader2["sales_men"].ToString());
                       txtInspName.Text = (reader2["insp"].ToString());
                       comboBoxReceivedIn.Text = (reader2["GName"].ToString());
                       txtCurrentBal.Text = (reader2["balbefore"].ToString());
                       txtNewCurrentBal.Text = (reader2["balafter"].ToString());
                       DateTime datevalue;
                       if (DateTime.TryParse(reader2["startsfrom"].ToString(), out datevalue))
                       {
                          dateTimePickerStartsFrom.Value= datevalue;
                       }
                       else
                       {
                           // TODO - convertion failed... not a valid datetime
                           // Print it out and see what is wrong with it...
                       }
                       
                        txtChecque.Text = (reader2["checqueNo"].ToString());
                       txtPageNo.Text = (reader2["pageNo"].ToString());
                       

                    }
                   
                }
            }
            catch (Exception ex)
            {
                //  MessageBox.Show(ex.Message);


            }
            finally
            {
                connection.Close();
            }

        }

解决方案

First off, never do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Second, why are you using a loop to retrieve data? You always load it into the same place, so a look is silly - the last set of values will always be the only one displayed.
Third, why are you converting the database value to a string, then parsing it back to a DateTime value? If it's a DateTime in the database - and if it isn't it should be - then a simple cast will give your the value without needing to check anything.

So use a parameterised query, drop the loop and check the number of rows instead (more than one is a problem somewhere), make sure your DB has teh ciorrect datatype columns, and replace the parse with a cast:

dateTimePickerStartsFrom.Value = (DateTime) reader2["startsfrom"];


这篇关于如何从数据库中选择日期并将所选值设置为datetimepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆