Example #1
0
        public int AddRecipeToDatabase(Recipe recipe)
        {
            var           conn = new MySql.Data.MySqlClient.MySqlConnection(ConnectionString);
            StringBuilder sb   = new StringBuilder();

            sb.AppendFormat("INSERT INTO recipe_schema.recipe" + "({0},{1},{2})  VALUES ('{3}', '{4}', '{5}')", "Name", "Contributor", "Description", recipe.Name, recipe.Contributor, recipe.Description);
            conn.Query(sb.ToString());
            return((int)(UInt64)conn.ExecuteScalar("SELECT LAST_INSERT_ID()"));
        }
Example #2
0
        public T ExecuteScalar <T>(string connection, string sql, object parameters)
        {
            T result = default;

            using (var c = new MySqlData.MySqlClient.MySqlConnection(connection))
            {
                _log.LogTrace <MySqlQuery>($"EXECUTE: {sql}. Parameters: {JsonConvert.SerializeObject(parameters)}");

                c.Open();

                result = c.ExecuteScalar <T>(sql, parameters);

                c.Close();
            }

            return(result);
        }
Example #3
0
        /// <summary>
        /// ExecuteScalar 執行一個SQL命令返回結果集的第一列的第一行。
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public object ExecuteScale(string sql)
        {
            object oo = null;

            try
            {
                //using (var cn = new System.Data.SqlClient.SqlConnection(ConnStr))
                //{
                //    oo = cn.ExecuteScalar(sql);
                //}

                if (_ProviderName == "System.Data.SqlClient")
                {
                    using (System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(ConnStr))
                    {
                        oo = cn.ExecuteScalar(sql);
                    }
                }
                else if (_ProviderName == "MySql.Data.MySqlClient")
                {
                    using (MySql.Data.MySqlClient.MySqlConnection cn = new MySql.Data.MySqlClient.MySqlConnection(ConnStr))
                    {
                        oo = cn.ExecuteScalar(sql);
                    }
                }
                else
                {
                    using (System.Data.SqlClient.SqlConnection cn = SqlClient)
                    {
                        oo = cn.ExecuteScalar(sql);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(oo);
        }