Answered Conversions from c# to odbc sql server

alti

New member
Joined
Jun 8, 2020
Messages
1
Programming Experience
Beginner
hello ,

im having a problem converting from datetime c# to datetime sql server using odbc and time c# to time(7) sql server this is my code:

C#:
command.CommandText = "INSERT INTO pointer (Nom_employe, Date_Heure , Matin, Midi, apresmidi, Soir) VALUES ( ? ,? , ? , ?, ?, ?);";

// command.Parameters.AddWithValue("datetime", date_heure);
// command.Parameters.AddWithValue("nom", nom_employe);
command.Parameters.Add("@Nom", OdbcType.VarChar).Value = nom_employe;
command.Parameters.Add("@Date", OdbcType.Date).Value = Convert.ToDateTime( date_heure);

if (matin == null)
{
    command.Parameters.Add("@Matin", OdbcType.Time).Value = null;
}
else
{
    command.Parameters.Add("@Matin", OdbcType.Time).Value = TimeSpan.Parse(matin);
}

if (midi == null)
{
    command.Parameters.Add("@Midi", OdbcType.Time).Value = null;
}
else
{
    command.Parameters.Add("@Midi", OdbcType.Time).Value = TimeSpan.Parse(midi);
}

if (Apresmidi == null)
{
    command.Parameters.Add("@Apresmidi", OdbcType.Time).Value = null;
}
else
{
    command.Parameters.Add("@Apresmidi", OdbcType.Time).Value = TimeSpan.Parse( Apresmidi);
}

if (Soir == null)
{
    command.Parameters.Add("@Soir", OdbcType.Time).Value = null;
}
else
{
    command.Parameters.Add("@Soir", OdbcType.Time).Value = TimeSpan.Parse(Soir);
}

this is the error i am getting now : 'ERROR [HYC00] [Microsoft][ODBC SQL Server Driver]Fonctionnalité optionnelle non implémentée'

but befor this i had other errors about conversions and i dedn't manage to fix so i think the problem is still here ?

can i have some help please i have to deliver soon?
 
Last edited by a moderator:
I'm not sure whether this is the only issue there but it's definitely AN issue. When using ADO.NET, you don't use the C# null to represent a database NULL. The reason for that is because it couldn't be used for value types back when ADO.NET was originally created. Microsoft included the DBNull class specifically for the representation of database NULLs and it has a static Value property that returns an instance that you use when getting or setting database NULLs, e.g.
C#:
command.Parameters.Add("@Apresmidi", OdbcType.Time).Value = Apresmidi == null
                                                                ? (object) DBNull.Value
                                                                : TimeSpan.Parse(Apresmidi);
 
By the way, if you are using a DSN or have some other reason that you must use ODBC then fair enough but, as a first choice, you ought to use SqlClient for SQL Server rather than Odbc.
 
C#:
Parameters.AddWithValue("@date", DateTime.Now.ToString("dd-MM-yyyy"));
Works for me. Remove your white space @ Date_Heure , keep your code tight and clean. Code maid is good for that if you are sloppy.
 

Latest posts

Back
Top Bottom