用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c#代码库

动态 SQL 生成工具

2016-08-14 作者: 小章举报

[c#]代码库

using System;
using System.Collections.Generic;
 
namespace ObjectiveSQL
{
    class Program
    {
        static void Main(string[] args)
        {
            testSelect();
            testUpdate();
            testInsert();
            testDelete();
            try
            {
                testInsertWhere();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
 
        static void testSelect()
        {
            string usernamePrefix = "JOHN%";
            string role = "admin";
            string username = "admin";
 
            Command command = SQL.SELECT("*").From("USERS")
                            .Where("USERNAME LIKE ?", usernamePrefix)
                            .And("(ROLE=?", role).Or("USERNAME=?)", username).toCommand();
            Console.WriteLine(command.getStatement());
 
            command = SQL.SELECT("*").From("USERS")
                            .Where("USERNAME LIKE ?", usernamePrefix)
                            .Append("AND (")
                            .Append(role != null, "ROLE = ?", role)
                            .Or("USERNAME = ?", username)
                            .Append(")").toCommand();
            Console.WriteLine(command.getStatement());
 
            role = null;
            command = SQL.SELECT("COUNT(1), ROLE")
                            .From("USERS")
                            .Where(false, "REGISTER_TIME > sysdate - 1")    // dismissed
                            .AndIfNotEmpty("ROLE = ?", role)          // dismissed
                            .And("1=1")
                            .GroupBy("ROLE").toCommand();
            Console.WriteLine(command.getStatement());
 
 
            List<int> levels = new List<int>() { 1, 2, 3 };
            command = SQL.SELECT("*")
                            .From("USERS")
                            .Where("USER_LEVEL IN ?", levels).toCommand();
 
            Console.WriteLine(command.getStatement());
        }
 
        static void testUpdate()
        {
            Command command = SQL.UPDATE("USER")
                            .Set("AGE", 3)
                            .Set(false, "NAME", "admin").Where("ID=?", 1).toCommand();
            Console.WriteLine(command.getStatement());
        }
 
        static void testInsert()
        {
            Command command = SQL.INSERT("USER")
                            .Values("ID", 1)
                            .Values("USERNAME", "admin")
                            .Values("PASSWORD", "admin")
                            .Values("AGE", null).toCommand();
            Console.WriteLine(command.getStatement());
        }
 
        static void testInsertWhere()
        {
            Command command = SQL.INSERT("USER").Values("NAME", "admin").Where("").toCommand();
            Console.WriteLine(command.getStatement());
        }
 
        static void testDelete()
        {
            Command command = SQL.DELETE("USER").Where("ID in ?", new List<int>() { 1, 2, 3, 4, 5 }).toCommand();
            Console.WriteLine(command.getStatement());
        }
    }
}

[源代码打包下载]




网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...