Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to store a value in an online mysql database. It's a yes/no field. What is best and least problimatic? Saved as a boolean 0/-1. As a String yes/no or an integer 0/1. Thanks
Posted

Your going to have to convert everything anyway.
The fields in the table in this example would be

SQL
varchar(some length)
tinyint(1)
int(your choice)


I've had the best results as below

Hope this helps......

C#
public bool AddNewRecordInSomeTable(string sSomeString, bool bSomeBool, int iSomeNumber)
{
	bool bReturn = false;
        
	// make sure your db is open
        if (DbConn.State != ConnectionState.Open)
        	DbConn.Open();

        string sqlIns = "INSERT INTO YOUR_TABLE (fieldString, fieldBool, fieldNumber) +
                        " VALUES " +
                        "(@fieldString, @fieldBool, @fieldNumber)";

            try
            {
                MySqlCommand cmdIns = new MySqlCommand(sqlIns, DbConn);

                cmdIns.Parameters.AddWithValue("@fieldString", sSomeString.Trim());
                cmdIns.Parameters.AddWithValue("@fieldBool", bSomeBool);
                cmdIns.Parameters.AddWithValue("@fieldNumber", iSomeNumber);
                
                cmdIns.ExecuteNonQuery();

                cmdIns.Parameters.Clear();
                cmdIns.Dispose();
                cmdIns = null;

                bReturn = true;
            }
            catch (MySqlException e)
            {
                // Send the error to your error handler    e.ToString();
                bReturn = false;
            }

            if (DbConn.State == ConnectionState.Open)
                DbConn.Close();

            return (bReturn);
        }
}


public bool GetFirstTableRecordWhereBoolIsTrue(ref string sSomeString, bool bTheBoolValue, ref int iSomeNumber)
{
	bool bFound = false;

        if (DbConn.State != ConnectionState.Open)
	        DbConn.Open();

        try
        {
                string sql = "SELECT * FROM YOUR_TABLE";
                MySqlCommand cmd = new MySqlCommand(sql, DbConn);

                MySqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                   bTheBoolValue = Convert.ToBoolean(rdr[1]);

                   if(bTheBoolValue)
                      bFound = true;
		   sSomeString = rdr[0].ToString();
                   iSomeNumber = Convert.ToInt32(rdr[2]);
                                        
                   break;
                }
                rdr.Close();
            }
            catch (Exception ex)
            {
                // Send the error to your error handler    e.ToString();
                bFound = false;
            }

            if (DbConn.State == ConnectionState.Open)
                DbConn.Close();

            return (bFound);
        }
 
Share this answer
 
v2
Comments
Member 7766180 3-Aug-11 11:08am    
Thanks Rick, That did help. A lot of Useful Information! Thank You Once Again!
My take is that it really depends on your design. If you want to do some conversion on your application, then boolean would make sense. Otherwise, as long as your validation is handled well on the app, why not store it as a string? I believe it would not hurt, in terms of the size that it will consume on your database.
 
Share this answer
 
Comments
Member 7766180 2-Aug-11 21:56pm    
I thought that I read in an earlier post that storing a string is bad.
walterhevedeich 2-Aug-11 22:15pm    
What post?
Member 7766180 2-Aug-11 22:50pm    
On another question that I had asked. Can't remember which one.
Dr.Walt Fair, PE 2-Aug-11 22:53pm    
A string potentially uses more storage, etc. Whether it is a problem or not depends on what you are going to do with the value. Personally I've usually used booleans for yes/no fields wherever possible to avoid confusion.
Member 7766180 3-Aug-11 11:09am    
There is no application so to speak, it's runnibg in console mode and all I need is for it to return a 1 or a 0 to trigger the next event.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900