在MySQL中存储值 [英] Storing a Value in MySQL

查看:54
本文介绍了在MySQL中存储值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在在线mysql数据库中存储一个值.这是一个是/否字段.什么是最好的,也是最少的?另存为布尔值0/-1.作为字符串,是/否或整数0/1.谢谢

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

推荐答案

您将不得不转换所有内容.
在此示例中,表格中的字段为

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

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



我的最佳成绩如下

希望这会有所帮助...



I''ve had the best results as below

Hope this helps......

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);
        }


我认为这确实取决于您的设计.如果要在应用程序上进行一些转换,则布尔值是有意义的.否则,只要您的验证在应用程序中得到很好的处理,为什么不将其存储为字符串?我相信它不会对您的数据库消耗的大小造成损害.
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.


这篇关于在MySQL中存储值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆