Results 1 to 5 of 5

Thread: Problem of MySqlParameter in c#

  1. #1
    Join Date
    Nov 2008
    Posts
    996

    Problem of MySqlParameter in c#

    Here I created a generic class called Object which is supposed to be able to load / save / change any of my business objects. Each one of my business objects are inherited a generic class called Table, it possesses an attribute that most defines the name of the table and the primary key of the table base. Each of the properties have an attribute correspondent to the name and type of field base. That's the context.

    My problem is with the backup. Whenever I want to save an object, I loop on the attribute and I get the name of fields and the associated value, I built my request like this.

    The structure of the request is correct but the valuers are not replaced. For example, to type a query I will insert:
    Code:
    INSERT INTO test (id, value) values (@ID, @value)
    It does not replace IDs parameter values.
    Here is my code, I hope I have been quite clear:

    Code:
    public static void SaveObject (AGDDataTable poObject) 
    {
    MySqlConnection cn;
    MySqlParameter param;
    MySqlCommand cmd; 
    String sSqlUpdate;
    String sSqlInsertFields;
    String sSqlInsertValues;
    
    Type type = poObject.GetType (); 
    
    AGDTable atrTable = ((AGDTable []) type.GetCustomAttributes (typeof (AGDTable), true)) [0]; 
    String sTableName = atrTable.TableName; 
    String sPrimaryKey = atrTable.PrimaryKey; 
    String sVirgule = "";
    
    sSqlUpdate = "UPDATE " + sTableName + " SET ";
    
    sSqlInsertFields = "INSERT INTO " + sTableName + "(" + sPrimaryKey;
    sSqlInsertValues = "VALUES (@ ID"; 
    
    foreach (PropertyInfo pi in type.GetProperties ()) / / Construction of the request from the properties in my class 
    {
    AGDField [] aoField = (AGDField []) pi.GetCustomAttributes (typeof (AGDField), false); 
    if (aoField.Length> 0) 
    {
    String sFieldName = aoField[0].FieldName;
    
    sSqlUpdate += sVirgule + sFieldName + " = @" + sFieldName;
    sSqlInsertFields += ", " + sFieldName;
    sSqlInsertValues += ", @" + sFieldName;
    
    sVirgule = ", ";
    }
    }
    
    sSqlUpdate += " WHERE " + sPrimaryKey + " = @ID";
    sSqlInsertFields += " )";
    sSqlInsertValues += " )";
    
    cn = new MySqlConnection(Properties.Resources.SECUConnectionString);
    cn.Open (); 
    
    if (poObject.Id> 0) 
    {
    cmd = new MySqlCommand (sSqlUpdate, cn); 
    }
    else 
    {
    poObject.Id = GetNextId(sTableName);
    
    cmd = new MySqlCommand (sSqlInsertFields + sSqlInsertValues, cn); 
    }
    
    foreach (PropertyInfo pi in type.GetProperties ()) / / Creating parameters 
    {
    AGDField [] aoField = (AGDField []) pi.GetCustomAttributes (typeof (AGDField), false); 
    if (aoField.Length> 0) 
    {
    / / Get the attribute AGDField to know the name of the field and type in base 
    AGDField field = aoField [0]; 
    param = new MySqlParameter (field.FieldName, field.DBType); 
    
    / / Get the value of this property for this instance of an object 
    param.Value = pi.GetValue (poObject, null); 
    cmd.Parameters.Add (param); 
    }
    }
    
    param = new MySqlParameter (sPrimaryKey, MySqlDbType.Int64); 
    param.Value = poObject.Id; 
    cmd.Parameters.Add (param); 
    
    try 
    {
    cmd.ExecuteNonQuery (); 
    }
    catch (Exception excp) 
    {
    Exception myExcp = new Exception("Could not Save " + poObject.GetType().Name + ". Error: " + excp.Message, excp); 
    throw (myExcp); 
    }
    
    }
    Do someone know why my settings are not taken into account?

  2. #2
    Join Date
    May 2008
    Posts
    3,971

    Re: Problem of MySqlParameter in c#

    1) Creating a class called Object is dangerous because there is already a class Object in the tree of .NET classes, which inherit all classes

    2) your parameters are not taken into account, I do not know what to do with concatenations of your chains, but it means nothing. Why not do something like this

    Code:
    String req = "INSERT INTO Table(field1, field2) VALUES(?field1, ?field2); 
    MySqlCommand cmd = new MySqlCommand (connection, req); 
    cmd.Parameters.Add ( "? field1", value); 
    cmd.Parameters.Add ( "? field2", value); 
    cmd.ExecuteNonQuery (); 
    ...
    instead of your foreach?

  3. #3
    Join Date
    Nov 2008
    Posts
    996

    Re: Problem of MySqlParameter in c#

    For the first remark, it is not called Object. Then I do not know the fields or the values when I call this function. It receives an object type AGDDataTable which can represent my application. Hence the usefulness of my foreach loop that I used to recover the fields and the values:

    Code:
    [AGDTable ( "SESSION", "ID_SESSION")] 
    public class Session: AGDDataTable 
    {
    [AGDField("ID_LOGIN", AGDDBType.BigInt, typeof(Login))]
    public Login Login { get; set; }
    
    [AGDField("ID_BD", AGDDBType.BigInt, typeof(BD))]
    Public BD BD { get; set; }
    
    [AGDField("DATEHDEBUT_SESSION", AGDDBType.BigInt)]
    public Int64 DateDebut { get; set; }
    
    [AGDField("DATEHFIN_SESSION", AGDDBType.BigInt)]
    public Int64 DateFin { get; set; }
    
    [AGDField("IP_SESSION", AGDDBType.Char)]
    public String Ip { get; set; }
    
    [AGDField("DTAEHMANIF_SESSION", AGDDBType.BigInt)]
    public Int64 DateManif { get; set; }
    
    [AGDField("ID_DATETRAITEMENT", AGDDBType.BigInt)]
    public Int64 DateTraitement { get; set; }
    
    [AGDField("CONTEXT_SESSION", AGDDBType.Blob)]
    public String Context { get; set; }
    }
    Do you know what I am trying to do or I'm not clear?

  4. #4
    Join Date
    Feb 2008
    Posts
    194

    Re: Problem of MySqlParameter in c#

    Do you use a ORM? Your metadata in your header methods forced me to say so.

  5. #5
    Join Date
    Nov 2008
    Posts
    996

    Re: Problem of MySqlParameter in c#

    No I do not use ORM.

    In fact I have a little more researched on my bug. I described my DBType in MySqlDBType and it works.

Similar Threads

  1. Sony VGNCS3 laptop 15 inch screen problem...LCD or CHIP problem?
    By Mick$Tyler in forum Hardware Peripherals
    Replies: 5
    Last Post: 31-10-2010, 06:49 AM
  2. Replies: 6
    Last Post: 27-07-2010, 12:10 AM
  3. Replies: 4
    Last Post: 10-04-2010, 04:19 PM
  4. AD problem in Server 2003, Userenv Event ID 1053 problem
    By Victor Kam in forum Active Directory
    Replies: 3
    Last Post: 24-09-2007, 04:44 PM
  5. Replies: 1
    Last Post: 30-11-2004, 11:57 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,777,386.84846 seconds with 16 queries