在C#中的特定日期从sql服务器数据库中检索数据; [英] Retrieving the Data from sql server database on a particular date in C#;
本文介绍了在C#中的特定日期从sql服务器数据库中检索数据;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我正在使用以下代码将数据插入datebase
Hi
I''m using the following code to insert data into datebase
comm.CommandText = "insert into Dairy (Date, Dairy_Content) values(@date, @Content)";
comm.Parameters.Add(new SqlParameter("@date",dateTimePicker1.Value.Date));
comm.Parameters.Add(new SqlParameter("@Content", richTextBox1.Text));
conn.Open()
try
{
comm.Connection = conn;
comm.ExecuteNonQuery();
richTextBox1.Clear();
MessageBox.Show("saved to database");
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Error :";
msg += ex.Message;
MessageBox.Show(msg);
}
finally
{
conn.Close();
}
我需要代码以在特定的选定日期从数据库中检索数据.我使用下面的代码来获取数据,但它给出了错误
I need code to retrieve the data from database on a particular selected date.I used the below code to get the data but it is giving error
string con = @"Data Source=MAHESH\SQLEXPRESS;Initial Catalog=MyDairy;User ID=sa;Password= Mahesh2648";
SqlConnection conn = new SqlConnection(con);
SqlCommand comm;
SqlDataReader dr;
string str = "select Dairy_Content from Dairy where Date = " + dateTimePicker1.Value.Date.ToShortDateString() + " ";
comm = new SqlCommand(str, conn);
try
{
conn.open();
dr = comm.ExecuteReader();
if(dr.Read())
richTextBox1.Text = dr[0].ToString();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Error :";
msg += ex.Message;
MessageBox.Show(msg);
}
finally
{
dr.close;
conn.Close();
}
推荐答案
在日期前后加上单引号.
Put single quotes around the date.
where Date = '" + dateTimePicker1.Value.Date.ToShortDateString() + "' ";
这篇关于在C#中的特定日期从sql服务器数据库中检索数据;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文