简体   繁体   English

如何使用WinForm datetimepicker C#在SQL数据库中搜索

[英]How to searching in SQL database using WinForm datetimepicker C#

I created a model that connects SQLserver 2014 and VisalStudio2013(.NET 4,5) , Visal C# .In VB I have two forms аnd in SQL one table with records. 我创建了一个连接SQLserver 2014和VisaalStudio2013(.NET 4,5)和Visal C#的模型。在VB中,我有两种形式,在SQL中有记录的一个表。

In fist Winform I used "textbox" wich type is nvarchar (10) for searching in SQL database. 在Winform中,我在SQL数据库中使用的“文本框”类型为nvarchar (10)。

**But in the second Winform I want to searching in SQL database using "datetimepicker" where the type of colum is date ** **但是在第二个Winform中,我想使用“ datetimepicker”在SQL数据库中搜索,其中列的类型为日期 **

What must change in my code for this seach.Becouse give me : Error 7 Operator '==' cannot be applied to operands of type 'System.DateTime?' 我必须对此代码进行什么更改。因为给我:错误7运算符'=='不能应用于类型'System.DateTime'的操作数吗? and 'int 和'int

If must white new code ,what should be ? 如果必须白色新代码,应该是什么?

This is the code from first form for searching 这是第一种形式的搜索代码

        textKkKod1.Focus();

        loadSearchData();

        int kochani;
        try
        {
            kochani = Convert.ToInt32(this.textKkKod1.Text);
        }
        catch
        {
            MessageBox.Show("Error 404");
            return;
        }
        string kodNaKonduktor = this.textKkKod1.Text;
        var koch = db.Koches.Where(k => k.CODEK == kochani).FirstOrDefault();

        if (koch == null)
            return;

        this.textKkKod1.Text = Convert.ToString(koch.CODEK);
        //this.textKkKochaniData1.Value = Convert.ToDateTime(koch.DATER);
        this.textKkRazpiska1.Text = koch.Razpiska;



    }

    private void loadSearchData()
    {



        int kochani;
        try
        {
            kochani = Convert.ToInt32(this.textKkKod1.Text);
        }
        catch
        {
            MessageBox.Show("Eror 404");
            return;
        }
        kochBindingSource.DataSource = db.Koches.Where(k => k.CODEK == kochani).ToList();

        this.gridKochaniKonduktor1.DataSource = this.kochBindingSource.DataSource;


    }

The error you are getting is saying that you are trying to compare a date to an integer. 您得到的错误是您正在尝试将日期与整数进行比较。 You cannot compare two objects of different types. 您不能比较两个不同类型的对象。

From the code in your question, this is caused by line: 从您问题中的代码,这是由以下行引起的:

var koch = db.Koches.Where(k => k.CODEK == kochani).FirstOrDefault();

The same error would also appear here: 同样的错误也会出现在这里:

kochBindingSource.DataSource = db.Koches.Where(k => k.CODEK == kochani).ToList();

We know kochani is an integer, which means CODEK must be a datetime. 我们知道kochani是一个整数,这意味着CODEK必须是日期时间。 (Unless I've missed it, you haven't shown us how CODEK is declared/ initialized/ assigned too.) (除非我错过了,否则您还没有向我们展示CODEK的声明/初始化/分配方式。)

You need to either convert CODEK to an integer : (I doubt you'll be able to store a date as an integer actually, best to use long ) 您需要将CODEK转换为integer :(我怀疑您实际上是否可以将日期存储为整数,最好使用long

long codekAsLong = long.Parse(CODEK.ToString("yyyyMMddHHmmss"));

Or convert kochani to a datetime to fix this. 或将kochani转换为datetime以解决此问题。

DateTime kochaniAsDT = new DateTime(1970, 1, 1).AddMilliseconds(kochani);
//This assumes kochani represents milliseconds

-I cannot tell you which as I don't know the context in which this code is being used. -我无法告诉您哪个,因为我不知道使用此代码的上下文。 (I'm struggling with the variable names enough as it is...) (我在变量名称方面苦苦挣扎……)

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

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