c#执行大型SQL语句[关闭]

Posted

技术标签:

【中文标题】c#执行大型SQL语句[关闭]【英文标题】:c# execute large SQL statement [closed] 【发布时间】:2014-01-30 19:07:17 【问题描述】:

我有一个大型 SQL 语句,我想在 c# 中运行,但是,我看到的所有示例都是针对一行长度的 SQL 语句。

到目前为止,这是我的 C# 代码。在我的代码之后,我已经包含了查询,我想运行。


C# 查询代码
private void button4_Click(object sender, EventArgs e)
        
           System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection();
           conn.ConnectionString = "Driver=Oracle in OraClient11g_home1;" +
                                   "Dbq=" + "database" +
                                   ";Uid=" + "admin" + ";Pwd=" + "admin" + ";";
           try
           
               conn.Open();
               OdbcCommand com = new OdbcCommand("select count(*) from user_tables;", conn);
               com.CommandType = CommandType.Text;
               OdbcDataReader rdr = com.ExecuteReader();
               while (rdr.Read())
               
                   var mystring = rdr.GetString(0);
                   MessageBox.Show(mystring);
                               
           
           catch (Exception ex)
           
               MessageBox.Show(ex.Message); 
           
           conn.Close();
        


要运行的查询
SELECT user_id,
         user_name,
         user_name_full,
         (SELECT user_name
            FROM MD_SITE_USER
           WHERE user_id = u.GROUP_ID)
            assigned_user_group,
         user_or_group,
         CASE
            WHEN (SELECT o.grant_or_revoke
                    FROM MD_SITE_APP_ACTION a, MD_SITE_APP_ACTION_OWNER o
                   WHERE     a.action_id = o.action_id
                         AND a.action_name = 'Admin.start'
                         AND o.user_id = u.GROUP_ID) = '+'
            THEN
               'Y'
            ELSE
               'N'
         END
            "Assigned By Group?",
         CASE
            WHEN (SELECT o.grant_or_revoke
                    FROM MD_SITE_APP_ACTION a, MD_SITE_APP_ACTION_OWNER o
                   WHERE     a.action_id = o.action_id
                         AND a.action_name = 'Admin.start'
                         AND o.user_id = u.user_id) = '+'
            THEN
               'Y'
            ELSE
               'N'
         END
            "Assigned By User?"
    FROM MD_SITE_USER u
   WHERE     user_name IS NOT NULL
         AND (   CASE
                    WHEN (SELECT o.grant_or_revoke
                            FROM MD_SITE_APP_ACTION a,
                                 MD_SITE_APP_ACTION_OWNER o
                           WHERE     a.action_id = o.action_id
                                 AND a.action_name = 'Admin.start'
                                 AND o.user_id = u.GROUP_ID) = '+'
                    THEN
                       'Y'
                    ELSE
                       'N'
                 END = 'Y'
              OR CASE
                    WHEN (SELECT o.grant_or_revoke
                            FROM MD_SITE_APP_ACTION a,
                                 MD_SITE_APP_ACTION_OWNER o
                           WHERE     a.action_id = o.action_id
                                 AND a.action_name = 'Admin.start'
                                 AND o.user_id = u.user_id) = '+'
                    THEN
                       'Y'
                    ELSE
                       'N'
                 END = 'Y')
ORDER BY user_or_group, user_name;

【问题讨论】:

写成一行,或者使用StringBuilder 你是问如何运行这么长的代码吗?您可以在一个过程中创建它并执行该过程。 使它成为一个存储过程。为什么需要运行所有这些内联 sql 代码,看起来很可怕! 【参考方案1】:

这样做...或者使用 StringBuilder 类...

    new OdbcCommand(@"SELECT user_id,
         user_name,
         user_name_full,
         (SELECT user_name
            FROM MD_SITE_USER
           WHERE user_id = u.GROUP_ID)
            assigned_user_group,
         user_or_group,
         CASE
            WHEN (SELECT o.grant_or_revoke
                    FROM MD_SITE_APP_ACTION a, MD_SITE_APP_ACTION_OWNER o
                   WHERE     a.action_id = o.action_id
                         AND a.action_name = 'Admin.start'
                         AND o.user_id = u.GROUP_ID) = '+'
            THEN
               'Y'
            ELSE
               'N'
         END
            'Assigned By Group?',
         CASE
            WHEN (SELECT o.grant_or_revoke
                    FROM MD_SITE_APP_ACTION a, MD_SITE_APP_ACTION_OWNER o
                   WHERE     a.action_id = o.action_id
                         AND a.action_name = 'Admin.start'
                         AND o.user_id = u.user_id) = '+'
            THEN
               'Y'
            ELSE
               'N'
         END
            'Assigned By User?'
    FROM MD_SITE_USER u
   WHERE     user_name IS NOT NULL
         AND (   CASE
                    WHEN (SELECT o.grant_or_revoke
                            FROM MD_SITE_APP_ACTION a,
                                 MD_SITE_APP_ACTION_OWNER o
                           WHERE     a.action_id = o.action_id
                                 AND a.action_name = 'Admin.start'
                                 AND o.user_id = u.GROUP_ID) = '+'
                    THEN
                       'Y'
                    ELSE
                       'N'
                 END = 'Y'
              OR CASE
                    WHEN (SELECT o.grant_or_revoke
                            FROM MD_SITE_APP_ACTION a,
                                 MD_SITE_APP_ACTION_OWNER o
                           WHERE     a.action_id = o.action_id
                                 AND a.action_name = 'Admin.start'
                                 AND o.user_id = u.user_id) = '+'
                    THEN
                       'Y'
                    ELSE
                       'N'
                 END = 'Y')
ORDER BY user_or_group, user_name;", conn);

【讨论】:

【参考方案2】:

您可以通过@ 符号使用string literal

string longstring = @"SELECT
        *
    FROM MyTable
  ";

请注意,在您的情况下,您的查询中有 "。要逃避这些,请将它们替换为 ""

string myQuery = @"SELECT user_id,
     user_name,
     user_name_full,
     (SELECT user_name
        FROM MD_SITE_USER
       WHERE user_id = u.GROUP_ID)
        assigned_user_group,
     user_or_group,
     CASE
        WHEN (SELECT o.grant_or_revoke
                FROM MD_SITE_APP_ACTION a, MD_SITE_APP_ACTION_OWNER o
               WHERE     a.action_id = o.action_id
                     AND a.action_name = 'Admin.start'
                     AND o.user_id = u.GROUP_ID) = '+'
        THEN
           'Y'
        ELSE
           'N'
     END
        ""Assigned By Group?"",
     CASE
        WHEN (SELECT o.grant_or_revoke
                FROM MD_SITE_APP_ACTION a, MD_SITE_APP_ACTION_OWNER o
               WHERE     a.action_id = o.action_id
                     AND a.action_name = 'Admin.start'
                     AND o.user_id = u.user_id) = '+'
        THEN
           'Y'
        ELSE
           'N'
     END
        ""Assigned By User?""
FROM MD_SITE_USER u
WHERE     user_name IS NOT NULL
     AND (   CASE
                WHEN (SELECT o.grant_or_revoke
                        FROM MD_SITE_APP_ACTION a,
                             MD_SITE_APP_ACTION_OWNER o
                       WHERE     a.action_id = o.action_id
                             AND a.action_name = 'Admin.start'
                             AND o.user_id = u.GROUP_ID) = '+'
                THEN
                   'Y'
                ELSE
                   'N'
             END = 'Y'
          OR CASE
                WHEN (SELECT o.grant_or_revoke
                        FROM MD_SITE_APP_ACTION a,
                             MD_SITE_APP_ACTION_OWNER o
                       WHERE     a.action_id = o.action_id
                             AND a.action_name = 'Admin.start'
                             AND o.user_id = u.user_id) = '+'
                THEN
                   'Y'
                ELSE
                   'N'
             END = 'Y')
 ORDER BY user_or_group, user_name;
";

【讨论】:

有什么理由,这样的东西行不通吗? 我尝试从文件中读取 SQL,但它出错了(语法)。我知道我错过了一些简单的东西。我确实将它剪切并粘贴到我的代码中并且它以这种方式工作,但我希望能够从文件中读取 SQL。不允许存储过程。 如果你从一个文件中读取它,你不需要转义引号(如果你这样做了) 从文件读取与将其作为字符串包含的唯一区别是引号的转义,我没有以@符号开头。 根据我的 messagebox.show,当使用流式阅读器读取行时,正在添加字符。不知道为什么会这样。

以上是关于c#执行大型SQL语句[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

执行大型 SQL 脚本(使用 GO 命令)

c#用 ef怎么执行sql语句

c# 实现定义一套中间SQL可以跨库执行的SQL语句

如何使用 OracleCommand C# 执行不同的多条 SQL 语句

如何用C#程序直接执行.sql文件里的SQL语句?急着用啊!新手求高手指点

为啥C#中数据库sql语句中在表的前面加上.dbo,不加也能正常执行,两者有啥区别!