Solved

ODBCCommand & DateTime

Posted on 2006-11-21
7
1,997 Views
Last Modified: 2008-01-09
Hi,

I have two problems with my code...
1. about DateTime, when I acquire a DateTime, it contains AM/PM in my native language, which MySQL cannot understands.  How can I solve this problem?
2. I try to add attributes into SQL commands, but I got this error message "invalid attributes".  How exactly I can pass the correct values in parameters?

Here is my code..
========================
OdbcCommand dc;
OdbcConnection conn;

using (conn = new OdbcConnection(@"Driver={MySQL Connector/ODBC v5 Driver};SEVER=localhost;UID=root;PWD=root;Database=reporter;"))
 {
     string user_id = obj.user_id;
     DateTime date = obj.date;

     dc = conn.CreateCommand();
     conn.Open();
     dc.CommandText = "use reporter;";
     dc.ExecuteNonQuery();


     dc = new OdbcCommand("insert into rpt_carrier_id (user_id, date) " +
         "values (?, ?)", conn);
     dc.Parameters.Add("", OdbcType.VarChar, 32).Value = user_id;
     dc.Parameters.Add("", OdbcType.DateTime).Value = date;

     dc.ExecuteNonQuery();
}
0
Comment
Question by:xenia27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 

Author Comment

by:xenia27
ID: 17993702
Is there anyway that AM/PM won't be shown???
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 17993800
Hi xenia,

1. You could manually format the date to whichever format you want. For example, you can use date.ToString("MM-dd-yyyy") to get your date in a 11-22-2006 format, and add the date as a string parameter.

2. You should name the sql parameters:

dc = new OdbcCommand("insert into rpt_carrier_id (user_id, date) values (@id, @date)", conn);
dc.Parameters.Add("@id", OdbcType.VarChar, 32).Value = user_id;
dc.Parameters.Add("@date", OdbcType.DateTime).Value = date;

HTH,
Razzie
0
 

Author Comment

by:xenia27
ID: 17993809
I am still having this error code.."ERROR [HY092][MySQL][MyODBC 5.00.05] Invalid attribute/option identifier"
I am using ODBCcommand to set the parameters.  Do I still use the same way to set the parameters?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Razzie_
ID: 17994301
Hmm well I'm not sure about this. I always use the Mysql .NET driver instead of ODBC (http://dev.mysql.com/downloads/connector/net/1.0.html) since it is mysql specific. You can always give it a try if you want. It is easy. Just install it, reference the .dll in your VS.NET project, and you can use it.

Anyway, to answer your question, I'm not sure about the parameter syntax. Try:

dc = new OdbcCommand("insert into rpt_carrier_id (user_id, date) values (?, ?)", conn);
dc.Parameters.Add("@user_id", OdbcType.VarChar, 32);
dc.Parameters["@user_id"].Value = user_id;
dc.Parameters.Add("@date", OdbcType.DateTime);
dc.Parameters["@date"].Value = date;

You can give it a try. Sorry I can't test it for you, but do not have a mysql database with ODBC connector here to test it for you!
HTH,
Razzie
0
 

Author Comment

by:xenia27
ID: 17994312
I still have  that error message...What exactly it means "Invalid attribute" or "Invalid option identifier"??  I do believe all my attributes are correct...
0
 
LVL 8

Accepted Solution

by:
Razzie_ earned 500 total points
ID: 17994929
Well after searching the net for this, it seems this is a problem with MDAC / ODBC. Some few links with info about this:

http://groups.google.com/group/microsoft.public.sqlserver.connect/msg/b1b1c54950503bdf?hl=en&lr=&ie=UTF-8
http://www.webservertalk.com/message1201947.html

I really hope this helps. Try reinstalling ODBC / MDAC drivers.

Good luck,
Razzie
0
 

Author Comment

by:xenia27
ID: 18005384
OK...that's why I cannot run the code...thank you~~~
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question