Jump to content

[Resolvido] [Dúvida] Como colocar DateTime no select command ?


Recommended Posts

Posted (edited)

Boa tarde,

estou com um problema em passar para um comando select uma data (SelectedDate) para um comando:

protected override void OnLoad(EventArgs e)
{
	base.OnLoad(e);
	string cnnString = ConfigurationManager.ConnectionStrings["BaseDadosSQL"].ConnectionString;

	DateTime dataSelect = Calendar2.SelectedDate;
	label1.Text = dataSelect.ToShortDateString();
	SqlConnection cnn = new SqlConnection(cnnString);

string seleccao = "SELECT categoria FROM Despesas WHERE data = "; //<<<------- ????


	SqlCommand cmd = new SqlCommand(seleccao, cnn);

	cnn.Open();
	SqlDataReader DR = cmd.ExecuteReader();

	while (DR.Read())
		categoriaList.Items.Add(string.Format("{0}", DR.GetString(0)));

	DR.Close();

	cnn.Close();
}

O meu problema é não saber como passar uma variável dateTime para dentro do comando.

essa data é suposto ter só a data e não o tempo.

Edited by Caça
GeSHi
Posted (edited)

Olá,

Experimenta assim (não testei):

protected override void OnLoad(EventArgs e)
{
		base.OnLoad(e);
		string cnnString = ConfigurationManager.ConnectionStrings["BaseDadosSQL"].ConnectionString;

	DateTime dataSelect = Calendar2.SelectedDate;
		label1.Text = dataSelect.ToShortDateString();
	SqlConnection cnn = new SqlConnection(cnnString);

string seleccao = "SELECT categoria FROM Despesas WHERE data = @Data";


		SqlCommand cmd = new SqlCommand(seleccao, cnn);

cmd.Parameters.Add(new SqlParameter("@Data", dataSelect.Date));

		cnn.Open();
		SqlDataReader DR = cmd.ExecuteReader();

	while (DR.Read())
				categoriaList.Items.Add(string.Format("{0}", DR.GetString(0)));

		DR.Close();

		cnn.Close();
}
Edited by Caça
GeSHi

Pedro Martins

Sharing is Knowledge!

http://www.linkedin.com/in/rechousa

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.