C#如何根据时间访问从数据库中获取值并将其显示在文本框中 [英] C# how to fetch value from database and display it on a textbox based on time- access

查看:123
本文介绍了C#如何根据时间访问从数据库中获取值并将其显示在文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在google上找到的这段代码.
我试图从数据库中获取值并将其显示在文本框中.基于时间

I am trying this code i found on google.
and i''m trying to fetch value from database and display it on a textbox. based it on a time

OleDbConnection cn = new OleDbConnection();
            cn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\`Hp\Desktop\PROGRAM1\notificationSystem\notificationSystem\Database.accdb";
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = cn;

     

            string cmdText = "SELECT * FROM news " +
                         "WHERE npublished BETWEEN ? AND ?";

            DateTime dt = this.metroDateTime1.Value.Date;
            DateTime dt2 = this.metroDateTime1.Value.Date.AddMinutes(1440);
            cmd.CommandText = cmdText;
            cmd.Parameters.AddWithValue("@p1", dt);
            cmd.Parameters.AddWithValue("@p2", dt2);
            cmd.Parameters.AddWithValue("@article", metroTextBox2.Text);
            OleDbDataAdapter adapter = new OleDbDataAdapter(cmdText, cn);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            Form5 f5 = new Form5();
            f5.newsGrid.DataSource = ds.Tables[0];
            cn.Close();



我应该怎么办?我想念什么吗?
任何帮助将不胜感激.

我尝试过的事情:

尝试了上面的代码,但无法正常工作.



What should i do? do i miss something?
Any help would be gladly appreciated.

What I have tried:

Tried the code above but it won''t work.

推荐答案



您在查询中缺少参数名称.您正在cmd(命令对象)检查中发送3个参数并更正查询.


Ashish Nigam
Hi,

you are missing parameter Names on your Query. You are sending 3 parameters in your cmd (command object) check and correct your query.


Ashish Nigam


这篇关于C#如何根据时间访问从数据库中获取值并将其显示在文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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