Groups | Blog | Home
all groups > dotnet windows forms > august 2004 >

dotnet windows forms : Incorrect SQL conversion into String::Format statement.


Przemek
8/31/2004 8:27:08 PM
Hi
A problem is - how can I make that statement correct? - I would like to
SELECT somenthink like that:

String *SQLstatement = String::Format(S" SELECT * FROM Table WHERE Date =
{0}", DateTimePicker1->Value->ToShortDateString() );

There is a String statement (*SQLstatement) so I concatenate DateTimePicker1
value as string value too. But I've got an "incorrect data type error". Is
there any
solution to make this cnvertion clear - maybe using any SQL finction inside
string?.

[Access file - "Date" column type: DateTime, oleDbDataAdapter]
Good day, thanks for wrote.

Jon Skeet [C# MVP]
8/31/2004 8:35:58 PM
[quoted text, click to view]

I would suggest not doing that in the first place. Use parameters
instead, and you don't need to do any conversion yourself.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Jakob Christensen
9/1/2004 2:29:11 AM
This is a short (untested) example of how to use parameters (in C#):

string connectionString = "...";
SqlConnection cn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT * FROM Table WHERE Date =
@dtDate", cn);

SqlParameter param = new SqlParameter("@dtDate", SqlDbType.DateTime, 8);
param.Direction = ParameterDirection.Input;
param.Value = new DateTime(2004, 1, 1);
cmd.Parameters.Add(param);

cn.Open();
using (SqlDataReader reader =
cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
for (; reader.Read(); )
{
// Do stuff
}
}

HTH, Jakob.


[quoted text, click to view]
Przemek
9/1/2004 8:53:23 AM
[quoted text, click to view]

AddThis Social Bookmark Button