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